Home | History | Annotate | Download | only in base
      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 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 *pv, size_t cb);
     63   virtual int SendTo(const void *pv, size_t cb, const SocketAddress& addr);
     64   virtual int Recv(void *pv, size_t cb);
     65   virtual int RecvFrom(void *pv, size_t cb, SocketAddress *paddr);
     66   virtual int Listen(int backlog);
     67   virtual Win32Socket *Accept(SocketAddress *paddr);
     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 
    112   // SocketServer Interface
    113   virtual Socket* CreateSocket(int type);
    114   virtual AsyncSocket* CreateAsyncSocket(int type);
    115   virtual void SetMessageQueue(MessageQueue* queue);
    116   virtual bool Wait(int cms, bool process_io);
    117   virtual void WakeUp();
    118 
    119   void Pump();
    120 
    121   HWND handle() { return wnd_.handle(); }
    122 
    123  private:
    124   class MessageWindow : public Win32Window {
    125    public:
    126     explicit MessageWindow(Win32SocketServer* ss) : ss_(ss) {}
    127    private:
    128     virtual bool OnMessage(UINT msg, WPARAM wp, LPARAM lp, LRESULT& result);
    129     Win32SocketServer* ss_;
    130   };
    131 
    132   static const TCHAR kWindowName[];
    133   MessageQueue *message_queue_;
    134   MessageWindow wnd_;
    135   CriticalSection cs_;
    136   bool posted_;
    137 };
    138 
    139 ///////////////////////////////////////////////////////////////////////////////
    140 // Win32Thread. Automatically pumps Windows messages.
    141 ///////////////////////////////////////////////////////////////////////////////
    142 
    143 class Win32Thread : public Thread {
    144  public:
    145   Win32Thread() : ss_(this), id_(0) {
    146     set_socketserver(&ss_);
    147   }
    148   virtual ~Win32Thread() {
    149     set_socketserver(NULL);
    150   }
    151   virtual void Run() {
    152     MSG msg;
    153     id_ = GetCurrentThreadId();
    154     while (GetMessage(&msg, NULL, 0, 0)) {
    155       TranslateMessage(&msg);
    156       DispatchMessage(&msg);
    157     }
    158     id_ = 0;
    159   }
    160   virtual void Quit() {
    161     PostThreadMessage(id_, WM_QUIT, 0, 0);
    162   }
    163  private:
    164   Win32SocketServer ss_;
    165   DWORD id_;
    166 };
    167 
    168 ///////////////////////////////////////////////////////////////////////////////
    169 
    170 }  // namespace talk_base
    171 
    172 #endif  // WIN32
    173 
    174 #endif  // TALK_BASE_WIN32SOCKETSERVER_H_
    175