| 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 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 public: | 158 public: |
| 159 enum Type { | 159 enum Type { |
| 160 kFile, | 160 kFile, |
| 161 kStd, | 161 kStd, |
| 162 kDirectoryWatch, | 162 kDirectoryWatch, |
| 163 kClientSocket, | 163 kClientSocket, |
| 164 kListenSocket, | 164 kListenSocket, |
| 165 kDatagramSocket | 165 kDatagramSocket |
| 166 }; | 166 }; |
| 167 | 167 |
| 168 class ScopedLock { | |
| 169 public: | |
| 170 explicit ScopedLock(Handle* handle) | |
| 171 : handle_(handle) { | |
| 172 handle_->Lock(); | |
| 173 } | |
| 174 ~ScopedLock() { | |
| 175 handle_->Unlock(); | |
| 176 } | |
| 177 | |
| 178 private: | |
| 179 Handle* handle_; | |
| 180 }; | |
| 181 | |
| 182 virtual ~Handle(); | 168 virtual ~Handle(); |
| 183 | 169 |
| 184 // Socket interface exposing normal socket operations. | 170 // Socket interface exposing normal socket operations. |
| 185 intptr_t Available(); | 171 intptr_t Available(); |
| 186 intptr_t Read(void* buffer, intptr_t num_bytes); | 172 intptr_t Read(void* buffer, intptr_t num_bytes); |
| 187 intptr_t RecvFrom(void* buffer, | 173 intptr_t RecvFrom(void* buffer, |
| 188 intptr_t num_bytes, | 174 intptr_t num_bytes, |
| 189 struct sockaddr* sa, | 175 struct sockaddr* sa, |
| 190 socklen_t addr_len); | 176 socklen_t addr_len); |
| 191 virtual intptr_t Write(const void* buffer, intptr_t num_bytes); | 177 virtual intptr_t Write(const void* buffer, intptr_t num_bytes); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 212 void MarkClosing() { flags_ |= (1 << kClosing); } | 198 void MarkClosing() { flags_ |= (1 << kClosing); } |
| 213 void MarkClosedRead() { flags_ |= (1 << kCloseRead); } | 199 void MarkClosedRead() { flags_ |= (1 << kCloseRead); } |
| 214 void MarkClosedWrite() { flags_ |= (1 << kCloseWrite); } | 200 void MarkClosedWrite() { flags_ |= (1 << kCloseWrite); } |
| 215 void MarkError() { flags_ |= (1 << kError); } | 201 void MarkError() { flags_ |= (1 << kError); } |
| 216 | 202 |
| 217 virtual void EnsureInitialized( | 203 virtual void EnsureInitialized( |
| 218 EventHandlerImplementation* event_handler) = 0; | 204 EventHandlerImplementation* event_handler) = 0; |
| 219 | 205 |
| 220 HANDLE handle() { return handle_; } | 206 HANDLE handle() { return handle_; } |
| 221 | 207 |
| 222 void Lock(); | |
| 223 void Unlock(); | |
| 224 | |
| 225 bool CreateCompletionPort(HANDLE completion_port); | 208 bool CreateCompletionPort(HANDLE completion_port); |
| 226 | 209 |
| 227 void Close(); | 210 void Close(); |
| 228 virtual void DoClose(); | 211 virtual void DoClose(); |
| 229 virtual bool IsClosed() = 0; | 212 virtual bool IsClosed() = 0; |
| 230 | 213 |
| 231 bool IsHandleClosed() const { return handle_ == INVALID_HANDLE_VALUE; } | 214 bool IsHandleClosed() const { return handle_ == INVALID_HANDLE_VALUE; } |
| 232 | 215 |
| 233 Type type() { return type_; } | 216 Type type() { return type_; } |
| 234 bool is_file() { return type_ == kFile; } | 217 bool is_file() { return type_ == kFile; } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 245 bool SupportsOverlappedIO() { | 228 bool SupportsOverlappedIO() { |
| 246 return (flags_ & (1 << kDoesNotSupportOverlappedIO)) == 0; | 229 return (flags_ & (1 << kDoesNotSupportOverlappedIO)) == 0; |
| 247 } | 230 } |
| 248 | 231 |
| 249 void ReadSyncCompleteAsync(); | 232 void ReadSyncCompleteAsync(); |
| 250 | 233 |
| 251 DWORD last_error() { return last_error_; } | 234 DWORD last_error() { return last_error_; } |
| 252 void set_last_error(DWORD last_error) { last_error_ = last_error; } | 235 void set_last_error(DWORD last_error) { last_error_ = last_error; } |
| 253 | 236 |
| 254 protected: | 237 protected: |
| 238 // For access to monitor_; |
| 239 friend class EventHandlerImplementation; |
| 240 |
| 255 enum Flags { | 241 enum Flags { |
| 256 kClosing = 0, | 242 kClosing = 0, |
| 257 kCloseRead = 1, | 243 kCloseRead = 1, |
| 258 kCloseWrite = 2, | 244 kCloseWrite = 2, |
| 259 kDoesNotSupportOverlappedIO = 3, | 245 kDoesNotSupportOverlappedIO = 3, |
| 260 kError = 4 | 246 kError = 4 |
| 261 }; | 247 }; |
| 262 | 248 |
| 263 explicit Handle(intptr_t handle); | 249 explicit Handle(intptr_t handle); |
| 264 | 250 |
| 265 virtual void HandleIssueError(); | 251 virtual void HandleIssueError(); |
| 266 | 252 |
| 267 Type type_; | 253 Type type_; |
| 268 HANDLE handle_; | 254 HANDLE handle_; |
| 269 HANDLE completion_port_; | 255 HANDLE completion_port_; |
| 270 EventHandlerImplementation* event_handler_; | 256 EventHandlerImplementation* event_handler_; |
| 271 | 257 |
| 272 OverlappedBuffer* data_ready_; // Buffer for data ready to be read. | 258 OverlappedBuffer* data_ready_; // Buffer for data ready to be read. |
| 273 OverlappedBuffer* pending_read_; // Buffer for pending read. | 259 OverlappedBuffer* pending_read_; // Buffer for pending read. |
| 274 OverlappedBuffer* pending_write_; // Buffer for pending write | 260 OverlappedBuffer* pending_write_; // Buffer for pending write |
| 275 | 261 |
| 276 DWORD last_error_; | 262 DWORD last_error_; |
| 277 | 263 |
| 264 ThreadId read_thread_id_; |
| 265 bool read_thread_starting_; |
| 266 bool read_thread_finished_; |
| 267 Monitor* monitor_; |
| 268 |
| 278 private: | 269 private: |
| 270 void WaitForReadThreadStarted(); |
| 271 void NotifyReadThreadStarted(); |
| 272 void WaitForReadThreadFinished(); |
| 273 void NotifyReadThreadFinished(); |
| 274 |
| 279 int flags_; | 275 int flags_; |
| 280 CRITICAL_SECTION cs_; // Critical section protecting this object. | |
| 281 }; | 276 }; |
| 282 | 277 |
| 283 | 278 |
| 284 class FileHandle : public DescriptorInfoSingleMixin<Handle> { | 279 class FileHandle : public DescriptorInfoSingleMixin<Handle> { |
| 285 public: | 280 public: |
| 286 explicit FileHandle(HANDLE handle) | 281 explicit FileHandle(HANDLE handle) |
| 287 : DescriptorInfoSingleMixin(reinterpret_cast<intptr_t>(handle), true) { | 282 : DescriptorInfoSingleMixin(reinterpret_cast<intptr_t>(handle), true) { |
| 288 type_ = kFile; | 283 type_ = kFile; |
| 289 } | 284 } |
| 290 | 285 |
| 291 virtual void EnsureInitialized(EventHandlerImplementation* event_handler); | 286 virtual void EnsureInitialized(EventHandlerImplementation* event_handler); |
| 292 virtual bool IsClosed(); | 287 virtual bool IsClosed(); |
| 293 }; | 288 }; |
| 294 | 289 |
| 295 | 290 |
| 296 class StdHandle : public FileHandle { | 291 class StdHandle : public FileHandle { |
| 297 public: | 292 public: |
| 298 explicit StdHandle(HANDLE handle) | 293 explicit StdHandle(HANDLE handle) |
| 299 : FileHandle(handle), | 294 : FileHandle(handle), |
| 295 thread_id_(Thread::kInvalidThreadId), |
| 300 thread_wrote_(0), | 296 thread_wrote_(0), |
| 301 write_thread_exists_(false), | 297 write_thread_exists_(false), |
| 302 write_thread_running_(false), | 298 write_thread_running_(false) { |
| 303 write_monitor_(new Monitor()) { | |
| 304 type_ = kStd; | 299 type_ = kStd; |
| 305 } | 300 } |
| 306 | 301 |
| 307 ~StdHandle() { | |
| 308 delete write_monitor_; | |
| 309 } | |
| 310 | |
| 311 virtual void DoClose(); | 302 virtual void DoClose(); |
| 312 virtual intptr_t Write(const void* buffer, intptr_t num_bytes); | 303 virtual intptr_t Write(const void* buffer, intptr_t num_bytes); |
| 313 | 304 |
| 314 void WriteSyncCompleteAsync(); | 305 void WriteSyncCompleteAsync(); |
| 315 void RunWriteLoop(); | 306 void RunWriteLoop(); |
| 316 | 307 |
| 317 private: | 308 private: |
| 309 ThreadId thread_id_; |
| 318 intptr_t thread_wrote_; | 310 intptr_t thread_wrote_; |
| 319 bool write_thread_exists_; | 311 bool write_thread_exists_; |
| 320 bool write_thread_running_; | 312 bool write_thread_running_; |
| 321 Monitor* write_monitor_; | |
| 322 }; | 313 }; |
| 323 | 314 |
| 324 | 315 |
| 325 class DirectoryWatchHandle : public DescriptorInfoSingleMixin<Handle> { | 316 class DirectoryWatchHandle : public DescriptorInfoSingleMixin<Handle> { |
| 326 public: | 317 public: |
| 327 DirectoryWatchHandle(HANDLE handle, int events, bool recursive) | 318 DirectoryWatchHandle(HANDLE handle, int events, bool recursive) |
| 328 : DescriptorInfoSingleMixin(reinterpret_cast<intptr_t>(handle), true), | 319 : DescriptorInfoSingleMixin(reinterpret_cast<intptr_t>(handle), true), |
| 329 events_(events), | 320 events_(events), |
| 330 recursive_(recursive) { | 321 recursive_(recursive) { |
| 331 type_ = kDirectoryWatch; | 322 type_ = kDirectoryWatch; |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 519 void HandleConnect(ClientSocket* client_socket, | 510 void HandleConnect(ClientSocket* client_socket, |
| 520 int bytes, | 511 int bytes, |
| 521 OverlappedBuffer* buffer); | 512 OverlappedBuffer* buffer); |
| 522 void HandleIOCompletion(DWORD bytes, ULONG_PTR key, OVERLAPPED* overlapped); | 513 void HandleIOCompletion(DWORD bytes, ULONG_PTR key, OVERLAPPED* overlapped); |
| 523 | 514 |
| 524 HANDLE completion_port() { return completion_port_; } | 515 HANDLE completion_port() { return completion_port_; } |
| 525 | 516 |
| 526 private: | 517 private: |
| 527 ClientSocket* client_sockets_head_; | 518 ClientSocket* client_sockets_head_; |
| 528 | 519 |
| 520 Monitor* startup_monitor_; |
| 521 ThreadId handler_thread_id_; |
| 522 |
| 529 TimeoutQueue timeout_queue_; // Time for next timeout. | 523 TimeoutQueue timeout_queue_; // Time for next timeout. |
| 530 bool shutdown_; | 524 bool shutdown_; |
| 531 HANDLE completion_port_; | 525 HANDLE completion_port_; |
| 532 }; | 526 }; |
| 533 | 527 |
| 534 } // namespace bin | 528 } // namespace bin |
| 535 } // namespace dart | 529 } // namespace dart |
| 536 | 530 |
| 537 #endif // BIN_EVENTHANDLER_WIN_H_ | 531 #endif // BIN_EVENTHANDLER_WIN_H_ |
| OLD | NEW |