• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   void CreateSink();
78   bool SetAsync(int events);
79   int DoConnect(const SocketAddress& addr);
80   bool HandleClosed(int close_error);
81   void PostClosed();
82   void UpdateLastError();
83   static int TranslateOption(Option opt, int* slevel, int* sopt);
84 
85   void OnSocketNotify(SOCKET socket, int event, int error);
86   void OnDnsNotify(HANDLE task, int error);
87 
88   SOCKET socket_;
89   int error_;
90   ConnState state_;
91   SocketAddress addr_;         // address that we connected to (see DoConnect)
92   uint32 connect_time_;
93   bool closing_;
94   int close_error_;
95 
96   class EventSink;
97   friend class EventSink;
98   EventSink * sink_;
99 
100   struct DnsLookup;
101   DnsLookup * dns_;
102 };
103 
104 ///////////////////////////////////////////////////////////////////////////////
105 // Win32SocketServer
106 ///////////////////////////////////////////////////////////////////////////////
107 
108 class Win32SocketServer : public SocketServer {
109  public:
110   explicit Win32SocketServer(MessageQueue* message_queue);
111   virtual ~Win32SocketServer();
112 
set_modeless_dialog(HWND hdlg)113   void set_modeless_dialog(HWND hdlg) {
114     hdlg_ = hdlg;
115   }
116 
117   // SocketServer Interface
118   virtual Socket* CreateSocket(int type);
119   virtual Socket* CreateSocket(int family, int type);
120 
121   virtual AsyncSocket* CreateAsyncSocket(int type);
122   virtual AsyncSocket* CreateAsyncSocket(int family, int type);
123 
124   virtual void SetMessageQueue(MessageQueue* queue);
125   virtual bool Wait(int cms, bool process_io);
126   virtual void WakeUp();
127 
128   void Pump();
129 
handle()130   HWND handle() { return wnd_.handle(); }
131 
132  private:
133   class MessageWindow : public Win32Window {
134    public:
MessageWindow(Win32SocketServer * ss)135     explicit MessageWindow(Win32SocketServer* ss) : ss_(ss) {}
136    private:
137     virtual bool OnMessage(UINT msg, WPARAM wp, LPARAM lp, LRESULT& result);
138     Win32SocketServer* ss_;
139   };
140 
141   static const TCHAR kWindowName[];
142   MessageQueue *message_queue_;
143   MessageWindow wnd_;
144   CriticalSection cs_;
145   bool posted_;
146   HWND hdlg_;
147 };
148 
149 ///////////////////////////////////////////////////////////////////////////////
150 // Win32Thread. Automatically pumps Windows messages.
151 ///////////////////////////////////////////////////////////////////////////////
152 
153 class Win32Thread : public Thread {
154  public:
Win32Thread()155   Win32Thread() : ss_(this), id_(0) {
156     set_socketserver(&ss_);
157   }
~Win32Thread()158   virtual ~Win32Thread() {
159     Stop();
160     set_socketserver(NULL);
161   }
Run()162   virtual void Run() {
163     id_ = GetCurrentThreadId();
164     Thread::Run();
165     id_ = 0;
166   }
Quit()167   virtual void Quit() {
168     PostThreadMessage(id_, WM_QUIT, 0, 0);
169   }
170  private:
171   Win32SocketServer ss_;
172   DWORD id_;
173 };
174 
175 ///////////////////////////////////////////////////////////////////////////////
176 
177 }  // namespace talk_base
178 
179 #endif  // WIN32
180 
181 #endif  // TALK_BASE_WIN32SOCKETSERVER_H_
182