mudgangster

Tiny, scriptable MUD client
Log | Files | Refs | README

TracySocket.hpp (2597B)


      1 #ifndef __TRACYSOCKET_HPP__
      2 #define __TRACYSOCKET_HPP__
      3 
      4 #include <functional>
      5 
      6 struct sockaddr;
      7 
      8 namespace tracy
      9 {
     10 
     11 #ifdef _WIN32
     12 void InitWinSock();
     13 #endif
     14 
     15 class Socket
     16 {
     17     enum { BufSize = 128 * 1024 };
     18 
     19 public:
     20     Socket();
     21     Socket( int sock );
     22     ~Socket();
     23 
     24     bool Connect( const char* addr, int port );
     25     void Close();
     26 
     27     int Send( const void* buf, int len );
     28     int GetSendBufSize();
     29 
     30     bool Read( void* buf, int len, int timeout, std::function<bool()> exitCb );
     31     bool ReadRaw( void* buf, int len, int timeout );
     32     bool HasData();
     33 
     34     Socket( const Socket& ) = delete;
     35     Socket( Socket&& ) = delete;
     36     Socket& operator=( const Socket& ) = delete;
     37     Socket& operator=( Socket&& ) = delete;
     38 
     39 private:
     40     int RecvBuffered( void* buf, int len, int timeout );
     41     int Recv( void* buf, int len, int timeout );
     42 
     43     char* m_buf;
     44     char* m_bufPtr;
     45     int m_sock;
     46     int m_bufLeft;
     47 };
     48 
     49 class ListenSocket
     50 {
     51 public:
     52     ListenSocket();
     53     ~ListenSocket();
     54 
     55     bool Listen( int port, int backlog );
     56     Socket* Accept();
     57     void Close();
     58 
     59     ListenSocket( const ListenSocket& ) = delete;
     60     ListenSocket( ListenSocket&& ) = delete;
     61     ListenSocket& operator=( const ListenSocket& ) = delete;
     62     ListenSocket& operator=( ListenSocket&& ) = delete;
     63 
     64 private:
     65     int m_sock;
     66 };
     67 
     68 class UdpBroadcast
     69 {
     70 public:
     71     UdpBroadcast();
     72     ~UdpBroadcast();
     73 
     74     bool Open( const char* addr, int port );
     75     void Close();
     76 
     77     int Send( int port, const void* data, int len );
     78 
     79     UdpBroadcast( const UdpBroadcast& ) = delete;
     80     UdpBroadcast( UdpBroadcast&& ) = delete;
     81     UdpBroadcast& operator=( const UdpBroadcast& ) = delete;
     82     UdpBroadcast& operator=( UdpBroadcast&& ) = delete;
     83 
     84 private:
     85     int m_sock;
     86 };
     87 
     88 class IpAddress
     89 {
     90 public:
     91     IpAddress();
     92     ~IpAddress();
     93 
     94     void Set( const struct sockaddr& addr );
     95 
     96     uint32_t GetNumber() const { return m_number; }
     97     const char* GetText() const { return m_text; }
     98 
     99     IpAddress( const IpAddress& ) = delete;
    100     IpAddress( IpAddress&& ) = delete;
    101     IpAddress& operator=( const IpAddress& ) = delete;
    102     IpAddress& operator=( IpAddress&& ) = delete;
    103 
    104 private:
    105     uint32_t m_number;
    106     char m_text[17];
    107 };
    108 
    109 class UdpListen
    110 {
    111 public:
    112     UdpListen();
    113     ~UdpListen();
    114 
    115     bool Listen( int port );
    116     void Close();
    117 
    118     const char* Read( size_t& len, IpAddress& addr );
    119 
    120     UdpListen( const UdpListen& ) = delete;
    121     UdpListen( UdpListen&& ) = delete;
    122     UdpListen& operator=( const UdpListen& ) = delete;
    123     UdpListen& operator=( UdpListen&& ) = delete;
    124 
    125 private:
    126     int m_sock;
    127 };
    128 
    129 }
    130 
    131 #endif