Commit b85024ba2bfaa26d9b3c2e44fdffbd2607ada58b
1 parent
77070cb1
bind automatically and set socket to nonblocking. Add testcode.
Showing
5 changed files
with
151 additions
and
2 deletions
@@ -36,7 +36,13 @@ static | @@ -36,7 +36,13 @@ static | ||
36 | int | 36 | int |
37 | connEntryPointCtor(void * _this, va_list * params) | 37 | connEntryPointCtor(void * _this, va_list * params) |
38 | { | 38 | { |
39 | + TR_CommEndPoint this = _this; | ||
40 | + | ||
39 | TR_PARENTCALL(TR_ConnEntryPoint, _this, TR_Class, ctor, params); | 41 | TR_PARENTCALL(TR_ConnEntryPoint, _this, TR_Class, ctor, params); |
42 | + | ||
43 | + TR_socketBind((TR_Socket)this->transport); | ||
44 | + TR_socketNonblock((TR_Socket)this->transport); | ||
45 | + | ||
40 | return 0; | 46 | return 0; |
41 | } | 47 | } |
42 | 48 |
@@ -32,6 +32,7 @@ | @@ -32,6 +32,7 @@ | ||
32 | #include "tr/connector.h" | 32 | #include "tr/connector.h" |
33 | #include "tr/connection.h" | 33 | #include "tr/connection.h" |
34 | #include "tr/protocol.h" | 34 | #include "tr/protocol.h" |
35 | +#include "tr/connect_entry_point.h" | ||
35 | 36 | ||
36 | static | 37 | static |
37 | int | 38 | int |
@@ -84,8 +85,8 @@ connectorCvInit(TR_class_ptr cls) | @@ -84,8 +85,8 @@ connectorCvInit(TR_class_ptr cls) | ||
84 | { | 85 | { |
85 | TR_EVENT_HANDLER_SET_METHOD( | 86 | TR_EVENT_HANDLER_SET_METHOD( |
86 | cls, | 87 | cls, |
87 | - TR_CommEndPoint, | ||
88 | - TR_CEP_EVENT_READ_READY, | 88 | + TR_ConnEntryPoint, |
89 | + TR_CET_EVENT_ACC_READY, | ||
89 | connectorAccept); | 90 | connectorAccept); |
90 | } | 91 | } |
91 | 92 |
testers/build.sh
0 → 100644
testers/testserver.c
0 → 100644
1 | +#include <stdio.h> | ||
2 | + | ||
3 | +#include "trbase.h" | ||
4 | +#include "trcomm.h" | ||
5 | +#include "trevent.h" | ||
6 | + | ||
7 | +TR_CLASS(TestHandler) { | ||
8 | + TR_EXTENDS(TR_EventHandler); | ||
9 | +}; | ||
10 | +TR_INSTANCE_INIT(TestHandler); | ||
11 | +TR_CLASSVARS_DECL(TestHandler) { | ||
12 | + TR_CV_EXTENDS(TR_EventHandler); | ||
13 | +}; | ||
14 | + | ||
15 | +static | ||
16 | +int | ||
17 | +testHandlerHeartbeat(TR_EventHandler this, TR_Event event) | ||
18 | +{ | ||
19 | + puts("heartbeat"); | ||
20 | + return FALSE; | ||
21 | +} | ||
22 | + | ||
23 | +static | ||
24 | +int | ||
25 | +testHandlerNewMessage(TR_EventHandler this, TR_Event event) | ||
26 | +{ | ||
27 | + puts("new message"); | ||
28 | + return FALSE; | ||
29 | +} | ||
30 | + | ||
31 | +static | ||
32 | +int | ||
33 | +testHandlerClose(TR_EventHandler this, TR_Event event) | ||
34 | +{ | ||
35 | + puts("close"); | ||
36 | + return FALSE; | ||
37 | +} | ||
38 | + | ||
39 | +static | ||
40 | +int | ||
41 | +testHandlerUpgrade(TR_EventHandler this, TR_Event event) | ||
42 | +{ | ||
43 | + puts("upgrade"); | ||
44 | + return FALSE; | ||
45 | +} | ||
46 | + | ||
47 | +static | ||
48 | +int | ||
49 | +testHandlerCtor(void * _this, va_list * params) | ||
50 | +{ | ||
51 | + TR_PARENTCALL(TestHandler, _this, TR_Class, ctor, params); | ||
52 | + return 0; | ||
53 | +} | ||
54 | + | ||
55 | +static | ||
56 | +void | ||
57 | +testHandlerDtor(void * _this, va_list * params) | ||
58 | +{ | ||
59 | + TR_PARENTCALL(TestHandler, _this, TR_Class, dtor); | ||
60 | +} | ||
61 | + | ||
62 | +static | ||
63 | +void | ||
64 | +testHandlerCvInit(TR_class_ptr class) | ||
65 | +{ | ||
66 | + TR_EVENT_HANDLER_SET_METHOD( | ||
67 | + class, | ||
68 | + TR_EventDispatcher, | ||
69 | + TR_DISPATCHER_EVENT_HEARTBEAT, | ||
70 | + testHandlerHeartbeat); | ||
71 | + TR_EVENT_HANDLER_SET_METHOD( | ||
72 | + class, | ||
73 | + TR_CommEndPoint, | ||
74 | + TR_CEP_EVENT_NEW_MSG, | ||
75 | + testHandlerNewMessage); | ||
76 | + TR_EVENT_HANDLER_SET_METHOD( | ||
77 | + class, | ||
78 | + TR_EventDispatcher, | ||
79 | + TR_CEP_EVENT_CLOSE, | ||
80 | + testHandlerClose); | ||
81 | + TR_EVENT_HANDLER_SET_METHOD( | ||
82 | + class, | ||
83 | + TR_EventDispatcher, | ||
84 | + TR_CEP_EVENT_UPGRADE, | ||
85 | + testHandlerUpgrade); | ||
86 | +} | ||
87 | + | ||
88 | +TR_INSTANCE(TR_Hash, testHandlerEventMethods); | ||
89 | +TR_INIT_IFACE(TR_Class, testHandlerCtor, testHandlerDtor, NULL); | ||
90 | +TR_CREATE_CLASS( | ||
91 | + TestHandler, | ||
92 | + TR_EventHandler, | ||
93 | + testHandlerCvInit, | ||
94 | + TR_IF(TR_Class)) = { | ||
95 | + { &(_testHandlerEventMethods.data) } | ||
96 | +}; | ||
97 | + | ||
98 | +TR_INSTANCE(TR_LoggerSyslog, mylogger, {TR_LOGGER_DEBUG}); | ||
99 | + | ||
100 | +int | ||
101 | +main (int argc, char * argv[]) | ||
102 | +{ | ||
103 | + TR_CommManager cmgr = (TR_CommManager)TR_new(TR_CommManagerPoll); | ||
104 | + TR_EventDispatcher dispatcher = TR_new(TR_EventDispatcher); | ||
105 | + TR_ConnEntryPoint ep; | ||
106 | + TR_TcpSocket ep_sock; | ||
107 | + TR_Protocol protocol; | ||
108 | + | ||
109 | + TR_logger = TR_INSTANCE_CAST(TR_Logger, mylogger); | ||
110 | + | ||
111 | + TR_eventDispatcherRegisterHandler(dispatcher, (TR_EventHandler)cmgr); | ||
112 | + TR_eventDispatcherRegisterHandler(dispatcher, | ||
113 | + (TR_EventHandler)TR_new(TR_Connector)); | ||
114 | + TR_eventDispatcherRegisterHandler(dispatcher, | ||
115 | + (TR_EventHandler)TR_new(TR_IoHandler)); | ||
116 | + TR_eventDispatcherRegisterHandler(dispatcher, | ||
117 | + (TR_EventHandler)TR_new(TR_ProtocolHandler)); | ||
118 | + TR_eventDispatcherRegisterHandler(dispatcher, | ||
119 | + (TR_EventHandler)TR_new(TestHandler)); | ||
120 | + | ||
121 | + protocol = TR_new(TR_ProtocolRaw); | ||
122 | + ep_sock = TR_new(TR_TcpSocket, TR_logger, "0.0.0.0", 5678, 0); | ||
123 | + ep = TR_new(TR_ConnEntryPoint, ep_sock, protocol); | ||
124 | + | ||
125 | + TR_commManagerAddEndpoint(cmgr, (TR_CommEndPoint)ep); | ||
126 | + | ||
127 | + TR_eventDispatcherSetHeartbeat(dispatcher, 1000); | ||
128 | + TR_eventDispatcherStart(dispatcher); | ||
129 | + TR_eventHandlerClassCleanup(TestHandler); | ||
130 | + | ||
131 | + TR_delete(cmgr); | ||
132 | + TR_delete(dispatcher); | ||
133 | + | ||
134 | + return 0; | ||
135 | +} | ||
136 | + | ||
137 | +// vim: set ts=4 sw=4: |
Please
register
or
login
to post a comment