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.

180 lines
5.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef TALK_BASE_WIN32SOCKETSERVER_H_
  28. #define TALK_BASE_WIN32SOCKETSERVER_H_
  29. #ifdef WIN32
  30. #include "talk/base/asyncsocket.h"
  31. #include "talk/base/criticalsection.h"
  32. #include "talk/base/messagequeue.h"
  33. #include "talk/base/socketserver.h"
  34. #include "talk/base/socketfactory.h"
  35. #include "talk/base/socket.h"
  36. #include "talk/base/thread.h"
  37. #include "talk/base/win32window.h"
  38. namespace talk_base {
  39. ///////////////////////////////////////////////////////////////////////////////
  40. // Win32Socket
  41. ///////////////////////////////////////////////////////////////////////////////
  42. class Win32Socket : public AsyncSocket {
  43. public:
  44. Win32Socket();
  45. virtual ~Win32Socket();
  46. bool CreateT(int family, int type);
  47. int Attach(SOCKET s);
  48. void SetTimeout(int ms);
  49. // AsyncSocket Interface
  50. virtual SocketAddress GetLocalAddress() const;
  51. virtual SocketAddress GetRemoteAddress() const;
  52. virtual int Bind(const SocketAddress& addr);
  53. virtual int Connect(const SocketAddress& addr);
  54. virtual int Send(const void *buffer, size_t length);
  55. virtual int SendTo(const void *buffer, size_t length, const SocketAddress& addr);
  56. virtual int Recv(void *buffer, size_t length);
  57. virtual int RecvFrom(void *buffer, size_t length, SocketAddress *out_addr);
  58. virtual int Listen(int backlog);
  59. virtual Win32Socket *Accept(SocketAddress *out_addr);
  60. virtual int Close();
  61. virtual int GetError() const;
  62. virtual void SetError(int error);
  63. virtual ConnState GetState() const;
  64. virtual int EstimateMTU(uint16* mtu);
  65. virtual int GetOption(Option opt, int* value);
  66. virtual int SetOption(Option opt, int value);
  67. private:
  68. void CreateSink();
  69. bool SetAsync(int events);
  70. int DoConnect(const SocketAddress& addr);
  71. bool HandleClosed(int close_error);
  72. void PostClosed();
  73. void UpdateLastError();
  74. static int TranslateOption(Option opt, int* slevel, int* sopt);
  75. void OnSocketNotify(SOCKET socket, int event, int error);
  76. void OnDnsNotify(HANDLE task, int error);
  77. SOCKET socket_;
  78. int error_;
  79. ConnState state_;
  80. SocketAddress addr_; // address that we connected to (see DoConnect)
  81. uint32 connect_time_;
  82. bool closing_;
  83. int close_error_;
  84. class EventSink;
  85. friend class EventSink;
  86. EventSink * sink_;
  87. struct DnsLookup;
  88. DnsLookup * dns_;
  89. };
  90. ///////////////////////////////////////////////////////////////////////////////
  91. // Win32SocketServer
  92. ///////////////////////////////////////////////////////////////////////////////
  93. class Win32SocketServer : public SocketServer {
  94. public:
  95. explicit Win32SocketServer(MessageQueue* message_queue);
  96. virtual ~Win32SocketServer();
  97. void set_modeless_dialog(HWND hdlg) {
  98. hdlg_ = hdlg;
  99. }
  100. // SocketServer Interface
  101. virtual Socket* CreateSocket(int type);
  102. virtual Socket* CreateSocket(int family, int type);
  103. virtual AsyncSocket* CreateAsyncSocket(int type);
  104. virtual AsyncSocket* CreateAsyncSocket(int family, int type);
  105. virtual void SetMessageQueue(MessageQueue* queue);
  106. virtual bool Wait(int cms, bool process_io);
  107. virtual void WakeUp();
  108. void Pump();
  109. HWND handle() { return wnd_.handle(); }
  110. private:
  111. class MessageWindow : public Win32Window {
  112. public:
  113. explicit MessageWindow(Win32SocketServer* ss) : ss_(ss) {}
  114. private:
  115. virtual bool OnMessage(UINT msg, WPARAM wp, LPARAM lp, LRESULT& result);
  116. Win32SocketServer* ss_;
  117. };
  118. static const TCHAR kWindowName[];
  119. MessageQueue *message_queue_;
  120. MessageWindow wnd_;
  121. CriticalSection cs_;
  122. bool posted_;
  123. HWND hdlg_;
  124. };
  125. ///////////////////////////////////////////////////////////////////////////////
  126. // Win32Thread. Automatically pumps Windows messages.
  127. ///////////////////////////////////////////////////////////////////////////////
  128. class Win32Thread : public Thread {
  129. public:
  130. Win32Thread() : ss_(this), id_(0) {
  131. set_socketserver(&ss_);
  132. }
  133. virtual ~Win32Thread() {
  134. set_socketserver(NULL);
  135. }
  136. virtual void Run() {
  137. id_ = GetCurrentThreadId();
  138. Thread::Run();
  139. id_ = 0;
  140. }
  141. virtual void Quit() {
  142. PostThreadMessage(id_, WM_QUIT, 0, 0);
  143. }
  144. private:
  145. Win32SocketServer ss_;
  146. DWORD id_;
  147. };
  148. ///////////////////////////////////////////////////////////////////////////////
  149. } // namespace talk_base
  150. #endif // WIN32
  151. #endif // TALK_BASE_WIN32SOCKETSERVER_H_