| 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 <mswsock.h> |
| 12 #include <winsock2.h> | 13 #include <winsock2.h> |
| 13 #include <ws2tcpip.h> | 14 #include <ws2tcpip.h> |
| 14 #include <mswsock.h> | |
| 15 | 15 |
| 16 #include "bin/builtin.h" | 16 #include "bin/builtin.h" |
| 17 #include "bin/thread.h" | 17 #include "bin/thread.h" |
| 18 | 18 |
| 19 | |
| 20 namespace dart { | 19 namespace dart { |
| 21 namespace bin { | 20 namespace bin { |
| 22 | 21 |
| 23 // Forward declarations. | 22 // Forward declarations. |
| 24 class EventHandlerImplementation; | 23 class EventHandlerImplementation; |
| 25 class Handle; | 24 class Handle; |
| 26 class FileHandle; | 25 class FileHandle; |
| 27 class SocketHandle; | 26 class SocketHandle; |
| 28 class ClientSocket; | 27 class ClientSocket; |
| 29 class ListenSocket; | 28 class ListenSocket; |
| 30 | 29 |
| 31 | |
| 32 // An OverlappedBuffer encapsulates the OVERLAPPED structure and the | 30 // An OverlappedBuffer encapsulates the OVERLAPPED structure and the |
| 33 // associated data buffer. For accept it also contains the pre-created | 31 // associated data buffer. For accept it also contains the pre-created |
| 34 // socket for the client. | 32 // socket for the client. |
| 35 class OverlappedBuffer { | 33 class OverlappedBuffer { |
| 36 public: | 34 public: |
| 37 enum Operation { | 35 enum Operation { |
| 38 kAccept, kRead, kRecvFrom, kWrite, kSendTo, kDisconnect, kConnect | 36 kAccept, kRead, kRecvFrom, kWrite, kSendTo, kDisconnect, kConnect |
| 39 }; | 37 }; |
| 40 | 38 |
| 41 static OverlappedBuffer* AllocateAcceptBuffer(int buffer_size); | 39 static OverlappedBuffer* AllocateAcceptBuffer(int buffer_size); |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 | 140 |
| 143 // For the recvfrom operation additional storace is allocated for the | 141 // For the recvfrom operation additional storace is allocated for the |
| 144 // source sockaddr. | 142 // source sockaddr. |
| 145 socklen_t* from_len_addr_; // Pointer to source sockaddr size storage. | 143 socklen_t* from_len_addr_; // Pointer to source sockaddr size storage. |
| 146 struct sockaddr* from_; // Pointer to source sockaddr storage. | 144 struct sockaddr* from_; // Pointer to source sockaddr storage. |
| 147 | 145 |
| 148 // Buffer for recv/send/AcceptEx. This must be at the end of the | 146 // Buffer for recv/send/AcceptEx. This must be at the end of the |
| 149 // object as the object is allocated larger than it's definition | 147 // object as the object is allocated larger than it's definition |
| 150 // indicate to extend this array. | 148 // indicate to extend this array. |
| 151 uint8_t buffer_data_[1]; | 149 uint8_t buffer_data_[1]; |
| 150 |
| 151 DISALLOW_COPY_AND_ASSIGN(OverlappedBuffer); |
| 152 }; | 152 }; |
| 153 | 153 |
| 154 | 154 |
| 155 // Abstract super class for holding information on listen and connected | 155 // Abstract super class for holding information on listen and connected |
| 156 // sockets. | 156 // sockets. |
| 157 class Handle : public DescriptorInfoBase { | 157 class Handle : public DescriptorInfoBase { |
| 158 public: | 158 public: |
| 159 enum Type { | 159 enum Type { |
| 160 kFile, | 160 kFile, |
| 161 kStd, | 161 kStd, |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 bool read_thread_starting_; | 266 bool read_thread_starting_; |
| 267 bool read_thread_finished_; | 267 bool read_thread_finished_; |
| 268 | 268 |
| 269 private: | 269 private: |
| 270 void WaitForReadThreadStarted(); | 270 void WaitForReadThreadStarted(); |
| 271 void NotifyReadThreadStarted(); | 271 void NotifyReadThreadStarted(); |
| 272 void WaitForReadThreadFinished(); | 272 void WaitForReadThreadFinished(); |
| 273 void NotifyReadThreadFinished(); | 273 void NotifyReadThreadFinished(); |
| 274 | 274 |
| 275 int flags_; | 275 int flags_; |
| 276 |
| 277 DISALLOW_COPY_AND_ASSIGN(Handle); |
| 276 }; | 278 }; |
| 277 | 279 |
| 278 | 280 |
| 279 class FileHandle : public DescriptorInfoSingleMixin<Handle> { | 281 class FileHandle : public DescriptorInfoSingleMixin<Handle> { |
| 280 public: | 282 public: |
| 281 explicit FileHandle(HANDLE handle) | 283 explicit FileHandle(HANDLE handle) |
| 282 : DescriptorInfoSingleMixin(reinterpret_cast<intptr_t>(handle), true) { | 284 : DescriptorInfoSingleMixin(reinterpret_cast<intptr_t>(handle), true) { |
| 283 type_ = kFile; | 285 type_ = kFile; |
| 284 } | 286 } |
| 285 | 287 |
| 286 virtual void EnsureInitialized(EventHandlerImplementation* event_handler); | 288 virtual void EnsureInitialized(EventHandlerImplementation* event_handler); |
| 287 virtual bool IsClosed(); | 289 virtual bool IsClosed(); |
| 290 |
| 291 private: |
| 292 DISALLOW_COPY_AND_ASSIGN(FileHandle); |
| 288 }; | 293 }; |
| 289 | 294 |
| 290 | 295 |
| 291 class StdHandle : public FileHandle { | 296 class StdHandle : public FileHandle { |
| 292 public: | 297 public: |
| 293 explicit StdHandle(HANDLE handle) | 298 explicit StdHandle(HANDLE handle) |
| 294 : FileHandle(handle), | 299 : FileHandle(handle), |
| 295 thread_id_(Thread::kInvalidThreadId), | 300 thread_id_(Thread::kInvalidThreadId), |
| 296 thread_wrote_(0), | 301 thread_wrote_(0), |
| 297 write_thread_exists_(false), | 302 write_thread_exists_(false), |
| 298 write_thread_running_(false) { | 303 write_thread_running_(false) { |
| 299 type_ = kStd; | 304 type_ = kStd; |
| 300 } | 305 } |
| 301 | 306 |
| 302 virtual void DoClose(); | 307 virtual void DoClose(); |
| 303 virtual intptr_t Write(const void* buffer, intptr_t num_bytes); | 308 virtual intptr_t Write(const void* buffer, intptr_t num_bytes); |
| 304 | 309 |
| 305 void WriteSyncCompleteAsync(); | 310 void WriteSyncCompleteAsync(); |
| 306 void RunWriteLoop(); | 311 void RunWriteLoop(); |
| 307 | 312 |
| 308 private: | 313 private: |
| 309 ThreadId thread_id_; | 314 ThreadId thread_id_; |
| 310 intptr_t thread_wrote_; | 315 intptr_t thread_wrote_; |
| 311 bool write_thread_exists_; | 316 bool write_thread_exists_; |
| 312 bool write_thread_running_; | 317 bool write_thread_running_; |
| 318 |
| 319 DISALLOW_COPY_AND_ASSIGN(StdHandle); |
| 313 }; | 320 }; |
| 314 | 321 |
| 315 | 322 |
| 316 class DirectoryWatchHandle : public DescriptorInfoSingleMixin<Handle> { | 323 class DirectoryWatchHandle : public DescriptorInfoSingleMixin<Handle> { |
| 317 public: | 324 public: |
| 318 DirectoryWatchHandle(HANDLE handle, int events, bool recursive) | 325 DirectoryWatchHandle(HANDLE handle, int events, bool recursive) |
| 319 : DescriptorInfoSingleMixin(reinterpret_cast<intptr_t>(handle), true), | 326 : DescriptorInfoSingleMixin(reinterpret_cast<intptr_t>(handle), true), |
| 320 events_(events), | 327 events_(events), |
| 321 recursive_(recursive) { | 328 recursive_(recursive) { |
| 322 type_ = kDirectoryWatch; | 329 type_ = kDirectoryWatch; |
| 323 } | 330 } |
| 324 | 331 |
| 325 virtual void EnsureInitialized(EventHandlerImplementation* event_handler); | 332 virtual void EnsureInitialized(EventHandlerImplementation* event_handler); |
| 326 virtual bool IsClosed(); | 333 virtual bool IsClosed(); |
| 327 | 334 |
| 328 virtual bool IssueRead(); | 335 virtual bool IssueRead(); |
| 329 | 336 |
| 330 void Stop(); | 337 void Stop(); |
| 331 | 338 |
| 332 private: | 339 private: |
| 333 int events_; | 340 int events_; |
| 334 bool recursive_; | 341 bool recursive_; |
| 342 |
| 343 DISALLOW_COPY_AND_ASSIGN(DirectoryWatchHandle); |
| 335 }; | 344 }; |
| 336 | 345 |
| 337 | 346 |
| 338 class SocketHandle : public Handle { | 347 class SocketHandle : public Handle { |
| 339 public: | 348 public: |
| 340 SOCKET socket() const { return socket_; } | 349 SOCKET socket() const { return socket_; } |
| 341 | 350 |
| 342 protected: | 351 protected: |
| 343 explicit SocketHandle(intptr_t s) | 352 explicit SocketHandle(intptr_t s) |
| 344 : Handle(s), | 353 : Handle(s), |
| 345 socket_(s) {} | 354 socket_(s) {} |
| 346 | 355 |
| 347 virtual void HandleIssueError(); | 356 virtual void HandleIssueError(); |
| 348 | 357 |
| 349 private: | 358 private: |
| 350 const SOCKET socket_; | 359 const SOCKET socket_; |
| 360 |
| 361 DISALLOW_COPY_AND_ASSIGN(SocketHandle); |
| 351 }; | 362 }; |
| 352 | 363 |
| 353 | 364 |
| 354 // Information on listen sockets. | 365 // Information on listen sockets. |
| 355 class ListenSocket : public DescriptorInfoMultipleMixin<SocketHandle> { | 366 class ListenSocket : public DescriptorInfoMultipleMixin<SocketHandle> { |
| 356 public: | 367 public: |
| 357 explicit ListenSocket(intptr_t s) : DescriptorInfoMultipleMixin(s, true), | 368 explicit ListenSocket(intptr_t s) : DescriptorInfoMultipleMixin(s, true), |
| 358 AcceptEx_(NULL), | 369 AcceptEx_(NULL), |
| 359 pending_accept_count_(0), | 370 pending_accept_count_(0), |
| 360 accepted_head_(NULL), | 371 accepted_head_(NULL), |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 396 int pending_accept_count_; | 407 int pending_accept_count_; |
| 397 | 408 |
| 398 // Linked list of accepted connections provided by completion code. Ready to | 409 // Linked list of accepted connections provided by completion code. Ready to |
| 399 // be handed over through accept. | 410 // be handed over through accept. |
| 400 ClientSocket* accepted_head_; | 411 ClientSocket* accepted_head_; |
| 401 ClientSocket* accepted_tail_; | 412 ClientSocket* accepted_tail_; |
| 402 | 413 |
| 403 // The number of accepted connections which are waiting to be removed from | 414 // The number of accepted connections which are waiting to be removed from |
| 404 // this queue and processed by dart isolates. | 415 // this queue and processed by dart isolates. |
| 405 int accepted_count_; | 416 int accepted_count_; |
| 417 |
| 418 DISALLOW_COPY_AND_ASSIGN(ListenSocket); |
| 406 }; | 419 }; |
| 407 | 420 |
| 408 | 421 |
| 409 // Information on connected sockets. | 422 // Information on connected sockets. |
| 410 class ClientSocket : public DescriptorInfoSingleMixin<SocketHandle> { | 423 class ClientSocket : public DescriptorInfoSingleMixin<SocketHandle> { |
| 411 public: | 424 public: |
| 412 explicit ClientSocket(intptr_t s) : DescriptorInfoSingleMixin(s, true), | 425 explicit ClientSocket(intptr_t s) : DescriptorInfoSingleMixin(s, true), |
| 413 DisconnectEx_(NULL), | 426 DisconnectEx_(NULL), |
| 414 next_(NULL), | 427 next_(NULL), |
| 415 connected_(false), | 428 connected_(false), |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 453 closed_ = true; | 466 closed_ = true; |
| 454 } | 467 } |
| 455 | 468 |
| 456 private: | 469 private: |
| 457 bool LoadDisconnectEx(); | 470 bool LoadDisconnectEx(); |
| 458 | 471 |
| 459 LPFN_DISCONNECTEX DisconnectEx_; | 472 LPFN_DISCONNECTEX DisconnectEx_; |
| 460 ClientSocket* next_; | 473 ClientSocket* next_; |
| 461 bool connected_; | 474 bool connected_; |
| 462 bool closed_; | 475 bool closed_; |
| 476 |
| 477 DISALLOW_COPY_AND_ASSIGN(ClientSocket); |
| 463 }; | 478 }; |
| 464 | 479 |
| 465 | 480 |
| 466 class DatagramSocket : public DescriptorInfoSingleMixin<SocketHandle> { | 481 class DatagramSocket : public DescriptorInfoSingleMixin<SocketHandle> { |
| 467 public: | 482 public: |
| 468 explicit DatagramSocket(intptr_t s) : DescriptorInfoSingleMixin(s, true) { | 483 explicit DatagramSocket(intptr_t s) : DescriptorInfoSingleMixin(s, true) { |
| 469 type_ = kDatagramSocket; | 484 type_ = kDatagramSocket; |
| 470 } | 485 } |
| 471 | 486 |
| 472 virtual ~DatagramSocket() { | 487 virtual ~DatagramSocket() { |
| 473 // Don't delete this object until all pending requests have been handled. | 488 // Don't delete this object until all pending requests have been handled. |
| 474 ASSERT(!HasPendingRead()); | 489 ASSERT(!HasPendingRead()); |
| 475 ASSERT(!HasPendingWrite()); | 490 ASSERT(!HasPendingWrite()); |
| 476 } | 491 } |
| 477 | 492 |
| 478 // Internal interface used by the event handler. | 493 // Internal interface used by the event handler. |
| 479 virtual bool IssueRecvFrom(); | 494 virtual bool IssueRecvFrom(); |
| 480 virtual bool IssueSendTo(sockaddr* sa, socklen_t sa_len); | 495 virtual bool IssueSendTo(sockaddr* sa, socklen_t sa_len); |
| 481 | 496 |
| 482 virtual void EnsureInitialized(EventHandlerImplementation* event_handler); | 497 virtual void EnsureInitialized(EventHandlerImplementation* event_handler); |
| 483 virtual void DoClose(); | 498 virtual void DoClose(); |
| 484 virtual bool IsClosed(); | 499 virtual bool IsClosed(); |
| 500 |
| 501 private: |
| 502 DISALLOW_COPY_AND_ASSIGN(DatagramSocket); |
| 485 }; | 503 }; |
| 486 | 504 |
| 505 |
| 487 // Event handler. | 506 // Event handler. |
| 488 class EventHandlerImplementation { | 507 class EventHandlerImplementation { |
| 489 public: | 508 public: |
| 490 EventHandlerImplementation(); | 509 EventHandlerImplementation(); |
| 491 virtual ~EventHandlerImplementation(); | 510 virtual ~EventHandlerImplementation(); |
| 492 | 511 |
| 493 void SendData(intptr_t id, Dart_Port dart_port, int64_t data); | 512 void SendData(intptr_t id, Dart_Port dart_port, int64_t data); |
| 494 void Start(EventHandler* handler); | 513 void Start(EventHandler* handler); |
| 495 void Shutdown(); | 514 void Shutdown(); |
| 496 | 515 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 516 | 535 |
| 517 private: | 536 private: |
| 518 ClientSocket* client_sockets_head_; | 537 ClientSocket* client_sockets_head_; |
| 519 | 538 |
| 520 Monitor* startup_monitor_; | 539 Monitor* startup_monitor_; |
| 521 ThreadId handler_thread_id_; | 540 ThreadId handler_thread_id_; |
| 522 | 541 |
| 523 TimeoutQueue timeout_queue_; // Time for next timeout. | 542 TimeoutQueue timeout_queue_; // Time for next timeout. |
| 524 bool shutdown_; | 543 bool shutdown_; |
| 525 HANDLE completion_port_; | 544 HANDLE completion_port_; |
| 545 |
| 546 DISALLOW_COPY_AND_ASSIGN(EventHandlerImplementation); |
| 526 }; | 547 }; |
| 527 | 548 |
| 528 } // namespace bin | 549 } // namespace bin |
| 529 } // namespace dart | 550 } // namespace dart |
| 530 | 551 |
| 531 #endif // BIN_EVENTHANDLER_WIN_H_ | 552 #endif // BIN_EVENTHANDLER_WIN_H_ |
| OLD | NEW |