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