3
* Copyright 2004--2005, Google Inc.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
8
* 1. Redistributions of source code must retain the above copyright notice,
9
* this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
13
* 3. The name of the author may not be used to endorse or promote products
14
* derived from this software without specific prior written permission.
16
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
#ifndef TALK_BASE_WIN32SOCKETSERVER_H_
29
#define TALK_BASE_WIN32SOCKETSERVER_H_
32
#include "talk/base/asyncsocket.h"
33
#include "talk/base/criticalsection.h"
34
#include "talk/base/messagequeue.h"
35
#include "talk/base/socketserver.h"
36
#include "talk/base/socketfactory.h"
37
#include "talk/base/socket.h"
38
#include "talk/base/thread.h"
39
#include "talk/base/win32window.h"
43
///////////////////////////////////////////////////////////////////////////////
45
///////////////////////////////////////////////////////////////////////////////
47
class Win32Socket : public AsyncSocket {
50
virtual ~Win32Socket();
52
bool CreateT(int family, int type);
55
void SetTimeout(int ms);
57
// AsyncSocket Interface
58
virtual SocketAddress GetLocalAddress() const;
59
virtual SocketAddress GetRemoteAddress() const;
60
virtual int Bind(const SocketAddress& addr);
61
virtual int Connect(const SocketAddress& addr);
62
virtual int Send(const void *buffer, size_t length);
63
virtual int SendTo(const void *buffer, size_t length, const SocketAddress& addr);
64
virtual int Recv(void *buffer, size_t length);
65
virtual int RecvFrom(void *buffer, size_t length, SocketAddress *out_addr);
66
virtual int Listen(int backlog);
67
virtual Win32Socket *Accept(SocketAddress *out_addr);
69
virtual int GetError() const;
70
virtual void SetError(int error);
71
virtual ConnState GetState() const;
72
virtual int EstimateMTU(uint16* mtu);
73
virtual int GetOption(Option opt, int* value);
74
virtual int SetOption(Option opt, int value);
77
bool SetAsync(int events);
78
int DoConnect(const SocketAddress& addr);
79
bool HandleClosed(int close_error);
81
void UpdateLastError();
82
static int TranslateOption(Option opt, int* slevel, int* sopt);
84
void OnSocketNotify(SOCKET socket, int event, int error);
85
void OnDnsNotify(HANDLE task, int error);
90
SocketAddress addr_; // address that we connected to (see DoConnect)
96
friend class EventSink;
103
///////////////////////////////////////////////////////////////////////////////
105
///////////////////////////////////////////////////////////////////////////////
107
class Win32SocketServer : public SocketServer {
109
explicit Win32SocketServer(MessageQueue* message_queue);
110
virtual ~Win32SocketServer();
112
void set_modeless_dialog(HWND hdlg) {
116
// SocketServer Interface
117
virtual Socket* CreateSocket(int type);
118
virtual Socket* CreateSocket(int family, int type);
120
virtual AsyncSocket* CreateAsyncSocket(int type);
121
virtual AsyncSocket* CreateAsyncSocket(int family, int type);
123
virtual void SetMessageQueue(MessageQueue* queue);
124
virtual bool Wait(int cms, bool process_io);
125
virtual void WakeUp();
129
HWND handle() { return wnd_.handle(); }
132
class MessageWindow : public Win32Window {
134
explicit MessageWindow(Win32SocketServer* ss) : ss_(ss) {}
136
virtual bool OnMessage(UINT msg, WPARAM wp, LPARAM lp, LRESULT& result);
137
Win32SocketServer* ss_;
140
static const TCHAR kWindowName[];
141
MessageQueue *message_queue_;
148
///////////////////////////////////////////////////////////////////////////////
149
// Win32Thread. Automatically pumps Windows messages.
150
///////////////////////////////////////////////////////////////////////////////
152
class Win32Thread : public Thread {
154
Win32Thread() : ss_(this), id_(0) {
155
set_socketserver(&ss_);
157
virtual ~Win32Thread() {
158
set_socketserver(NULL);
161
id_ = GetCurrentThreadId();
165
virtual void Quit() {
166
PostThreadMessage(id_, WM_QUIT, 0, 0);
169
Win32SocketServer ss_;
173
///////////////////////////////////////////////////////////////////////////////
175
} // namespace talk_base
179
#endif // TALK_BASE_WIN32SOCKETSERVER_H_