win32_socket_server.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright 2004 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef RTC_BASE_WIN32_SOCKET_SERVER_H_
  11. #define RTC_BASE_WIN32_SOCKET_SERVER_H_
  12. #if defined(WEBRTC_WIN)
  13. #include "rtc_base/async_socket.h"
  14. #include "rtc_base/socket.h"
  15. #include "rtc_base/socket_factory.h"
  16. #include "rtc_base/socket_server.h"
  17. #include "rtc_base/synchronization/mutex.h"
  18. #include "rtc_base/thread.h"
  19. #include "rtc_base/win32_window.h"
  20. namespace rtc {
  21. ///////////////////////////////////////////////////////////////////////////////
  22. // Win32Socket
  23. ///////////////////////////////////////////////////////////////////////////////
  24. class Win32Socket : public AsyncSocket {
  25. public:
  26. Win32Socket();
  27. ~Win32Socket() override;
  28. bool CreateT(int family, int type);
  29. int Attach(SOCKET s);
  30. void SetTimeout(int ms);
  31. // AsyncSocket Interface
  32. SocketAddress GetLocalAddress() const override;
  33. SocketAddress GetRemoteAddress() const override;
  34. int Bind(const SocketAddress& addr) override;
  35. int Connect(const SocketAddress& addr) override;
  36. int Send(const void* buffer, size_t length) override;
  37. int SendTo(const void* buffer,
  38. size_t length,
  39. const SocketAddress& addr) override;
  40. int Recv(void* buffer, size_t length, int64_t* timestamp) override;
  41. int RecvFrom(void* buffer,
  42. size_t length,
  43. SocketAddress* out_addr,
  44. int64_t* timestamp) override;
  45. int Listen(int backlog) override;
  46. Win32Socket* Accept(SocketAddress* out_addr) override;
  47. int Close() override;
  48. int GetError() const override;
  49. void SetError(int error) override;
  50. ConnState GetState() const override;
  51. int GetOption(Option opt, int* value) override;
  52. int SetOption(Option opt, int value) override;
  53. private:
  54. void CreateSink();
  55. bool SetAsync(int events);
  56. int DoConnect(const SocketAddress& addr);
  57. bool HandleClosed(int close_error);
  58. void PostClosed();
  59. void UpdateLastError();
  60. static int TranslateOption(Option opt, int* slevel, int* sopt);
  61. void OnSocketNotify(SOCKET socket, int event, int error);
  62. void OnDnsNotify(HANDLE task, int error);
  63. SOCKET socket_;
  64. int error_;
  65. ConnState state_;
  66. SocketAddress addr_; // address that we connected to (see DoConnect)
  67. uint32_t connect_time_;
  68. bool closing_;
  69. int close_error_;
  70. class EventSink;
  71. friend class EventSink;
  72. EventSink* sink_;
  73. struct DnsLookup;
  74. DnsLookup* dns_;
  75. };
  76. ///////////////////////////////////////////////////////////////////////////////
  77. // Win32SocketServer
  78. ///////////////////////////////////////////////////////////////////////////////
  79. class Win32SocketServer : public SocketServer {
  80. public:
  81. Win32SocketServer();
  82. ~Win32SocketServer() override;
  83. void set_modeless_dialog(HWND hdlg) { hdlg_ = hdlg; }
  84. // SocketServer Interface
  85. Socket* CreateSocket(int family, int type) override;
  86. AsyncSocket* CreateAsyncSocket(int family, int type) override;
  87. void SetMessageQueue(Thread* queue) override;
  88. bool Wait(int cms, bool process_io) override;
  89. void WakeUp() override;
  90. void Pump();
  91. HWND handle() { return wnd_.handle(); }
  92. private:
  93. class MessageWindow : public Win32Window {
  94. public:
  95. explicit MessageWindow(Win32SocketServer* ss) : ss_(ss) {}
  96. private:
  97. bool OnMessage(UINT msg, WPARAM wp, LPARAM lp, LRESULT& result) override;
  98. Win32SocketServer* ss_;
  99. };
  100. static const wchar_t kWindowName[];
  101. Thread* message_queue_;
  102. MessageWindow wnd_;
  103. webrtc::Mutex mutex_;
  104. bool posted_;
  105. HWND hdlg_;
  106. };
  107. ///////////////////////////////////////////////////////////////////////////////
  108. // Win32Thread. Automatically pumps Windows messages.
  109. ///////////////////////////////////////////////////////////////////////////////
  110. class Win32Thread : public Thread {
  111. public:
  112. explicit Win32Thread(SocketServer* ss);
  113. ~Win32Thread() override;
  114. void Run() override;
  115. void Quit() override;
  116. private:
  117. DWORD id_;
  118. };
  119. ///////////////////////////////////////////////////////////////////////////////
  120. } // namespace rtc
  121. #endif // WEBRTC_WIN
  122. #endif // RTC_BASE_WIN32_SOCKET_SERVER_H_