| 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 |
| 11 | 11 |
| 12 #include <winsock2.h> | 12 #include <winsock2.h> |
| 13 #include <mswsock.h> | 13 #include <mswsock.h> |
| 14 | 14 |
| 15 #include "bin/builtin.h" | 15 #include "bin/builtin.h" |
| 16 #include "platform/thread.h" |
| 16 | 17 |
| 17 | 18 |
| 18 namespace dart { | 19 namespace dart { |
| 19 namespace bin { | 20 namespace bin { |
| 20 | 21 |
| 21 // Forward declarations. | 22 // Forward declarations. |
| 22 class EventHandlerImplementation; | 23 class EventHandlerImplementation; |
| 23 class Handle; | 24 class Handle; |
| 24 class FileHandle; | 25 class FileHandle; |
| 25 class SocketHandle; | 26 class SocketHandle; |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 uint8_t buffer_data_[1]; | 126 uint8_t buffer_data_[1]; |
| 126 }; | 127 }; |
| 127 | 128 |
| 128 | 129 |
| 129 // Abstract super class for holding information on listen and connected | 130 // Abstract super class for holding information on listen and connected |
| 130 // sockets. | 131 // sockets. |
| 131 class Handle { | 132 class Handle { |
| 132 public: | 133 public: |
| 133 enum Type { | 134 enum Type { |
| 134 kFile, | 135 kFile, |
| 136 kStd, |
| 135 kDirectoryWatch, | 137 kDirectoryWatch, |
| 136 kClientSocket, | 138 kClientSocket, |
| 137 kListenSocket | 139 kListenSocket |
| 138 }; | 140 }; |
| 139 | 141 |
| 140 class ScopedLock { | 142 class ScopedLock { |
| 141 public: | 143 public: |
| 142 explicit ScopedLock(Handle* handle) | 144 explicit ScopedLock(Handle* handle) |
| 143 : handle_(handle) { | 145 : handle_(handle) { |
| 144 handle_->Lock(); | 146 handle_->Lock(); |
| 145 } | 147 } |
| 146 ~ScopedLock() { | 148 ~ScopedLock() { |
| 147 handle_->Unlock(); | 149 handle_->Unlock(); |
| 148 } | 150 } |
| 149 | 151 |
| 150 private: | 152 private: |
| 151 Handle* handle_; | 153 Handle* handle_; |
| 152 }; | 154 }; |
| 153 | 155 |
| 154 virtual ~Handle(); | 156 virtual ~Handle(); |
| 155 | 157 |
| 156 // Socket interface exposing normal socket operations. | 158 // Socket interface exposing normal socket operations. |
| 157 int Available(); | 159 int Available(); |
| 158 int Read(void* buffer, int num_bytes); | 160 int Read(void* buffer, int num_bytes); |
| 159 int Write(const void* buffer, int num_bytes); | 161 virtual int Write(const void* buffer, int num_bytes); |
| 160 | 162 |
| 161 // Internal interface used by the event handler. | 163 // Internal interface used by the event handler. |
| 162 virtual bool IssueRead(); | 164 virtual bool IssueRead(); |
| 163 virtual bool IssueWrite(); | 165 virtual bool IssueWrite(); |
| 164 bool HasPendingRead(); | 166 bool HasPendingRead(); |
| 165 bool HasPendingWrite(); | 167 bool HasPendingWrite(); |
| 166 void ReadComplete(OverlappedBuffer* buffer); | 168 void ReadComplete(OverlappedBuffer* buffer); |
| 167 void WriteComplete(OverlappedBuffer* buffer); | 169 void WriteComplete(OverlappedBuffer* buffer); |
| 168 | 170 |
| 169 bool IsClosing() { return (flags_ & (1 << kClosing)) != 0; } | 171 bool IsClosing() { return (flags_ & (1 << kClosing)) != 0; } |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 intptr_t mask() { return mask_; } | 206 intptr_t mask() { return mask_; } |
| 205 | 207 |
| 206 void MarkDoesNotSupportOverlappedIO() { | 208 void MarkDoesNotSupportOverlappedIO() { |
| 207 flags_ |= (1 << kDoesNotSupportOverlappedIO); | 209 flags_ |= (1 << kDoesNotSupportOverlappedIO); |
| 208 } | 210 } |
| 209 bool SupportsOverlappedIO() { | 211 bool SupportsOverlappedIO() { |
| 210 return (flags_ & (1 << kDoesNotSupportOverlappedIO)) == 0; | 212 return (flags_ & (1 << kDoesNotSupportOverlappedIO)) == 0; |
| 211 } | 213 } |
| 212 | 214 |
| 213 void ReadSyncCompleteAsync(); | 215 void ReadSyncCompleteAsync(); |
| 214 void WriteSyncCompleteAsync(); | |
| 215 | 216 |
| 216 DWORD last_error() { return last_error_; } | 217 DWORD last_error() { return last_error_; } |
| 217 void set_last_error(DWORD last_error) { last_error_ = last_error; } | 218 void set_last_error(DWORD last_error) { last_error_ = last_error; } |
| 218 | 219 |
| 219 protected: | 220 protected: |
| 220 enum Flags { | 221 enum Flags { |
| 221 kClosing = 0, | 222 kClosing = 0, |
| 222 kCloseRead = 1, | 223 kCloseRead = 1, |
| 223 kCloseWrite = 2, | 224 kCloseWrite = 2, |
| 224 kDoesNotSupportOverlappedIO = 3, | 225 kDoesNotSupportOverlappedIO = 3, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 235 Dart_Port port_; // Dart port to communicate events for this socket. | 236 Dart_Port port_; // Dart port to communicate events for this socket. |
| 236 intptr_t mask_; // Mask of events to report through the port. | 237 intptr_t mask_; // Mask of events to report through the port. |
| 237 HANDLE completion_port_; | 238 HANDLE completion_port_; |
| 238 EventHandlerImplementation* event_handler_; | 239 EventHandlerImplementation* event_handler_; |
| 239 | 240 |
| 240 OverlappedBuffer* data_ready_; // Buffer for data ready to be read. | 241 OverlappedBuffer* data_ready_; // Buffer for data ready to be read. |
| 241 OverlappedBuffer* pending_read_; // Buffer for pending read. | 242 OverlappedBuffer* pending_read_; // Buffer for pending read. |
| 242 OverlappedBuffer* pending_write_; // Buffer for pending write | 243 OverlappedBuffer* pending_write_; // Buffer for pending write |
| 243 | 244 |
| 244 DWORD last_error_; | 245 DWORD last_error_; |
| 245 DWORD thread_wrote_; | |
| 246 | 246 |
| 247 private: | 247 private: |
| 248 int flags_; | 248 int flags_; |
| 249 CRITICAL_SECTION cs_; // Critical section protecting this object. | 249 CRITICAL_SECTION cs_; // Critical section protecting this object. |
| 250 }; | 250 }; |
| 251 | 251 |
| 252 | 252 |
| 253 class FileHandle : public Handle { | 253 class FileHandle : public Handle { |
| 254 public: | 254 public: |
| 255 explicit FileHandle(HANDLE handle) | 255 explicit FileHandle(HANDLE handle) |
| 256 : Handle(handle) { type_ = kFile; } | 256 : Handle(handle) { type_ = kFile; } |
| 257 FileHandle(HANDLE handle, Dart_Port port) | 257 FileHandle(HANDLE handle, Dart_Port port) |
| 258 : Handle(handle, port) { type_ = kFile; } | 258 : Handle(handle, port) { type_ = kFile; } |
| 259 | 259 |
| 260 virtual void EnsureInitialized(EventHandlerImplementation* event_handler); | 260 virtual void EnsureInitialized(EventHandlerImplementation* event_handler); |
| 261 virtual bool IsClosed(); | 261 virtual bool IsClosed(); |
| 262 virtual void DoClose(); | |
| 263 }; | 262 }; |
| 264 | 263 |
| 265 | 264 |
| 265 class StdHandle : public FileHandle { |
| 266 public: |
| 267 explicit StdHandle(HANDLE handle) |
| 268 : FileHandle(handle), |
| 269 thread_wrote_(0), |
| 270 write_thread_exists_(false), |
| 271 write_thread_running_(false), |
| 272 write_monitor_(new Monitor()) { |
| 273 type_ = kStd; |
| 274 } |
| 275 |
| 276 ~StdHandle() { |
| 277 delete write_monitor_; |
| 278 } |
| 279 |
| 280 virtual void DoClose(); |
| 281 virtual int Write(const void* buffer, int num_bytes); |
| 282 |
| 283 void WriteSyncCompleteAsync(); |
| 284 void RunWriteLoop(); |
| 285 |
| 286 private: |
| 287 DWORD thread_wrote_; |
| 288 bool write_thread_exists_; |
| 289 bool write_thread_running_; |
| 290 dart::Monitor* write_monitor_; |
| 291 }; |
| 292 |
| 293 |
| 266 class DirectoryWatchHandle : public Handle { | 294 class DirectoryWatchHandle : public Handle { |
| 267 public: | 295 public: |
| 268 DirectoryWatchHandle(HANDLE handle, int events, bool recursive) | 296 DirectoryWatchHandle(HANDLE handle, int events, bool recursive) |
| 269 : Handle(handle), | 297 : Handle(handle), |
| 270 events_(events), | 298 events_(events), |
| 271 recursive_(recursive) { | 299 recursive_(recursive) { |
| 272 type_ = kDirectoryWatch; | 300 type_ = kDirectoryWatch; |
| 273 } | 301 } |
| 274 | 302 |
| 275 virtual void EnsureInitialized(EventHandlerImplementation* event_handler); | 303 virtual void EnsureInitialized(EventHandlerImplementation* event_handler); |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 420 | 448 |
| 421 TimeoutQueue timeout_queue_; // Time for next timeout. | 449 TimeoutQueue timeout_queue_; // Time for next timeout. |
| 422 bool shutdown_; | 450 bool shutdown_; |
| 423 HANDLE completion_port_; | 451 HANDLE completion_port_; |
| 424 }; | 452 }; |
| 425 | 453 |
| 426 } // namespace bin | 454 } // namespace bin |
| 427 } // namespace dart | 455 } // namespace dart |
| 428 | 456 |
| 429 #endif // BIN_EVENTHANDLER_WIN_H_ | 457 #endif // BIN_EVENTHANDLER_WIN_H_ |
| OLD | NEW |