1 /*
2  * libjingle
3  * Copyright 2004--2005, Google Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
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.
15  *
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.
26  */
27 
28 #ifndef TALK_BASE_WIN32SOCKETSERVER_H_
29 #define TALK_BASE_WIN32SOCKETSERVER_H_
30 
31 #ifdef WIN32
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"
40 
41 namespace talk_base {
42 
43 ///////////////////////////////////////////////////////////////////////////////
44 // Win32Socket
45 ///////////////////////////////////////////////////////////////////////////////
46 
47 class Win32Socket : public AsyncSocket {
48  public:
49   Win32Socket();
50   virtual ~Win32Socket();
51 
52   bool CreateT(int family, int type);
53 
54   int Attach(SOCKET s);
55   void SetTimeout(int ms);
56 
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);
68   virtual int Close();
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);
75 
76  private:
77   bool SetAsync(int events);
78   int DoConnect(const SocketAddress& addr);
79   bool HandleClosed(int close_error);
80   void PostClosed();
81   void UpdateLastError();
82   static int TranslateOption(Option opt, int* slevel, int* sopt);
83 
84   void OnSocketNotify(SOCKET socket, int event, int error);
85   void OnDnsNotify(HANDLE task, int error);
86 
87   SOCKET socket_;
88   int error_;
89   ConnState state_;
90   SocketAddress addr_;         // address that we connected to (see DoConnect)
91   uint32 connect_time_;
92   bool closing_;
93   int close_error_;
94 
95   class EventSink;
96   friend class EventSink;
97   EventSink * sink_;
98 
99   struct DnsLookup;
100   DnsLookup * dns_;
101 };
102 
103 ///////////////////////////////////////////////////////////////////////////////
104 // Win32SocketServer
105 ///////////////////////////////////////////////////////////////////////////////
106 
107 class Win32SocketServer : public SocketServer {
108  public:
109   explicit Win32SocketServer(MessageQueue* message_queue);
110   virtual ~Win32SocketServer();
111 
set_modeless_dialog(HWND hdlg)112   void set_modeless_dialog(HWND hdlg) {
113     hdlg_ = hdlg;
114   }
115 
116   // SocketServer Interface
117   virtual Socket* CreateSocket(int type);
118   virtual Socket* CreateSocket(int family, int type);
119 
120   virtual AsyncSocket* CreateAsyncSocket(int type);
121   virtual AsyncSocket* CreateAsyncSocket(int family, int type);
122 
123   virtual void SetMessageQueue(MessageQueue* queue);
124   virtual bool Wait(int cms, bool process_io);
125   virtual void WakeUp();
126 
127   void Pump();
128 
handle()129   HWND handle() { return wnd_.handle(); }
130 
131  private:
132   class MessageWindow : public Win32Window {
133    public:
MessageWindow(Win32SocketServer * ss)134     explicit MessageWindow(Win32SocketServer* ss) : ss_(ss) {}
135    private:
136     virtual bool OnMessage(UINT msg, WPARAM wp, LPARAM lp, LRESULT& result);
137     Win32SocketServer* ss_;
138   };
139 
140   static const TCHAR kWindowName[];
141   MessageQueue *message_queue_;
142   MessageWindow wnd_;
143   CriticalSection cs_;
144   bool posted_;
145   HWND hdlg_;
146 };
147 
148 ///////////////////////////////////////////////////////////////////////////////
149 // Win32Thread. Automatically pumps Windows messages.
150 ///////////////////////////////////////////////////////////////////////////////
151 
152 class Win32Thread : public Thread {
153  public:
Win32Thread()154   Win32Thread() : ss_(this), id_(0) {
155     set_socketserver(&ss_);
156   }
~Win32Thread()157   virtual ~Win32Thread() {
158     set_socketserver(NULL);
159   }
Run()160   virtual void Run() {
161     id_ = GetCurrentThreadId();
162     Thread::Run();
163     id_ = 0;
164   }
Quit()165   virtual void Quit() {
166     PostThreadMessage(id_, WM_QUIT, 0, 0);
167   }
168  private:
169   Win32SocketServer ss_;
170   DWORD id_;
171 };
172 
173 ///////////////////////////////////////////////////////////////////////////////
174 
175 }  // namespace talk_base
176 
177 #endif  // WIN32
178 
179 #endif  // TALK_BASE_WIN32SOCKETSERVER_H_
180