Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // TCP/IP server that handles IO asynchronously in the specified MessageLoop.
      6 // These objects are NOT thread safe.  They use WSAEVENT handles to monitor
      7 // activity in a given MessageLoop.  This means that callbacks will
      8 // happen in that loop's thread always and that all other methods (including
      9 // constructors and destructors) should also be called from the same thread.
     10 
     11 #ifndef NET_BASE_LISTEN_SOCKET_H_
     12 #define NET_BASE_LISTEN_SOCKET_H_
     13 
     14 #include "build/build_config.h"
     15 
     16 #if defined(OS_WIN)
     17 #include <winsock2.h>
     18 #endif
     19 #include <string>
     20 #if defined(OS_WIN)
     21 #include "base/object_watcher.h"
     22 #elif defined(OS_POSIX)
     23 #include "base/message_loop.h"
     24 #endif
     25 
     26 #include "base/basictypes.h"
     27 #include "base/ref_counted.h"
     28 
     29 #if defined(OS_POSIX)
     30 struct event;  // From libevent
     31 typedef int SOCKET;
     32 #endif
     33 
     34 // Implements a raw socket interface
     35 class ListenSocket : public base::RefCountedThreadSafe<ListenSocket>,
     36 #if defined(OS_WIN)
     37                      public base::ObjectWatcher::Delegate
     38 #elif defined(OS_POSIX)
     39                      public MessageLoopForIO::Watcher
     40 #endif
     41 {
     42  public:
     43   // TODO(erikkay): this delegate should really be split into two parts
     44   // to split up the listener from the connected socket.  Perhaps this class
     45   // should be split up similarly.
     46   class ListenSocketDelegate {
     47    public:
     48     virtual ~ListenSocketDelegate() {}
     49 
     50     // server is the original listening Socket, connection is the new
     51     // Socket that was created.  Ownership of connection is transferred
     52     // to the delegate with this call.
     53     virtual void DidAccept(ListenSocket *server, ListenSocket *connection) = 0;
     54     virtual void DidRead(ListenSocket *connection, const std::string& data) = 0;
     55     virtual void DidClose(ListenSocket *sock) = 0;
     56   };
     57 
     58   // Listen on port for the specified IP address.  Use 127.0.0.1 to only
     59   // accept local connections.
     60   static ListenSocket* Listen(std::string ip, int port,
     61                               ListenSocketDelegate* del);
     62 
     63   // Send data to the socket.
     64   void Send(const char* bytes, int len, bool append_linefeed = false);
     65   void Send(const std::string& str, bool append_linefeed = false);
     66 
     67   // NOTE: This is for unit test use only!
     68   // Pause/Resume calling Read().  Note that ResumeReads() will also call
     69   // Read() if there is anything to read.
     70   void PauseReads();
     71   void ResumeReads();
     72 
     73  protected:
     74   friend class base::RefCountedThreadSafe<ListenSocket>;
     75 
     76   static const SOCKET kInvalidSocket;
     77   static const int kSocketError;
     78 
     79   ListenSocket(SOCKET s, ListenSocketDelegate* del);
     80   virtual ~ListenSocket();
     81   static SOCKET Listen(std::string ip, int port);
     82   // if valid, returned SOCKET is non-blocking
     83   static SOCKET Accept(SOCKET s);
     84 
     85   virtual void SendInternal(const char* bytes, int len);
     86 
     87   virtual void Listen();
     88   virtual void Accept();
     89   virtual void Read();
     90   virtual void Close();
     91   virtual void CloseSocket(SOCKET s);
     92 
     93   enum WaitState {
     94     NOT_WAITING      = 0,
     95     WAITING_ACCEPT   = 1,
     96     WAITING_READ     = 3,
     97     WAITING_CLOSE    = 4
     98   };
     99   // Pass any value in case of Windows, because in Windows
    100   // we are not using state.
    101   void WatchSocket(WaitState state);
    102   void UnwatchSocket();
    103 
    104 #if defined(OS_WIN)
    105   // ObjectWatcher delegate
    106   virtual void OnObjectSignaled(HANDLE object);
    107   base::ObjectWatcher watcher_;
    108   HANDLE socket_event_;
    109 #elif defined(OS_POSIX)
    110   WaitState wait_state_;
    111   // The socket's libevent wrapper
    112   MessageLoopForIO::FileDescriptorWatcher watcher_;
    113   // Called by MessagePumpLibevent when the socket is ready to do I/O
    114   void OnFileCanReadWithoutBlocking(int fd);
    115   void OnFileCanWriteWithoutBlocking(int fd);
    116 #endif
    117 
    118   SOCKET socket_;
    119   ListenSocketDelegate *socket_delegate_;
    120 
    121  private:
    122   bool reads_paused_;
    123   bool has_pending_reads_;
    124 
    125   DISALLOW_COPY_AND_ASSIGN(ListenSocket);
    126 };
    127 
    128 #endif  // NET_BASE_LISTEN_SOCKET_H_
    129