Commit 31b1b404c6db3a5cf52a90a4527807989410b432
1 parent
5cfb6cd2
add communication manager. These are the things that do poll, select, waitformul…
…tipleobjects or whatever.
Showing
8 changed files
with
672 additions
and
0 deletions
include/tr/comm_manager.h
0 → 100644
1 | +/** | |
2 | + * \file | |
3 | + * | |
4 | + * \author Georg Hopp | |
5 | + * | |
6 | + * \copyright | |
7 | + * Copyright © 2014 Georg Hopp | |
8 | + * | |
9 | + * This program is free software: you can redistribute it and/or modify | |
10 | + * it under the terms of the GNU General Public License as published by | |
11 | + * the Free Software Foundation, either version 3 of the License, or | |
12 | + * (at your option) any later version. | |
13 | + * | |
14 | + * This program is distributed in the hope that it will be useful, | |
15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | + * GNU General Public License for more details. | |
18 | + * | |
19 | + * You should have received a copy of the GNU General Public License | |
20 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
21 | + */ | |
22 | + | |
23 | +#ifndef __TR_COMM_MANAGER_H__ | |
24 | +#define __TR_COMM_MANAGER_H__ | |
25 | + | |
26 | +#include <sys/types.h> | |
27 | + | |
28 | +#include "trbase.h" | |
29 | +#include "trevent.h" | |
30 | + | |
31 | +TR_CLASS(TR_CommManager) { | |
32 | + TR_EXTENDS(TR_EventHandler); | |
33 | + | |
34 | + TR_CommEndPoint * endpoints; | |
35 | + nfds_t n_endpoints; | |
36 | +}; | |
37 | +TR_INSTANCE_INIT(TR_CommManager); | |
38 | +TR_CLASSVARS_DECL(TR_CommManager) { | |
39 | + TR_CV_EXTENDS(TR_EventHandler); | |
40 | +}; | |
41 | + | |
42 | +void TR_commManagerAddEndpoint(void *, TR_CommEndPoint); | |
43 | +int TR_commManagerShutdown(void * _this, TR_Event event); | |
44 | + | |
45 | +#endif // __TR_COMM_MANAGER_H__ | |
46 | + | |
47 | +// vim: set ts=4 sw=4: | |
48 | + | ... | ... |
include/tr/comm_manager_poll.h
0 → 100644
1 | +/** | |
2 | + * \file | |
3 | + * | |
4 | + * \author Georg Hopp | |
5 | + * | |
6 | + * \copyright | |
7 | + * Copyright © 2014 Georg Hopp | |
8 | + * | |
9 | + * This program is free software: you can redistribute it and/or modify | |
10 | + * it under the terms of the GNU General Public License as published by | |
11 | + * the Free Software Foundation, either version 3 of the License, or | |
12 | + * (at your option) any later version. | |
13 | + * | |
14 | + * This program is distributed in the hope that it will be useful, | |
15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | + * GNU General Public License for more details. | |
18 | + * | |
19 | + * You should have received a copy of the GNU General Public License | |
20 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
21 | + */ | |
22 | + | |
23 | +#ifndef __TR_COMM_MANAGER_POLL_H__ | |
24 | +#define __TR_COMM_MANAGER_POLL_H__ | |
25 | + | |
26 | +#include <sys/types.h> | |
27 | +#include <poll.h> | |
28 | + | |
29 | +#include "trbase.h" | |
30 | +#include "trevent.h" | |
31 | + | |
32 | +TR_CLASS(TR_CommManagerPoll) { | |
33 | + TR_EXTENDS(TR_CommManager); | |
34 | + | |
35 | + struct pollfd * fds; | |
36 | +}; | |
37 | +TR_INSTANCE_INIT(TR_CommManagerPoll); | |
38 | +TR_CLASSVARS_DECL(TR_CommManagerPoll) { | |
39 | + TR_CV_EXTENDS(TR_EventHandler); | |
40 | +}; | |
41 | + | |
42 | +#endif // __TR_COMM_MANAGER_POLL_H__ | |
43 | + | |
44 | +// vim: set ts=4 sw=4: | |
45 | + | ... | ... |
include/tr/interface/comm_manager.h
0 → 100644
1 | +/** | |
2 | + * \file | |
3 | + * | |
4 | + * \author Georg Hopp | |
5 | + * | |
6 | + * \copyright | |
7 | + * Copyright © 2014 Georg Hopp | |
8 | + * | |
9 | + * This program is free software: you can redistribute it and/or modify | |
10 | + * it under the terms of the GNU General Public License as published by | |
11 | + * the Free Software Foundation, either version 3 of the License, or | |
12 | + * (at your option) any later version. | |
13 | + * | |
14 | + * This program is distributed in the hope that it will be useful, | |
15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | + * GNU General Public License for more details. | |
18 | + * | |
19 | + * You should have received a copy of the GNU General Public License | |
20 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
21 | + */ | |
22 | + | |
23 | +#ifndef __TR_INTERFACE_COMM_MANAGER_H__ | |
24 | +#define __TR_INTERFACE_COMM_MANAGER_H__ | |
25 | + | |
26 | +#include <sys/types.h> | |
27 | + | |
28 | +#include "trbase.h" | |
29 | +#include "trevent.h" | |
30 | + | |
31 | +#include "tr/comm_end_point.h" | |
32 | + | |
33 | +typedef void (* fptr_TR_commManagerAddEndpoint)(void *, TR_CommEndPoint); | |
34 | +typedef void (* fptr_TR_commManagerSelect)(void *, TR_Event, int); | |
35 | +typedef void (* fptr_TR_commManagerEnableWrite)(void *, TR_Event); | |
36 | +typedef void (* fptr_TR_commManagerDisableWrite)(void *, TR_Event); | |
37 | +typedef void (* fptr_TR_commManagerClose)(void *, TR_Event); | |
38 | +typedef void (* fptr_TR_commManagerShutdownRead)(void *, TR_Event); | |
39 | +typedef void (* fptr_TR_commManagerShutdownRead)(void *, TR_Event); | |
40 | + | |
41 | +TR_INTERFACE(TR_CommManager) { | |
42 | + TR_IFID; | |
43 | + fptr_TR_commManagerAddEndpoint addEndpoint; | |
44 | + fptr_TR_commManagerSelect select; | |
45 | + fptr_TR_commManagerEnableWrite enableWrite; | |
46 | + fptr_TR_commManagerDisableWrite disableWrite; | |
47 | + fptr_TR_commManagerClose close; | |
48 | + fptr_TR_commManagerShutdownRead shutdownWrite; | |
49 | + fptr_TR_commManagerShutdownRead shutdownRead; | |
50 | +}; | |
51 | + | |
52 | +void TR_commManagerAddEndpoint(void *, TR_CommEndPoint); | |
53 | + | |
54 | +#endif // __TR_INTERFACE_COMM_MANAGER_H__ | |
55 | + | |
56 | +// vim: set ts=4 sw=4: | ... | ... |
src/comm_manager.c
0 → 100644
1 | +/** | |
2 | + * \file | |
3 | + * | |
4 | + * \author Georg Hopp | |
5 | + * | |
6 | + * \copyright | |
7 | + * Copyright © 2014 Georg Hopp | |
8 | + * | |
9 | + * This program is free software: you can redistribute it and/or modify | |
10 | + * it under the terms of the GNU General Public License as published by | |
11 | + * the Free Software Foundation, either version 3 of the License, or | |
12 | + * (at your option) any later version. | |
13 | + * | |
14 | + * This program is distributed in the hope that it will be useful, | |
15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | + * GNU General Public License for more details. | |
18 | + * | |
19 | + * You should have received a copy of the GNU General Public License | |
20 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
21 | + */ | |
22 | + | |
23 | +#include <unistd.h> | |
24 | + | |
25 | +#include "trbase.h" | |
26 | +#include "trevent.h" | |
27 | + | |
28 | +#include "tr/comm_end_point.h" | |
29 | +#include "tr/connection.h" | |
30 | +#include "tr/comm_manager.h" | |
31 | +#include "tr/interface/comm_manager.h" | |
32 | + | |
33 | +static | |
34 | +int | |
35 | +commManagerCtor(void * _this, va_list * params) | |
36 | +{ | |
37 | + TR_CommManager this = _this; | |
38 | + | |
39 | + this->n_endpoints = sysconf(_SC_OPEN_MAX); | |
40 | + this->endpoints = TR_calloc(sizeof(TR_CommEndPoint), this->n_endpoints); | |
41 | + | |
42 | + return 0; | |
43 | +} | |
44 | + | |
45 | +static | |
46 | +void | |
47 | +commManagerDtor(void * _this) | |
48 | +{ | |
49 | + TR_CommManager this = _this; | |
50 | + nfds_t i; | |
51 | + | |
52 | + for (i = 0; i < this->n_endpoints; i++) { | |
53 | + TR_delete(this->endpoints[i]); | |
54 | + } | |
55 | + | |
56 | + TR_PARENTCALL(_this, TR_Class, dtor); | |
57 | +} | |
58 | + | |
59 | +static | |
60 | +int | |
61 | +TR__commManagerAddEndpoint(void * _this, Event event) | |
62 | +{ | |
63 | + TR_commManagerAddEndpoint( | |
64 | + (TR_CommManager)_this, | |
65 | + (TR_CommEndPoint)event->subject); | |
66 | + return 1; | |
67 | +} | |
68 | + | |
69 | +void TR_commManagerSelect(void *, TR_Event, int); | |
70 | +void TR_commManagerEnableWrite(void *, TR_Event); | |
71 | +void TR_commManagerDisableWrite(void *, TR_Event); | |
72 | +void TR_commManagerClose(void *, TR_Event); | |
73 | +void TR_commManagerShutdownRead(void *, TR_Event); | |
74 | +void TR_commManagerShutdownRead(void *, TR_Event); | |
75 | + | |
76 | +static | |
77 | +void | |
78 | +commManagerCvInit(TR_class_ptr cls) | |
79 | +{ | |
80 | + TR_EVENT_HANDLER_SET_METHOD( | |
81 | + cls, | |
82 | + TR_EventDispatcher, | |
83 | + TR_DISPATCHER_EVENT_DATA_WAIT, | |
84 | + TR_commManagerSelect); | |
85 | + | |
86 | + TR_EVENT_HANDLER_SET_METHOD( | |
87 | + cls, | |
88 | + TR_EventDispatcher, | |
89 | + TR_DISPATCHER_EVENT_SHUTDOWN, | |
90 | + TR_commManagerShutdown); | |
91 | + | |
92 | + TR_EVENT_HANDLER_SET_METHOD( | |
93 | + cls, | |
94 | + TR_Connection, | |
95 | + TR_CON_EVENT_NEW_CON, | |
96 | + TR__commManagerAddEndpoint); | |
97 | + | |
98 | + TR_EVENT_HANDLER_SET_METHOD( | |
99 | + cls, | |
100 | + TR_Connection, | |
101 | + TR_CON_EVENT_PENDING_DATA, | |
102 | + TR_commManagerEnableWrite); | |
103 | + | |
104 | + TR_EVENT_HANDLER_SET_METHOD( | |
105 | + cls, | |
106 | + TR_Connection, | |
107 | + TR_CON_EVENT_END_DATA, | |
108 | + TR_commManagerDisableWrite); | |
109 | + | |
110 | + TR_EVENT_HANDLER_SET_METHOD( | |
111 | + cls, | |
112 | + TR_CommEndPoint, | |
113 | + TR_CEP_EVENT_CLOSE, | |
114 | + TR_commManagerClose); | |
115 | + | |
116 | + TR_EVENT_HANDLER_SET_METHOD( | |
117 | + cls, | |
118 | + TR_CommEndPoint, | |
119 | + TR_CEP_EVENT_SHUT_READ, | |
120 | + TR_commManagerShutdownRead); | |
121 | + | |
122 | + TR_EVENT_HANDLER_SET_METHOD( | |
123 | + cls, | |
124 | + TR_CommEndPoint, | |
125 | + TR_CEP_EVENT_SHUT_READ, | |
126 | + TR_commManagerShutdownWrite); | |
127 | +} | |
128 | + | |
129 | +TR_INSTANCE(TR_Hash, _event_methods); | |
130 | +TR_INIT_IFACE(TR_Class, commManagerCtor, commManagerDtor, NULL); | |
131 | +TR_INIT_IFACE(TR_CommManager, NULL, NULL, NULL, NULL, NULL, NULL, NULL); | |
132 | +TR_CREATE_CLASS( | |
133 | + TR_CommManager, | |
134 | + TR_EventHandler, | |
135 | + commManagerCvInit, | |
136 | + TR_IF(TR_Class), | |
137 | + TR_IF(TR_CommManager)) = { | |
138 | + { &(__event_methods.data) } | |
139 | +}; | |
140 | + | |
141 | +// vim: set ts=4 sw=4: | ... | ... |
src/comm_manager_poll.c
0 → 100644
1 | +/** | |
2 | + * \file | |
3 | + * | |
4 | + * \author Georg Hopp | |
5 | + * | |
6 | + * \copyright | |
7 | + * Copyright © 2014 Georg Hopp | |
8 | + * | |
9 | + * This program is free software: you can redistribute it and/or modify | |
10 | + * it under the terms of the GNU General Public License as published by | |
11 | + * the Free Software Foundation, either version 3 of the License, or | |
12 | + * (at your option) any later version. | |
13 | + * | |
14 | + * This program is distributed in the hope that it will be useful, | |
15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | + * GNU General Public License for more details. | |
18 | + * | |
19 | + * You should have received a copy of the GNU General Public License | |
20 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
21 | + */ | |
22 | + | |
23 | +#include <unistd.h> | |
24 | + | |
25 | +#include "trbase.h" | |
26 | +#include "trevent.h" | |
27 | + | |
28 | +#include "tr/comm_end_point.h" | |
29 | +#include "tr/connection.h" | |
30 | +#include "tr/comm_manager.h" | |
31 | +#include "tr/interface/comm_manager.h" | |
32 | + | |
33 | +static | |
34 | +int | |
35 | +commManagerPollCtor(void * _this, va_list * params) | |
36 | +{ | |
37 | + TR_CommManagerPoll this = _this; | |
38 | + TR_CommManager cmgr = _this; | |
39 | + nfds_t i; | |
40 | + | |
41 | + TR_PARENTCALL(_this, TR_Class, ctor, params); | |
42 | + this->fds = TR_malloc(sizeof(struct pollfd) * cmgr->n_endpoints); | |
43 | + for (i = 0; i < cmgr->n_endpoints; i++) { | |
44 | + this->fds[i] = {-1, 0, 0}; | |
45 | + } | |
46 | + | |
47 | + return 0; | |
48 | +} | |
49 | + | |
50 | +static | |
51 | +void | |
52 | +commManagerPollDtor(void * _this) | |
53 | +{ | |
54 | + TR_CommManagerPoll this = _this; | |
55 | + | |
56 | + TR_MEM_FREE(this->fds); | |
57 | + TR_PARENTCALL(_this, TR_Class, dtor); | |
58 | +} | |
59 | + | |
60 | +static | |
61 | +void | |
62 | +TR_commManagerAddEndpoint(void * _this, TR_CommEndPoint endpoint) | |
63 | +{ | |
64 | + TR_CommManagerPoll this = _this; | |
65 | + this->fds[endpoint->transport->handle].fd = endpoint->transport->handle; | |
66 | + this->fds[endpoint->transport->handle].events = POLLIN; | |
67 | +} | |
68 | + | |
69 | +static | |
70 | +void | |
71 | +TR_commManagerSelect(void * _this, TR_Event event, int timeout) | |
72 | +{ | |
73 | + TR_CommManagerPoll this = _this; | |
74 | + TR_CommManager cmgr = _this; | |
75 | + nfds_t i; | |
76 | + int nevents; | |
77 | + | |
78 | + nevents = poll(this->fds, cmgr->n_endpoints, timeout); | |
79 | + | |
80 | + for (i = 0; i < cmgr->n_endpoints, i++) { | |
81 | + TR_CommEndPoint endpoint = this->endpoints[i]; | |
82 | + | |
83 | + if (this->fds[i].revents & POLLIN == POLLIN) { | |
84 | + nevents--; | |
85 | + if (TR_INSTANCE_OF(TR_TcpSocket, endpoints->transport) | |
86 | + && ((TR_TcpSocket)endpoint->transport)->listen) { | |
87 | + TR_eventHandlerIssueEvent( | |
88 | + (TR_EventHandler)this, | |
89 | + (TR_Connection)endpoint, | |
90 | + TR_CON_EVENT_ACC_READY, | |
91 | + NULL); | |
92 | + } else { | |
93 | + TR_eventHandlerIssueEvent( | |
94 | + (TR_EventHandler)this, | |
95 | + endpoint, | |
96 | + TR_CET_EVENT_READ_READY, | |
97 | + NULL); | |
98 | + } | |
99 | + } | |
100 | + | |
101 | + if (this->fds[i].revents & POLLOUT == POLLOUT) { | |
102 | + nevents--; | |
103 | + TR_eventHandlerIssueEvent( | |
104 | + (TR_EventHandler)this, | |
105 | + endpoint, | |
106 | + TR_CET_EVENT_WRITE_READY, | |
107 | + NULL); | |
108 | + } | |
109 | + | |
110 | + if (nevents <= 0) break; | |
111 | + } | |
112 | +} | |
113 | + | |
114 | +static | |
115 | +void | |
116 | +TR_commManagerEnableWrite(void * _this, TR_Event event) | |
117 | +{ | |
118 | + TR_CommManagerPoll this = _this; | |
119 | + TR_CommEndPoint endpoint = (TR_CommEndPoint)event->subject; | |
120 | + | |
121 | + if (! TR_socketFinWr(endpoint->transport)) { | |
122 | + this->fds[endpoint->transport->handle].event |= POLLOUT; | |
123 | + } | |
124 | +} | |
125 | + | |
126 | +static | |
127 | +int | |
128 | +TR_commManagerDisableWrite(void * _this, TR_Event event) | |
129 | +{ | |
130 | + TR_CommManagerPoll this = _this; | |
131 | + TR_CommEndPoint endpoint = (TR_CommEndPoint)event->subject; | |
132 | + | |
133 | + this->fds[endpoint->transport->handle].event &= ~POLLOUT; | |
134 | +} | |
135 | + | |
136 | +static | |
137 | +int | |
138 | +TR_commManagerClose(void * _this, TR_Event event) | |
139 | +{ | |
140 | + TR_CommManagerPoll this = _this; | |
141 | + TR_CommEndPoint endpoint = (TR_CommEndPoint)event->subject; | |
142 | + | |
143 | + this->fds[endpoint->transport->handle].event = 0; | |
144 | + this->fds[endpoint->transport->handle].fs = -1; | |
145 | +} | |
146 | + | |
147 | +static | |
148 | +int | |
149 | +TR_commManagerDisableRead(void * _this, TR_Event event) | |
150 | +{ | |
151 | + TR_CommManagerPoll this = _this; | |
152 | + TR_CommEndPoint endpoint = (TR_CommEndPoint)event->subject; | |
153 | + | |
154 | + this->fds[endpoint->transport->handle].event &= ~POLLIN; | |
155 | +} | |
156 | + | |
157 | +static | |
158 | +int | |
159 | +TR_commManagerShutdownRead(void * _this, TR_Event event) | |
160 | +{ | |
161 | +} | |
162 | + | |
163 | +static | |
164 | +void | |
165 | +commManagerPollCvInit(TR_class_ptr cls) { | |
166 | + TR_INHERIT_CLASSVARS(TR_CommManagerPoll, TR_CommManager); | |
167 | +} | |
168 | + | |
169 | +TR_INIT_IFACE(TR_Class, commManagerPollCtor, commManagerPollDtor, NULL); | |
170 | +TR_INIT_IFACE( | |
171 | + TR_CommManager, | |
172 | + TR_commManagerPollAddEndpoint, | |
173 | + TR_commManagerPollSelect, | |
174 | + TR_commManagerPollEnableWrite, | |
175 | + TR_commManagerPollDisableWrite, | |
176 | + TR_commManagerPollClose, | |
177 | + TR_commManagerPollDisableRead, | |
178 | + TR_commManagerPollDisableWrite); | |
179 | +TR_CREATE_CLASS( | |
180 | + TR_CommManagerPoll, | |
181 | + TR_CommManager, | |
182 | + TR_CommManagerPollCvInit, | |
183 | + TR_IF(TR_Class)); | |
184 | + | |
185 | +// vim: set ts=4 sw=4: | ... | ... |
src/comm_manager_shutdown.c
0 → 100644
1 | +/** | |
2 | + * \file | |
3 | + * | |
4 | + * \author Georg Hopp | |
5 | + * | |
6 | + * \copyright | |
7 | + * Copyright © 2014 Georg Hopp | |
8 | + * | |
9 | + * This program is free software: you can redistribute it and/or modify | |
10 | + * it under the terms of the GNU General Public License as published by | |
11 | + * the Free Software Foundation, either version 3 of the License, or | |
12 | + * (at your option) any later version. | |
13 | + * | |
14 | + * This program is distributed in the hope that it will be useful, | |
15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | + * GNU General Public License for more details. | |
18 | + * | |
19 | + * You should have received a copy of the GNU General Public License | |
20 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
21 | + */ | |
22 | + | |
23 | +#include "trbase.h" | |
24 | +#include "trio.h" | |
25 | +#include "trevent.h" | |
26 | + | |
27 | +#include "tr/comm_manager.h" | |
28 | + | |
29 | +int | |
30 | +TR_commManagerShutdown(void * _this, TR_Event event) | |
31 | +{ | |
32 | + TR_CommManager this = _this; | |
33 | + nfds_t i; | |
34 | + | |
35 | + for (i=0; i<this->n_endpoints; i++) { | |
36 | + if (this->endpoints[handle]) { | |
37 | + TR_eventHandlerIssueEvent( | |
38 | + (TR_EventHandler)_this, | |
39 | + (TR_EventSubject)this->endpoints[handle], | |
40 | + TR_CEP_EVENT_CLOSE, | |
41 | + NULL); | |
42 | + } | |
43 | + } | |
44 | + | |
45 | + return 0; | |
46 | +} | |
47 | + | |
48 | +// vim: set ts=4 sw=4: | ... | ... |
src/i_comm_manager.c
0 → 100644
1 | +/** | |
2 | + * \file | |
3 | + * | |
4 | + * \author Georg Hopp | |
5 | + * | |
6 | + * \copyright | |
7 | + * Copyright © 2014 Georg Hopp | |
8 | + * | |
9 | + * This program is free software: you can redistribute it and/or modify | |
10 | + * it under the terms of the GNU General Public License as published by | |
11 | + * the Free Software Foundation, either version 3 of the License, or | |
12 | + * (at your option) any later version. | |
13 | + * | |
14 | + * This program is distributed in the hope that it will be useful, | |
15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | + * GNU General Public License for more details. | |
18 | + * | |
19 | + * You should have received a copy of the GNU General Public License | |
20 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
21 | + */ | |
22 | + | |
23 | +#include <errno.h> | |
24 | + | |
25 | +#include "trbase.h" | |
26 | +#include "trevent.h" | |
27 | + | |
28 | +#include "tr/interface/comm_manager.h" | |
29 | +#include "tr/comm_end_point.h" | |
30 | + | |
31 | +TR_CREATE_INTERFACE(TR_CommManager, 7); | |
32 | + | |
33 | +void | |
34 | +TR_commManagerAddEndpoint(void * _this, TR_CommEndPoint endpoint) | |
35 | +{ | |
36 | + TR_ConnManager this = _this; | |
37 | + | |
38 | + if (this->endpoints[endpoint->transport->handle]) { | |
39 | + // this should never happen, but if so we assume this is a leftover | |
40 | + // that still has to be deleted. | |
41 | + TR_delete(this->endpoints[endpoint->transport->handle]); | |
42 | + } | |
43 | + | |
44 | + this->endpoints[endpoint->transport->handle] = endpoint; | |
45 | + TR_CALL(_this, TR_CommManager, addEndpoint, endpoint); | |
46 | +} | |
47 | + | |
48 | +int | |
49 | +TR_commManagerSelect(void * _this, TR_Event event) | |
50 | +{ | |
51 | + int timeout; // milliseconds | |
52 | + int * timeoutptr = event->data; | |
53 | + TR_EventDispatcher dispatcher = (TR_EventDispatcher)event->subject; | |
54 | + | |
55 | + if (NULL == timeoutptr) { | |
56 | + timeout = TR_eventDispatcherGetDataWaitTime(dispatcher); | |
57 | + } else { | |
58 | + timeout = *timeoutptr; | |
59 | + TR_MEM_FREE(timeoutptr); | |
60 | + } | |
61 | + | |
62 | + TR_CALL(_this, TR_CommManager, select, event, timeout); | |
63 | + return 1; | |
64 | +} | |
65 | + | |
66 | +int | |
67 | +TR_commManagerEnableWrite(void * _this, TR_Event event) | |
68 | +{ | |
69 | + TR_CALL(_this, TR_CommManager, enableWrite, event); | |
70 | + return 1; | |
71 | +} | |
72 | + | |
73 | +int | |
74 | +TR_commManagerDisableWrite(void * _this, TR_Event event) | |
75 | +{ | |
76 | + TR_EventHandler this = _this; | |
77 | + TR_CommEndPoint endpoint = (TR_CommEndPoint)event->subject; | |
78 | + | |
79 | + TR_CALL(_this, TR_CommManager, disableWrite, event); | |
80 | + | |
81 | + if (TR_socketFinRd(endpoint->transport)) { | |
82 | + TR_eventHandlerIssueEvent( | |
83 | + this, | |
84 | + event->subject, | |
85 | + TR_CEP_EVENT_SHUT_READ, | |
86 | + NULL); | |
87 | + } | |
88 | + | |
89 | + return 1; | |
90 | +} | |
91 | + | |
92 | +int | |
93 | +TR_commManagerClose(void * _this, TR_Event event) | |
94 | +{ | |
95 | + TR_ConnManager this = _this; | |
96 | + TR_CommEndPoint endpoint = (TR_CommEndPoint)event->subject; | |
97 | + | |
98 | + TR_socketShutdown(endpoint->transport); | |
99 | + TR_CALL(_this, TR_CommManager, close, event); | |
100 | + TR_delete(this->endpoints[endpoint->transport->handle]); | |
101 | + | |
102 | + return 0; | |
103 | +} | |
104 | + | |
105 | +int | |
106 | +TR_commManagerShutdownRead(void * _this, TR_Event event) | |
107 | +{ | |
108 | + TR_CALL(_this, TR_CommManager, shutdownRead, event); | |
109 | + | |
110 | + if (TR_socketFinRd(((TR_CommEndPoint)event->subject)->transport)) { | |
111 | + // close | |
112 | + TR_eventHandlerIssueEvent( | |
113 | + (TR_EventHandler)_this, | |
114 | + event->subject, | |
115 | + TR_CEP_EVENT_CLOSE, | |
116 | + NULL); | |
117 | + } else if (TR_cepHasPendingData(endpoint)) { | |
118 | + // handle pending data... close is issued from disableWrite | |
119 | + TR_eventHandlerIssueEvent( | |
120 | + (TR_EventHandler)_this, | |
121 | + event->subject, | |
122 | + TR_CEP_EVENT_CLOSE, | |
123 | + NULL); | |
124 | + } else { | |
125 | + TR_cepSetClose(endpoint); | |
126 | + } | |
127 | + | |
128 | + return 0; | |
129 | +} | |
130 | + | |
131 | +int | |
132 | +TR_commManagerShutdownWrite(void * _this, TR_Event event) | |
133 | +{ | |
134 | + TR_CALL(_this, TR_CommManager, shutdownWrite, event); | |
135 | + | |
136 | + if (TR_socketFinRd(((TR_CommEndPoint)event->subject)->transport)) { | |
137 | + TR_eventHandlerIssueEvent( | |
138 | + (TR_EventHandler)_this, | |
139 | + event->subject, | |
140 | + TR_CEP_EVENT_CLOSE, | |
141 | + NULL); | |
142 | + } | |
143 | + | |
144 | + return 0; | |
145 | +} | |
146 | + | |
147 | + | |
148 | +// vim: set ts=4 sw=4: | ... | ... |
Please
register
or
login
to post a comment