blob: 37515d92ff20906b035756f8de50b3fa597802ba [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef RTC_BASE_WIN32_SOCKET_SERVER_H_
12#define RTC_BASE_WIN32_SOCKET_SERVER_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#if defined(WEBRTC_WIN)
Steve Anton10542f22019-01-11 09:11:00 -080015#include "rtc_base/async_socket.h"
16#include "rtc_base/critical_section.h"
17#include "rtc_base/message_queue.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/socket.h"
Steve Anton10542f22019-01-11 09:11:00 -080019#include "rtc_base/socket_factory.h"
20#include "rtc_base/socket_server.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/thread.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "rtc_base/win32_window.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000023
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020024namespace rtc {
25
26///////////////////////////////////////////////////////////////////////////////
27// Win32Socket
28///////////////////////////////////////////////////////////////////////////////
29
30class Win32Socket : public AsyncSocket {
31 public:
32 Win32Socket();
Steve Anton9de3aac2017-10-24 10:08:26 -070033 ~Win32Socket() override;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020034
35 bool CreateT(int family, int type);
36
37 int Attach(SOCKET s);
38 void SetTimeout(int ms);
39
40 // AsyncSocket Interface
Steve Anton9de3aac2017-10-24 10:08:26 -070041 SocketAddress GetLocalAddress() const override;
42 SocketAddress GetRemoteAddress() const override;
43 int Bind(const SocketAddress& addr) override;
44 int Connect(const SocketAddress& addr) override;
45 int Send(const void* buffer, size_t length) override;
46 int SendTo(const void* buffer,
47 size_t length,
48 const SocketAddress& addr) override;
49 int Recv(void* buffer, size_t length, int64_t* timestamp) override;
50 int RecvFrom(void* buffer,
51 size_t length,
52 SocketAddress* out_addr,
53 int64_t* timestamp) override;
54 int Listen(int backlog) override;
55 Win32Socket* Accept(SocketAddress* out_addr) override;
56 int Close() override;
57 int GetError() const override;
58 void SetError(int error) override;
59 ConnState GetState() const override;
60 int GetOption(Option opt, int* value) override;
61 int SetOption(Option opt, int value) override;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020062
63 private:
64 void CreateSink();
65 bool SetAsync(int events);
66 int DoConnect(const SocketAddress& addr);
67 bool HandleClosed(int close_error);
68 void PostClosed();
69 void UpdateLastError();
70 static int TranslateOption(Option opt, int* slevel, int* sopt);
71
72 void OnSocketNotify(SOCKET socket, int event, int error);
73 void OnDnsNotify(HANDLE task, int error);
74
75 SOCKET socket_;
76 int error_;
77 ConnState state_;
Yves Gerey665174f2018-06-19 15:03:05 +020078 SocketAddress addr_; // address that we connected to (see DoConnect)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020079 uint32_t connect_time_;
80 bool closing_;
81 int close_error_;
82
83 class EventSink;
84 friend class EventSink;
Yves Gerey665174f2018-06-19 15:03:05 +020085 EventSink* sink_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020086
87 struct DnsLookup;
Yves Gerey665174f2018-06-19 15:03:05 +020088 DnsLookup* dns_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020089};
90
91///////////////////////////////////////////////////////////////////////////////
92// Win32SocketServer
93///////////////////////////////////////////////////////////////////////////////
94
95class Win32SocketServer : public SocketServer {
96 public:
97 Win32SocketServer();
Steve Anton9de3aac2017-10-24 10:08:26 -070098 ~Win32SocketServer() override;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020099
Yves Gerey665174f2018-06-19 15:03:05 +0200100 void set_modeless_dialog(HWND hdlg) { hdlg_ = hdlg; }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200101
102 // SocketServer Interface
Steve Anton9de3aac2017-10-24 10:08:26 -0700103 Socket* CreateSocket(int family, int type) override;
Steve Anton9de3aac2017-10-24 10:08:26 -0700104 AsyncSocket* CreateAsyncSocket(int family, int type) override;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200105
Steve Anton9de3aac2017-10-24 10:08:26 -0700106 void SetMessageQueue(MessageQueue* queue) override;
107 bool Wait(int cms, bool process_io) override;
108 void WakeUp() override;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200109
110 void Pump();
111
112 HWND handle() { return wnd_.handle(); }
113
114 private:
115 class MessageWindow : public Win32Window {
116 public:
117 explicit MessageWindow(Win32SocketServer* ss) : ss_(ss) {}
Yves Gerey665174f2018-06-19 15:03:05 +0200118
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200119 private:
Steve Anton9de3aac2017-10-24 10:08:26 -0700120 bool OnMessage(UINT msg, WPARAM wp, LPARAM lp, LRESULT& result) override;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200121 Win32SocketServer* ss_;
122 };
123
124 static const TCHAR kWindowName[];
Yves Gerey665174f2018-06-19 15:03:05 +0200125 MessageQueue* message_queue_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200126 MessageWindow wnd_;
127 CriticalSection cs_;
128 bool posted_;
129 HWND hdlg_;
130};
131
132///////////////////////////////////////////////////////////////////////////////
133// Win32Thread. Automatically pumps Windows messages.
134///////////////////////////////////////////////////////////////////////////////
135
136class Win32Thread : public Thread {
137 public:
Steve Anton9de3aac2017-10-24 10:08:26 -0700138 explicit Win32Thread(SocketServer* ss);
139 ~Win32Thread() override;
140
141 void Run() override;
142 void Quit() override;
143
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200144 private:
145 DWORD id_;
146};
147
148///////////////////////////////////////////////////////////////////////////////
149
150} // namespace rtc
151
152#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000153
Steve Anton10542f22019-01-11 09:11:00 -0800154#endif // RTC_BASE_WIN32_SOCKET_SERVER_H_