henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #ifndef RTC_BASE_WIN32_SOCKET_SERVER_H_ |
| 12 | #define RTC_BASE_WIN32_SOCKET_SERVER_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 14 | #if defined(WEBRTC_WIN) |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 15 | #include "rtc_base/async_socket.h" |
| 16 | #include "rtc_base/critical_section.h" |
| 17 | #include "rtc_base/message_queue.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "rtc_base/socket.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 19 | #include "rtc_base/socket_factory.h" |
| 20 | #include "rtc_base/socket_server.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "rtc_base/thread.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 22 | #include "rtc_base/win32_window.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 23 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 24 | namespace rtc { |
| 25 | |
| 26 | /////////////////////////////////////////////////////////////////////////////// |
| 27 | // Win32Socket |
| 28 | /////////////////////////////////////////////////////////////////////////////// |
| 29 | |
| 30 | class Win32Socket : public AsyncSocket { |
| 31 | public: |
| 32 | Win32Socket(); |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 33 | ~Win32Socket() override; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 34 | |
| 35 | bool CreateT(int family, int type); |
| 36 | |
| 37 | int Attach(SOCKET s); |
| 38 | void SetTimeout(int ms); |
| 39 | |
| 40 | // AsyncSocket Interface |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 41 | 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 Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 62 | |
| 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 78 | SocketAddress addr_; // address that we connected to (see DoConnect) |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 79 | uint32_t connect_time_; |
| 80 | bool closing_; |
| 81 | int close_error_; |
| 82 | |
| 83 | class EventSink; |
| 84 | friend class EventSink; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 85 | EventSink* sink_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 86 | |
| 87 | struct DnsLookup; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 88 | DnsLookup* dns_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | /////////////////////////////////////////////////////////////////////////////// |
| 92 | // Win32SocketServer |
| 93 | /////////////////////////////////////////////////////////////////////////////// |
| 94 | |
| 95 | class Win32SocketServer : public SocketServer { |
| 96 | public: |
| 97 | Win32SocketServer(); |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 98 | ~Win32SocketServer() override; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 99 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 100 | void set_modeless_dialog(HWND hdlg) { hdlg_ = hdlg; } |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 101 | |
| 102 | // SocketServer Interface |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 103 | Socket* CreateSocket(int family, int type) override; |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 104 | AsyncSocket* CreateAsyncSocket(int family, int type) override; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 105 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 106 | void SetMessageQueue(MessageQueue* queue) override; |
| 107 | bool Wait(int cms, bool process_io) override; |
| 108 | void WakeUp() override; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 109 | |
| 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 118 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 119 | private: |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 120 | bool OnMessage(UINT msg, WPARAM wp, LPARAM lp, LRESULT& result) override; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 121 | Win32SocketServer* ss_; |
| 122 | }; |
| 123 | |
| 124 | static const TCHAR kWindowName[]; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 125 | MessageQueue* message_queue_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 126 | MessageWindow wnd_; |
| 127 | CriticalSection cs_; |
| 128 | bool posted_; |
| 129 | HWND hdlg_; |
| 130 | }; |
| 131 | |
| 132 | /////////////////////////////////////////////////////////////////////////////// |
| 133 | // Win32Thread. Automatically pumps Windows messages. |
| 134 | /////////////////////////////////////////////////////////////////////////////// |
| 135 | |
| 136 | class Win32Thread : public Thread { |
| 137 | public: |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 138 | explicit Win32Thread(SocketServer* ss); |
| 139 | ~Win32Thread() override; |
| 140 | |
| 141 | void Run() override; |
| 142 | void Quit() override; |
| 143 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 144 | private: |
| 145 | DWORD id_; |
| 146 | }; |
| 147 | |
| 148 | /////////////////////////////////////////////////////////////////////////////// |
| 149 | |
| 150 | } // namespace rtc |
| 151 | |
| 152 | #endif // WEBRTC_WIN |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 153 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 154 | #endif // RTC_BASE_WIN32_SOCKET_SERVER_H_ |