You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
4.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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/critical_section.h"
  15. #include "rtc_base/message_queue.h"
  16. #include "rtc_base/socket.h"
  17. #include "rtc_base/socket_factory.h"
  18. #include "rtc_base/socket_server.h"
  19. #include "rtc_base/thread.h"
  20. #include "rtc_base/win32_window.h"
  21. namespace rtc {
  22. ///////////////////////////////////////////////////////////////////////////////
  23. // Win32Socket
  24. ///////////////////////////////////////////////////////////////////////////////
  25. class Win32Socket : public AsyncSocket {
  26. public:
  27. Win32Socket();
  28. ~Win32Socket() override;
  29. bool CreateT(int family, int type);
  30. int Attach(SOCKET s);
  31. void SetTimeout(int ms);
  32. // AsyncSocket Interface
  33. SocketAddress GetLocalAddress() const override;
  34. SocketAddress GetRemoteAddress() const override;
  35. int Bind(const SocketAddress& addr) override;
  36. int Connect(const SocketAddress& addr) override;
  37. int Send(const void* buffer, size_t length) override;
  38. int SendTo(const void* buffer,
  39. size_t length,
  40. const SocketAddress& addr) override;
  41. int Recv(void* buffer, size_t length, int64_t* timestamp) override;
  42. int RecvFrom(void* buffer,
  43. size_t length,
  44. SocketAddress* out_addr,
  45. int64_t* timestamp) override;
  46. int Listen(int backlog) override;
  47. Win32Socket* Accept(SocketAddress* out_addr) override;
  48. int Close() override;
  49. int GetError() const override;
  50. void SetError(int error) override;
  51. ConnState GetState() const override;
  52. int GetOption(Option opt, int* value) override;
  53. int SetOption(Option opt, int value) override;
  54. private:
  55. void CreateSink();
  56. bool SetAsync(int events);
  57. int DoConnect(const SocketAddress& addr);
  58. bool HandleClosed(int close_error);
  59. void PostClosed();
  60. void UpdateLastError();
  61. static int TranslateOption(Option opt, int* slevel, int* sopt);
  62. void OnSocketNotify(SOCKET socket, int event, int error);
  63. void OnDnsNotify(HANDLE task, int error);
  64. SOCKET socket_;
  65. int error_;
  66. ConnState state_;
  67. SocketAddress addr_; // address that we connected to (see DoConnect)
  68. uint32_t connect_time_;
  69. bool closing_;
  70. int close_error_;
  71. class EventSink;
  72. friend class EventSink;
  73. EventSink* sink_;
  74. struct DnsLookup;
  75. DnsLookup* dns_;
  76. };
  77. ///////////////////////////////////////////////////////////////////////////////
  78. // Win32SocketServer
  79. ///////////////////////////////////////////////////////////////////////////////
  80. class Win32SocketServer : public SocketServer {
  81. public:
  82. Win32SocketServer();
  83. ~Win32SocketServer() override;
  84. void set_modeless_dialog(HWND hdlg) { hdlg_ = hdlg; }
  85. // SocketServer Interface
  86. Socket* CreateSocket(int family, int type) override;
  87. AsyncSocket* CreateAsyncSocket(int family, int type) override;
  88. void SetMessageQueue(MessageQueue* queue) override;
  89. bool Wait(int cms, bool process_io) override;
  90. void WakeUp() override;
  91. void Pump();
  92. HWND handle() { return wnd_.handle(); }
  93. private:
  94. class MessageWindow : public Win32Window {
  95. public:
  96. explicit MessageWindow(Win32SocketServer* ss) : ss_(ss) {}
  97. private:
  98. bool OnMessage(UINT msg, WPARAM wp, LPARAM lp, LRESULT& result) override;
  99. Win32SocketServer* ss_;
  100. };
  101. static const wchar_t kWindowName[];
  102. MessageQueue* message_queue_;
  103. MessageWindow wnd_;
  104. CriticalSection cs_;
  105. bool posted_;
  106. HWND hdlg_;
  107. };
  108. ///////////////////////////////////////////////////////////////////////////////
  109. // Win32Thread. Automatically pumps Windows messages.
  110. ///////////////////////////////////////////////////////////////////////////////
  111. class Win32Thread : public Thread {
  112. public:
  113. explicit Win32Thread(SocketServer* ss);
  114. ~Win32Thread() override;
  115. void Run() override;
  116. void Quit() override;
  117. private:
  118. DWORD id_;
  119. };
  120. ///////////////////////////////////////////////////////////////////////////////
  121. } // namespace rtc
  122. #endif // WEBRTC_WIN
  123. #endif // RTC_BASE_WIN32_SOCKET_SERVER_H_