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