OLD | NEW |
---|---|
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #ifndef BIN_EVENTHANDLER_WIN_H_ | 5 #ifndef BIN_EVENTHANDLER_WIN_H_ |
6 #define BIN_EVENTHANDLER_WIN_H_ | 6 #define BIN_EVENTHANDLER_WIN_H_ |
7 | 7 |
8 #include <winsock2.h> | 8 #include <winsock2.h> |
9 #include <mswsock.h> | 9 #include <mswsock.h> |
10 | 10 |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
145 int Write(const void* buffer, int num_bytes); | 145 int Write(const void* buffer, int num_bytes); |
146 | 146 |
147 // Internal interface used by the event handler. | 147 // Internal interface used by the event handler. |
148 virtual bool IssueRead(); | 148 virtual bool IssueRead(); |
149 virtual bool IssueWrite(); | 149 virtual bool IssueWrite(); |
150 bool HasPendingRead(); | 150 bool HasPendingRead(); |
151 bool HasPendingWrite(); | 151 bool HasPendingWrite(); |
152 void ReadComplete(IOBuffer* buffer); | 152 void ReadComplete(IOBuffer* buffer); |
153 void WriteComplete(IOBuffer* buffer); | 153 void WriteComplete(IOBuffer* buffer); |
154 | 154 |
155 bool IsClosing() { return (flags_ & (1 << kClosing)) != 0; } | |
156 bool IsClosedRead() { return (flags_ & (1 << kCloseRead)) != 0; } | |
157 bool IsClosedWrite() { return (flags_ & (1 << kCloseWrite)) != 0; } | |
158 void MarkClosing() { flags_ |= (1 << kClosing); } | |
159 void MarkClosedRead() { flags_ |= (1 << kCloseRead); } | |
160 void MarkClosedWrite() { flags_ |= (1 << kCloseWrite); } | |
161 | |
155 virtual void EnsureInitialized( | 162 virtual void EnsureInitialized( |
156 EventHandlerImplementation* event_handler) = 0; | 163 EventHandlerImplementation* event_handler) = 0; |
157 | 164 |
158 HANDLE handle() { return handle_; } | 165 HANDLE handle() { return handle_; } |
159 Dart_Port port() { return port_; } | 166 Dart_Port port() { return port_; } |
160 EventHandlerImplementation* event_handler() { return event_handler_; } | 167 EventHandlerImplementation* event_handler() { return event_handler_; } |
161 | 168 |
162 void Lock(); | 169 void Lock(); |
163 void Unlock(); | 170 void Unlock(); |
164 | 171 |
165 bool CreateCompletionPort(HANDLE completion_port); | 172 bool CreateCompletionPort(HANDLE completion_port); |
166 | 173 |
167 void close(); | 174 void close(); |
168 virtual bool IsClosed() = 0; | 175 virtual bool IsClosed() = 0; |
169 | 176 |
170 void SetPortAndMask(Dart_Port port, intptr_t mask) { | 177 void SetPortAndMask(Dart_Port port, intptr_t mask) { |
171 port_ = port; | 178 port_ = port; |
172 mask_ = mask; | 179 mask_ = mask; |
173 } | 180 } |
174 Type type() { return type_; } | 181 Type type() { return type_; } |
175 bool is_file() { return type_ == kFile; } | 182 bool is_file() { return type_ == kFile; } |
176 bool is_socket() { return type_ == kListenSocket || type_ == kClientSocket; } | 183 bool is_socket() { return type_ == kListenSocket || type_ == kClientSocket; } |
177 bool is_listen_socket() { return type_ == kListenSocket; } | 184 bool is_listen_socket() { return type_ == kListenSocket; } |
178 bool is_client_socket() { return type_ == kClientSocket; } | 185 bool is_client_socket() { return type_ == kClientSocket; } |
179 intptr_t mask() { return mask_; } | 186 intptr_t mask() { return mask_; } |
180 | 187 |
181 bool is_closing() { return closing_; } | 188 protected: |
189 enum Flags { | |
190 kClosing = 0, | |
191 kCloseRead = 1, | |
192 kCloseWrite = 2 | |
193 }; | |
182 | 194 |
183 protected: | |
184 explicit Handle(HANDLE handle); | 195 explicit Handle(HANDLE handle); |
185 Handle(HANDLE handle, Dart_Port port); | 196 Handle(HANDLE handle, Dart_Port port); |
186 | 197 |
187 virtual void AfterClose() = 0; | 198 virtual void AfterClose() = 0; |
188 | 199 |
189 Type type_; | 200 Type type_; |
190 HANDLE handle_; | 201 HANDLE handle_; |
191 bool closing_; // Is this handle in the process of closing? | 202 int flags_; |
Mads Ager (google)
2011/11/08 09:57:08
Should parts of this be private now? I guess flags
Søren Gjesse
2011/11/08 12:56:50
Done.
| |
192 Dart_Port port_; // Dart port to communicate events for this socket. | 203 Dart_Port port_; // Dart port to communicate events for this socket. |
193 intptr_t mask_; // Mask of events to report through the port. | 204 intptr_t mask_; // Mask of events to report through the port. |
194 HANDLE completion_port_; | 205 HANDLE completion_port_; |
195 CRITICAL_SECTION cs_; // Critical section protecting this object. | 206 CRITICAL_SECTION cs_; // Critical section protecting this object. |
196 EventHandlerImplementation* event_handler_; | 207 EventHandlerImplementation* event_handler_; |
197 | 208 |
198 IOBuffer* data_ready_; // IO buffer for data ready to be read. | 209 IOBuffer* data_ready_; // IO buffer for data ready to be read. |
199 IOBuffer* pending_read_; // IO buffer for pending read. | 210 IOBuffer* pending_read_; // IO buffer for pending read. |
200 IOBuffer* pending_write_; // IO buffer for pending write | 211 IOBuffer* pending_write_; // IO buffer for pending write |
201 }; | 212 }; |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
268 ClientSocket* accepted_head_; | 279 ClientSocket* accepted_head_; |
269 ClientSocket* accepted_tail_; | 280 ClientSocket* accepted_tail_; |
270 }; | 281 }; |
271 | 282 |
272 | 283 |
273 // Information on connected sockets. | 284 // Information on connected sockets. |
274 class ClientSocket : public SocketHandle { | 285 class ClientSocket : public SocketHandle { |
275 public: | 286 public: |
276 explicit ClientSocket(SOCKET s) | 287 explicit ClientSocket(SOCKET s) |
277 : SocketHandle(s), | 288 : SocketHandle(s), |
278 next_(NULL), | 289 next_(NULL) { type_ = kClientSocket; } |
Mads Ager (google)
2011/11/08 09:57:08
Doesn't
explicit ClientSocket(SOCKET s) : Socke
Søren Gjesse
2011/11/08 12:56:50
Done.
| |
279 flags_(0) { type_ = kClientSocket; } | |
280 | 290 |
281 ClientSocket(SOCKET s, Dart_Port port) | 291 ClientSocket(SOCKET s, Dart_Port port) |
282 : SocketHandle(s, port), | 292 : SocketHandle(s, port), |
283 next_(NULL), | 293 next_(NULL) { type_ = kClientSocket; } |
Mads Ager (google)
2011/11/08 09:57:08
Ditto on the indentation.
Søren Gjesse
2011/11/08 12:56:50
Done.
| |
284 flags_(0) { type_ = kClientSocket; } | |
285 | 294 |
286 virtual ~ClientSocket() { | 295 virtual ~ClientSocket() { |
287 // Don't delete this object until all pending requests have been handled. | 296 // Don't delete this object until all pending requests have been handled. |
288 ASSERT(!HasPendingRead()); | 297 ASSERT(!HasPendingRead()); |
289 ASSERT(!HasPendingWrite()); | 298 ASSERT(!HasPendingWrite()); |
290 ASSERT(next_ == NULL); | 299 ASSERT(next_ == NULL); |
291 }; | 300 }; |
292 | 301 |
293 void Shutdown(int how); | 302 void Shutdown(int how); |
294 bool IsClosedRead() { return (flags_ & (1 << kCloseRead)) != 0; } | |
295 bool IsClosedWrite() { return (flags_ & (1 << kCloseWrite)) != 0; } | |
296 | |
297 void MarkClosedRead() { flags_ |= (1 << kCloseRead); } | |
298 void MarkClosedWrite() { flags_ |= (1 << kCloseWrite); } | |
299 | 303 |
300 // Internal interface used by the event handler. | 304 // Internal interface used by the event handler. |
301 virtual bool IssueRead(); | 305 virtual bool IssueRead(); |
302 virtual bool IssueWrite(); | 306 virtual bool IssueWrite(); |
303 | 307 |
304 virtual void EnsureInitialized( | 308 virtual void EnsureInitialized( |
305 EventHandlerImplementation* event_handler); | 309 EventHandlerImplementation* event_handler); |
306 virtual bool IsClosed(); | 310 virtual bool IsClosed(); |
307 | 311 |
308 ClientSocket* next() { return next_; } | 312 ClientSocket* next() { return next_; } |
309 void set_next(ClientSocket* next) { next_ = next; } | 313 void set_next(ClientSocket* next) { next_ = next; } |
310 | 314 |
311 private: | 315 private: |
312 enum Flags { | |
313 kCloseRead = 0, | |
314 kCloseWrite = 1 | |
315 }; | |
316 | |
317 virtual void AfterClose(); | 316 virtual void AfterClose(); |
318 | 317 |
319 ClientSocket* next_; | 318 ClientSocket* next_; |
320 int flags_; | |
321 }; | 319 }; |
322 | 320 |
323 | 321 |
324 // Event handler. | 322 // Event handler. |
325 class EventHandlerImplementation { | 323 class EventHandlerImplementation { |
326 public: | 324 public: |
327 EventHandlerImplementation(); | 325 EventHandlerImplementation(); |
328 virtual ~EventHandlerImplementation() {} | 326 virtual ~EventHandlerImplementation() {} |
329 | 327 |
330 void SendData(intptr_t id, Dart_Port dart_port, intptr_t data); | 328 void SendData(intptr_t id, Dart_Port dart_port, intptr_t data); |
331 void StartEventHandler(); | 329 void StartEventHandler(); |
332 | 330 |
333 DWORD GetTimeout(); | 331 DWORD GetTimeout(); |
334 void HandleInterrupt(InterruptMessage* msg); | 332 void HandleInterrupt(InterruptMessage* msg); |
335 void HandleTimeout(); | 333 void HandleTimeout(); |
336 void HandleAccept(ListenSocket* listen_socket, IOBuffer* buffer); | 334 void HandleAccept(ListenSocket* listen_socket, IOBuffer* buffer); |
337 void HandleClosed(Handle* handle); | 335 void HandleClosed(Handle* handle); |
338 void HandleRead(ClientSocket* client_socket, int bytes, IOBuffer* buffer); | 336 void HandleRead(Handle* handle, int bytes, IOBuffer* buffer); |
339 void HandleWrite(ClientSocket* client_socket, int bytes, IOBuffer* buffer); | 337 void HandleWrite(Handle* handle, int bytes, IOBuffer* buffer); |
340 void HandleClose(ClientSocket* client_socket); | 338 void HandleClose(ClientSocket* client_socket); |
341 void HandleIOCompletion(DWORD bytes, ULONG_PTR key, OVERLAPPED* overlapped); | 339 void HandleIOCompletion(DWORD bytes, ULONG_PTR key, OVERLAPPED* overlapped); |
342 | 340 |
343 HANDLE completion_port() { return completion_port_; } | 341 HANDLE completion_port() { return completion_port_; } |
344 | 342 |
345 private: | 343 private: |
346 ClientSocket* client_sockets_head_; | 344 ClientSocket* client_sockets_head_; |
347 | 345 |
348 int64_t timeout_; // Time for next timeout. | 346 int64_t timeout_; // Time for next timeout. |
349 Dart_Port timeout_port_; | 347 Dart_Port timeout_port_; |
350 HANDLE completion_port_; | 348 HANDLE completion_port_; |
351 }; | 349 }; |
352 | 350 |
353 | 351 |
354 #endif // BIN_EVENTHANDLER_WIN_H_ | 352 #endif // BIN_EVENTHANDLER_WIN_H_ |
OLD | NEW |