| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 #if !defined(BIN_EVENTHANDLER_H_) | 8 #if !defined(BIN_EVENTHANDLER_H_) |
| 9 #error Do not include eventhandler_win.h directly; use eventhandler.h instead. | 9 #error Do not include eventhandler_win.h directly; use eventhandler.h instead. |
| 10 #endif | 10 #endif |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 class ListenSocket; | 27 class ListenSocket; |
| 28 | 28 |
| 29 | 29 |
| 30 struct InterruptMessage { | 30 struct InterruptMessage { |
| 31 intptr_t id; | 31 intptr_t id; |
| 32 Dart_Port dart_port; | 32 Dart_Port dart_port; |
| 33 int64_t data; | 33 int64_t data; |
| 34 }; | 34 }; |
| 35 | 35 |
| 36 | 36 |
| 37 // An IOBuffer encapsulates the OVERLAPPED structure and the | 37 // An OverlappedBuffer encapsulates the OVERLAPPED structure and the |
| 38 // associated data buffer. For accept it also contains the pre-created | 38 // associated data buffer. For accept it also contains the pre-created |
| 39 // socket for the client. | 39 // socket for the client. |
| 40 class IOBuffer { | 40 class OverlappedBuffer { |
| 41 public: | 41 public: |
| 42 enum Operation { kAccept, kRead, kWrite, kDisconnect }; | 42 enum Operation { kAccept, kRead, kWrite, kDisconnect }; |
| 43 | 43 |
| 44 static IOBuffer* AllocateAcceptBuffer(int buffer_size); | 44 static OverlappedBuffer* AllocateAcceptBuffer(int buffer_size); |
| 45 static IOBuffer* AllocateReadBuffer(int buffer_size); | 45 static OverlappedBuffer* AllocateReadBuffer(int buffer_size); |
| 46 static IOBuffer* AllocateWriteBuffer(int buffer_size); | 46 static OverlappedBuffer* AllocateWriteBuffer(int buffer_size); |
| 47 static IOBuffer* AllocateDisconnectBuffer(); | 47 static OverlappedBuffer* AllocateDisconnectBuffer(); |
| 48 static void DisposeBuffer(IOBuffer* buffer); | 48 static void DisposeBuffer(OverlappedBuffer* buffer); |
| 49 | 49 |
| 50 // Find the IO buffer from the OVERLAPPED address. | 50 // Find the IO buffer from the OVERLAPPED address. |
| 51 static IOBuffer* GetFromOverlapped(OVERLAPPED* overlapped); | 51 static OverlappedBuffer* GetFromOverlapped(OVERLAPPED* overlapped); |
| 52 | 52 |
| 53 // Read data from a buffer which has been received. It will read up | 53 // Read data from a buffer which has been received. It will read up |
| 54 // to num_bytes bytes of data returning the actual number of bytes | 54 // to num_bytes bytes of data returning the actual number of bytes |
| 55 // read. This will update the index of the next byte in the buffer | 55 // read. This will update the index of the next byte in the buffer |
| 56 // so calling Read several times will keep returning new data from | 56 // so calling Read several times will keep returning new data from |
| 57 // the buffer until all data have been read. | 57 // the buffer until all data have been read. |
| 58 int Read(void* buffer, int num_bytes); | 58 int Read(void* buffer, int num_bytes); |
| 59 | 59 |
| 60 // Write data to a buffer before sending it. Returns the number of bytes | 60 // Write data to a buffer before sending it. Returns the number of bytes |
| 61 // actually written to the buffer. Calls to Write will always write to | 61 // actually written to the buffer. Calls to Write will always write to |
| (...skipping 19 matching lines...) Expand all Loading... |
| 81 // Returns a WASBUF structure initialized with the data in this IO buffer. | 81 // Returns a WASBUF structure initialized with the data in this IO buffer. |
| 82 WSABUF* GetWASBUF() { | 82 WSABUF* GetWASBUF() { |
| 83 wbuf_.buf = GetBufferStart(); | 83 wbuf_.buf = GetBufferStart(); |
| 84 wbuf_.len = GetBufferSize(); | 84 wbuf_.len = GetBufferSize(); |
| 85 return &wbuf_; | 85 return &wbuf_; |
| 86 }; | 86 }; |
| 87 | 87 |
| 88 void set_data_length(int data_length) { data_length_ = data_length; } | 88 void set_data_length(int data_length) { data_length_ = data_length; } |
| 89 | 89 |
| 90 private: | 90 private: |
| 91 IOBuffer(int buffer_size, Operation operation) | 91 OverlappedBuffer(int buffer_size, Operation operation) |
| 92 : operation_(operation), buflen_(buffer_size) { | 92 : operation_(operation), buflen_(buffer_size) { |
| 93 memset(GetBufferStart(), 0, GetBufferSize()); | 93 memset(GetBufferStart(), 0, GetBufferSize()); |
| 94 index_ = 0; | 94 index_ = 0; |
| 95 data_length_ = 0; | 95 data_length_ = 0; |
| 96 if (operation_ == kAccept) { | 96 if (operation_ == kAccept) { |
| 97 client_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); | 97 client_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 98 } | 98 } |
| 99 } | 99 } |
| 100 | 100 |
| 101 void* operator new(size_t size, int buffer_size) { | 101 void* operator new(size_t size, int buffer_size) { |
| 102 return malloc(size + buffer_size); | 102 return malloc(size + buffer_size); |
| 103 } | 103 } |
| 104 | 104 |
| 105 void operator delete(void* buffer) { | 105 void operator delete(void* buffer) { |
| 106 free(buffer); | 106 free(buffer); |
| 107 } | 107 } |
| 108 | 108 |
| 109 static IOBuffer* AllocateBuffer(int buffer_size, Operation operation); | 109 static OverlappedBuffer* AllocateBuffer(int buffer_size, |
| 110 Operation operation); |
| 110 | 111 |
| 111 OVERLAPPED overlapped_; // OVERLAPPED structure for overlapped IO. | 112 OVERLAPPED overlapped_; // OVERLAPPED structure for overlapped IO. |
| 112 SOCKET client_; // Used for AcceptEx client socket. | 113 SOCKET client_; // Used for AcceptEx client socket. |
| 113 int buflen_; // Length of the buffer. | 114 int buflen_; // Length of the buffer. |
| 114 Operation operation_; // Type of operation issued. | 115 Operation operation_; // Type of operation issued. |
| 115 | 116 |
| 116 int index_; // Index for next read from read buffer. | 117 int index_; // Index for next read from read buffer. |
| 117 int data_length_; // Length of the actual data in the buffer. | 118 int data_length_; // Length of the actual data in the buffer. |
| 118 | 119 |
| 119 WSABUF wbuf_; // Structure for passing buffer to WSA functions. | 120 WSABUF wbuf_; // Structure for passing buffer to WSA functions. |
| (...skipping 30 matching lines...) Expand all Loading... |
| 150 // Socket interface exposing normal socket operations. | 151 // Socket interface exposing normal socket operations. |
| 151 int Available(); | 152 int Available(); |
| 152 int Read(void* buffer, int num_bytes); | 153 int Read(void* buffer, int num_bytes); |
| 153 int Write(const void* buffer, int num_bytes); | 154 int Write(const void* buffer, int num_bytes); |
| 154 | 155 |
| 155 // Internal interface used by the event handler. | 156 // Internal interface used by the event handler. |
| 156 virtual bool IssueRead(); | 157 virtual bool IssueRead(); |
| 157 virtual bool IssueWrite(); | 158 virtual bool IssueWrite(); |
| 158 bool HasPendingRead(); | 159 bool HasPendingRead(); |
| 159 bool HasPendingWrite(); | 160 bool HasPendingWrite(); |
| 160 void ReadComplete(IOBuffer* buffer); | 161 void ReadComplete(OverlappedBuffer* buffer); |
| 161 void WriteComplete(IOBuffer* buffer); | 162 void WriteComplete(OverlappedBuffer* buffer); |
| 162 | 163 |
| 163 bool IsClosing() { return (flags_ & (1 << kClosing)) != 0; } | 164 bool IsClosing() { return (flags_ & (1 << kClosing)) != 0; } |
| 164 bool IsClosedRead() { return (flags_ & (1 << kCloseRead)) != 0; } | 165 bool IsClosedRead() { return (flags_ & (1 << kCloseRead)) != 0; } |
| 165 bool IsClosedWrite() { return (flags_ & (1 << kCloseWrite)) != 0; } | 166 bool IsClosedWrite() { return (flags_ & (1 << kCloseWrite)) != 0; } |
| 166 bool IsError() { return (flags_ & (1 << kError)) != 0; } | 167 bool IsError() { return (flags_ & (1 << kError)) != 0; } |
| 167 void MarkClosing() { flags_ |= (1 << kClosing); } | 168 void MarkClosing() { flags_ |= (1 << kClosing); } |
| 168 void MarkClosedRead() { flags_ |= (1 << kCloseRead); } | 169 void MarkClosedRead() { flags_ |= (1 << kCloseRead); } |
| 169 void MarkClosedWrite() { flags_ |= (1 << kCloseWrite); } | 170 void MarkClosedWrite() { flags_ |= (1 << kCloseWrite); } |
| 170 void MarkError() { flags_ |= (1 << kError); } | 171 void MarkError() { flags_ |= (1 << kError); } |
| 171 | 172 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 | 224 |
| 224 virtual void HandleIssueError(); | 225 virtual void HandleIssueError(); |
| 225 | 226 |
| 226 Type type_; | 227 Type type_; |
| 227 HANDLE handle_; | 228 HANDLE handle_; |
| 228 Dart_Port port_; // Dart port to communicate events for this socket. | 229 Dart_Port port_; // Dart port to communicate events for this socket. |
| 229 intptr_t mask_; // Mask of events to report through the port. | 230 intptr_t mask_; // Mask of events to report through the port. |
| 230 HANDLE completion_port_; | 231 HANDLE completion_port_; |
| 231 EventHandlerImplementation* event_handler_; | 232 EventHandlerImplementation* event_handler_; |
| 232 | 233 |
| 233 IOBuffer* data_ready_; // IO buffer for data ready to be read. | 234 OverlappedBuffer* data_ready_; // Buffer for data ready to be read. |
| 234 IOBuffer* pending_read_; // IO buffer for pending read. | 235 OverlappedBuffer* pending_read_; // Buffer for pending read. |
| 235 IOBuffer* pending_write_; // IO buffer for pending write | 236 OverlappedBuffer* pending_write_; // Buffer for pending write |
| 236 | 237 |
| 237 DWORD last_error_; | 238 DWORD last_error_; |
| 238 | 239 |
| 239 private: | 240 private: |
| 240 int flags_; | 241 int flags_; |
| 241 CRITICAL_SECTION cs_; // Critical section protecting this object. | 242 CRITICAL_SECTION cs_; // Critical section protecting this object. |
| 242 }; | 243 }; |
| 243 | 244 |
| 244 | 245 |
| 245 class FileHandle : public Handle { | 246 class FileHandle : public Handle { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 ASSERT(accepted_tail_ == NULL); | 285 ASSERT(accepted_tail_ == NULL); |
| 285 }; | 286 }; |
| 286 | 287 |
| 287 // Socket interface exposing normal socket operations. | 288 // Socket interface exposing normal socket operations. |
| 288 ClientSocket* Accept(); | 289 ClientSocket* Accept(); |
| 289 bool CanAccept(); | 290 bool CanAccept(); |
| 290 | 291 |
| 291 // Internal interface used by the event handler. | 292 // Internal interface used by the event handler. |
| 292 bool HasPendingAccept() { return pending_accept_count_ > 0; } | 293 bool HasPendingAccept() { return pending_accept_count_ > 0; } |
| 293 bool IssueAccept(); | 294 bool IssueAccept(); |
| 294 void AcceptComplete(IOBuffer* buffer, HANDLE completion_port); | 295 void AcceptComplete(OverlappedBuffer* buffer, HANDLE completion_port); |
| 295 | 296 |
| 296 virtual void EnsureInitialized( | 297 virtual void EnsureInitialized( |
| 297 EventHandlerImplementation* event_handler); | 298 EventHandlerImplementation* event_handler); |
| 298 virtual void DoClose(); | 299 virtual void DoClose(); |
| 299 virtual bool IsClosed(); | 300 virtual bool IsClosed(); |
| 300 | 301 |
| 301 int pending_accept_count() { return pending_accept_count_; } | 302 int pending_accept_count() { return pending_accept_count_; } |
| 302 | 303 |
| 303 private: | 304 private: |
| 304 bool LoadAcceptEx(); | 305 bool LoadAcceptEx(); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 335 ASSERT(!HasPendingWrite()); | 336 ASSERT(!HasPendingWrite()); |
| 336 ASSERT(next_ == NULL); | 337 ASSERT(next_ == NULL); |
| 337 }; | 338 }; |
| 338 | 339 |
| 339 void Shutdown(int how); | 340 void Shutdown(int how); |
| 340 | 341 |
| 341 // Internal interface used by the event handler. | 342 // Internal interface used by the event handler. |
| 342 virtual bool IssueRead(); | 343 virtual bool IssueRead(); |
| 343 virtual bool IssueWrite(); | 344 virtual bool IssueWrite(); |
| 344 void IssueDisconnect(); | 345 void IssueDisconnect(); |
| 345 void DisconnectComplete(IOBuffer* buffer); | 346 void DisconnectComplete(OverlappedBuffer* buffer); |
| 346 | 347 |
| 347 virtual void EnsureInitialized( | 348 virtual void EnsureInitialized( |
| 348 EventHandlerImplementation* event_handler); | 349 EventHandlerImplementation* event_handler); |
| 349 virtual void DoClose(); | 350 virtual void DoClose(); |
| 350 virtual bool IsClosed(); | 351 virtual bool IsClosed(); |
| 351 | 352 |
| 352 ClientSocket* next() { return next_; } | 353 ClientSocket* next() { return next_; } |
| 353 void set_next(ClientSocket* next) { next_ = next; } | 354 void set_next(ClientSocket* next) { next_ = next; } |
| 354 | 355 |
| 355 private: | 356 private: |
| (...skipping 12 matching lines...) Expand all Loading... |
| 368 | 369 |
| 369 void SendData(intptr_t id, Dart_Port dart_port, int64_t data); | 370 void SendData(intptr_t id, Dart_Port dart_port, int64_t data); |
| 370 void Start(EventHandler* handler); | 371 void Start(EventHandler* handler); |
| 371 void Shutdown(); | 372 void Shutdown(); |
| 372 | 373 |
| 373 static void EventHandlerEntry(uword args); | 374 static void EventHandlerEntry(uword args); |
| 374 | 375 |
| 375 int64_t GetTimeout(); | 376 int64_t GetTimeout(); |
| 376 void HandleInterrupt(InterruptMessage* msg); | 377 void HandleInterrupt(InterruptMessage* msg); |
| 377 void HandleTimeout(); | 378 void HandleTimeout(); |
| 378 void HandleAccept(ListenSocket* listen_socket, IOBuffer* buffer); | 379 void HandleAccept(ListenSocket* listen_socket, OverlappedBuffer* buffer); |
| 379 void HandleClosed(Handle* handle); | 380 void HandleClosed(Handle* handle); |
| 380 void HandleError(Handle* handle); | 381 void HandleError(Handle* handle); |
| 381 void HandleRead(Handle* handle, int bytes, IOBuffer* buffer); | 382 void HandleRead(Handle* handle, int bytes, OverlappedBuffer* buffer); |
| 382 void HandleWrite(Handle* handle, int bytes, IOBuffer* buffer); | 383 void HandleWrite(Handle* handle, int bytes, OverlappedBuffer* buffer); |
| 383 void HandleDisconnect(ClientSocket* client_socket, | 384 void HandleDisconnect(ClientSocket* client_socket, |
| 384 int bytes, | 385 int bytes, |
| 385 IOBuffer* buffer); | 386 OverlappedBuffer* buffer); |
| 386 void HandleIOCompletion(DWORD bytes, ULONG_PTR key, OVERLAPPED* overlapped); | 387 void HandleIOCompletion(DWORD bytes, ULONG_PTR key, OVERLAPPED* overlapped); |
| 387 | 388 |
| 388 HANDLE completion_port() { return completion_port_; } | 389 HANDLE completion_port() { return completion_port_; } |
| 389 | 390 |
| 390 private: | 391 private: |
| 391 ClientSocket* client_sockets_head_; | 392 ClientSocket* client_sockets_head_; |
| 392 | 393 |
| 393 TimeoutQueue timeout_queue_; // Time for next timeout. | 394 TimeoutQueue timeout_queue_; // Time for next timeout. |
| 394 bool shutdown_; | 395 bool shutdown_; |
| 395 HANDLE completion_port_; | 396 HANDLE completion_port_; |
| 396 }; | 397 }; |
| 397 | 398 |
| 398 } // namespace bin | 399 } // namespace bin |
| 399 } // namespace dart | 400 } // namespace dart |
| 400 | 401 |
| 401 #endif // BIN_EVENTHANDLER_WIN_H_ | 402 #endif // BIN_EVENTHANDLER_WIN_H_ |
| OLD | NEW |