blob: 28bee6af067653f1b85b18f6d489951106c92a68 [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
11#ifndef WEBRTC_BASE_WIN32SOCKETSERVER_H_
12#define WEBRTC_BASE_WIN32SOCKETSERVER_H_
13
14#if defined(WEBRTC_WIN)
15#include "webrtc/base/asyncsocket.h"
16#include "webrtc/base/criticalsection.h"
17#include "webrtc/base/messagequeue.h"
18#include "webrtc/base/socketserver.h"
19#include "webrtc/base/socketfactory.h"
20#include "webrtc/base/socket.h"
21#include "webrtc/base/thread.h"
22#include "webrtc/base/win32window.h"
23
24namespace 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);
Stefan Holmer9131efd2016-05-23 18:19:26 +020047 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);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000052 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;
Peter Boström0c4e06b2015-10-07 12:23:21 +020058 virtual int EstimateMTU(uint16_t* mtu);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000059 virtual int GetOption(Option opt, int* value);
60 virtual int SetOption(Option opt, int value);
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)
Peter Boström0c4e06b2015-10-07 12:23:21 +020078 uint32_t connect_time_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000079 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
94class Win32SocketServer : public SocketServer {
95 public:
96 explicit Win32SocketServer(MessageQueue* message_queue);
97 virtual ~Win32SocketServer();
98
99 void set_modeless_dialog(HWND hdlg) {
100 hdlg_ = hdlg;
101 }
102
103 // SocketServer Interface
104 virtual Socket* CreateSocket(int type);
105 virtual Socket* CreateSocket(int family, int type);
106
107 virtual AsyncSocket* CreateAsyncSocket(int type);
108 virtual AsyncSocket* CreateAsyncSocket(int family, int type);
109
110 virtual void SetMessageQueue(MessageQueue* queue);
111 virtual bool Wait(int cms, bool process_io);
112 virtual void WakeUp();
113
114 void Pump();
115
116 HWND handle() { return wnd_.handle(); }
117
118 private:
119 class MessageWindow : public Win32Window {
120 public:
121 explicit MessageWindow(Win32SocketServer* ss) : ss_(ss) {}
122 private:
123 virtual bool OnMessage(UINT msg, WPARAM wp, LPARAM lp, LRESULT& result);
124 Win32SocketServer* ss_;
125 };
126
127 static const TCHAR kWindowName[];
128 MessageQueue *message_queue_;
129 MessageWindow wnd_;
130 CriticalSection cs_;
131 bool posted_;
132 HWND hdlg_;
133};
134
135///////////////////////////////////////////////////////////////////////////////
136// Win32Thread. Automatically pumps Windows messages.
137///////////////////////////////////////////////////////////////////////////////
138
139class Win32Thread : public Thread {
140 public:
141 Win32Thread() : ss_(this), id_(0) {
142 set_socketserver(&ss_);
143 }
144 virtual ~Win32Thread() {
145 Stop();
146 set_socketserver(NULL);
147 }
148 virtual void Run() {
149 id_ = GetCurrentThreadId();
150 Thread::Run();
151 id_ = 0;
152 }
153 virtual void Quit() {
154 PostThreadMessage(id_, WM_QUIT, 0, 0);
155 }
156 private:
157 Win32SocketServer ss_;
158 DWORD id_;
159};
160
161///////////////////////////////////////////////////////////////////////////////
162
163} // namespace rtc
164
Honghai Zhang82d78622016-05-06 11:29:15 -0700165#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000166
167#endif // WEBRTC_BASE_WIN32SOCKETSERVER_H_