Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007 #pragma once
00008
00009 #include <vector>
00010
00011 namespace rp{ namespace net {
00012
00013 class _single_thread SocketAddress
00014 {
00015
00016 public:
00017 enum address_type_t {
00018 ADDRESS_TYPE_UNSPEC = 0,
00019 ADDRESS_TYPE_INET = 1,
00020 ADDRESS_TYPE_INET6 = 2,
00021 };
00022
00023 public:
00024
00025
00026
00027 SocketAddress();
00028 SocketAddress(const char * addrString, int port, address_type_t = ADDRESS_TYPE_INET);
00029
00030 SocketAddress(void * platform_data);
00031 SocketAddress(const SocketAddress &);
00032
00033 SocketAddress & operator = (const SocketAddress &);
00034
00035 virtual ~SocketAddress();
00036
00037 virtual int getPort() const;
00038 virtual u_result setPort(int port);
00039
00040 virtual u_result setAddressFromString(const char * address_string, address_type_t = ADDRESS_TYPE_INET);
00041 virtual u_result getAddressAsString(char * buffer, size_t buffersize) const;
00042
00043 virtual address_type_t getAddressType() const;
00044
00045 virtual u_result getRawAddress(_u8 * buffer, size_t bufferSize) const;
00046
00047 const void * getPlatformData() const {
00048 return _platform_data;
00049 }
00050
00051 virtual void setLoopbackAddress(address_type_t = ADDRESS_TYPE_INET);
00052 virtual void setBroadcastAddressIPv4();
00053 virtual void setAnyAddress(address_type_t = ADDRESS_TYPE_INET);
00054
00055 public:
00056 static size_t LoopUpHostName(const char * hostname, const char * sevicename, std::vector<SocketAddress> &addresspool , bool performDNS = true, address_type_t = ADDRESS_TYPE_INET);
00057
00058 protected:
00059 void * _platform_data;
00060 };
00061
00062
00063
00064 class SocketBase
00065 {
00066 public:
00067 enum socket_family_t {
00068 SOCKET_FAMILY_INET = 0,
00069 SOCKET_FAMILY_INET6 = 1,
00070 SOCKET_FAMILY_RAW = 2,
00071 };
00072
00073
00074 enum socket_direction_mask {
00075 SOCKET_DIR_RD = 0x1,
00076 SOCKET_DIR_WR = 0x2,
00077 SOCKET_DIR_BOTH = (SOCKET_DIR_RD | SOCKET_DIR_WR),
00078 };
00079
00080 enum {
00081 DEFAULT_SOCKET_TIMEOUT = 10000,
00082 };
00083
00084 virtual ~SocketBase() {}
00085 virtual void dispose() = 0;
00086 virtual u_result bind(const SocketAddress & ) = 0;
00087
00088 virtual u_result getLocalAddress(SocketAddress & ) = 0;
00089 virtual u_result setTimeout(_u32 timeout, socket_direction_mask msk = SOCKET_DIR_BOTH) = 0;
00090
00091 virtual u_result waitforSent(_u32 timeout = DEFAULT_SOCKET_TIMEOUT) = 0;
00092 virtual u_result waitforData(_u32 timeout = DEFAULT_SOCKET_TIMEOUT) = 0;
00093 protected:
00094 SocketBase() {}
00095 };
00096
00097
00098 class _single_thread StreamSocket : public SocketBase
00099 {
00100 public:
00101
00102 enum {
00103 MAX_BACKLOG = 128,
00104 };
00105
00106 static StreamSocket * CreateSocket(socket_family_t family = SOCKET_FAMILY_INET);
00107
00108 virtual u_result connect(const SocketAddress & pairAddress) = 0;
00109
00110 virtual u_result listen(int backlog = MAX_BACKLOG) = 0;
00111 virtual StreamSocket * accept(SocketAddress * pairAddress = NULL) = 0;
00112 virtual u_result waitforIncomingConnection(_u32 timeout = DEFAULT_SOCKET_TIMEOUT) = 0;
00113
00114 virtual u_result send(const void * buffer, size_t len) = 0;
00115
00116 virtual u_result recv(void *buf, size_t len, size_t & recv_len) = 0;
00117
00118 virtual u_result getPeerAddress(SocketAddress & ) = 0;
00119
00120 virtual u_result shutdown(socket_direction_mask mask) = 0;
00121
00122 virtual u_result enableKeepAlive(bool enable = true) = 0;
00123
00124 virtual u_result enableNoDelay(bool enable = true) = 0;
00125
00126 protected:
00127 virtual ~StreamSocket() {}
00128 StreamSocket() {}
00129 };
00130
00131 class _single_thread DGramSocket: public SocketBase
00132 {
00133
00134 public:
00135
00136 static DGramSocket * CreateSocket(socket_family_t family = SOCKET_FAMILY_INET);
00137
00138
00139
00140 virtual u_result sendTo(const SocketAddress & target, const void * buffer, size_t len) = 0;
00141
00142 virtual u_result recvFrom(void *buf, size_t len, size_t & recv_len, SocketAddress * sourceAddr = NULL) = 0;
00143
00144
00145 protected:
00146 virtual ~DGramSocket() {}
00147
00148 DGramSocket() {}
00149 };
00150
00151 }}