00001 #ifndef __SOCKET_IP_H__
00002 #define __SOCKET_IP_H__
00003
00004 #include "pandabase.h"
00005 #include "socket_portable.h"
00006 #include "socket_address.h"
00007 #include "typedObject.h"
00008
00009
00010 class Socket_TCP;
00011 class Socket_UDP;
00012 class Socket_TCP_Listen;
00013 class Socket_UDP_Incoming;
00014 class Socket_UDP_Outgoing;
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class EXPCL_PANDA_NATIVENET Socket_IP : public TypedObject {
00035 public:
00036 PUBLISHED:
00037
00038 inline Socket_IP();
00039 inline Socket_IP(SOCKET in);
00040 virtual ~Socket_IP();
00041
00042 inline void Close();
00043 inline static int GetLastError();
00044 inline int SetNonBlocking();
00045 inline int SetBlocking();
00046 inline bool SetReuseAddress(bool flag = true);
00047 inline bool Active();
00048 inline int SetRecvBufferSize(int size);
00049 inline void SetSocket(SOCKET ins);
00050 inline SOCKET GetSocket();
00051 inline SOCKET GetSocket() const;
00052 inline Socket_Address GetPeerName(void) const;
00053
00054
00055 inline static int InitNetworkDriver() { return init_network(); };
00056
00057 public:
00058 private:
00059 inline bool ErrorClose();
00060
00061 SOCKET _socket;
00062
00063 friend class Socket_TCP;
00064 friend class Socket_UDP;
00065 friend class Socket_TCP_Listen;
00066 friend class Socket_UDP_Incoming;
00067 friend class Socket_UDP_Outgoing;
00068 friend class Socket_TCP_SSL;
00069
00070 public:
00071 static TypeHandle get_class_type() {
00072 return _type_handle;
00073 }
00074 static void init_type() {
00075 TypedObject::init_type();
00076 register_type(_type_handle, "Socket_IP",
00077 TypedObject::get_class_type());
00078 }
00079 virtual TypeHandle get_type() const {
00080 return get_class_type();
00081 }
00082 virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00083
00084 private:
00085 static TypeHandle _type_handle;
00086 };
00087
00088
00089
00090
00091
00092
00093 inline bool Socket_IP::ErrorClose()
00094 {
00095 if (Active())
00096 DO_CLOSE(_socket);
00097 _socket = BAD_SOCKET;
00098 return false;
00099 }
00100
00101
00102
00103
00104
00105 inline bool Socket_IP::Active()
00106 {
00107 return (_socket != BAD_SOCKET);
00108 }
00109
00110
00111
00112
00113
00114 inline Socket_IP::Socket_IP()
00115 {
00116 _socket = BAD_SOCKET;
00117 }
00118
00119
00120
00121
00122
00123 inline Socket_IP::Socket_IP(SOCKET ins)
00124 {
00125 _socket = ins;
00126 }
00127
00128
00129
00130
00131
00132 inline Socket_IP::~Socket_IP()
00133 {
00134 Close();
00135 }
00136
00137
00138
00139
00140
00141 inline void Socket_IP::Close()
00142 {
00143 if (Active())
00144 DO_CLOSE(_socket);
00145 _socket = BAD_SOCKET;
00146 }
00147
00148
00149
00150
00151
00152 inline int Socket_IP::GetLastError()
00153 {
00154 return GETERROR();
00155 }
00156
00157
00158
00159
00160
00161 inline void Socket_IP::SetSocket(SOCKET ins)
00162 {
00163 Close();
00164 _socket = ins;
00165 }
00166
00167
00168
00169
00170
00171 int Socket_IP::SetRecvBufferSize(int insize)
00172 {
00173 if (setsockopt(_socket, (int) SOL_SOCKET, (int) SO_RCVBUF, (char *) &insize, sizeof(int)))
00174 return BASIC_ERROR;
00175
00176 return ALL_OK;
00177 }
00178
00179
00180
00181
00182
00183 inline int Socket_IP::SetNonBlocking()
00184 {
00185 #ifdef BSDBLOCK
00186
00187 int flags = fcntl(_socket, F_GETFL, 0);
00188 flags = flags | O_NONBLOCK;
00189 fcntl(_socket, F_SETFL, flags);
00190 return ALL_OK;
00191 #else
00192 unsigned long val = LOCAL_NONBLOCK;
00193 unsigned lanswer = 0;
00194 lanswer = SOCKIOCTL(_socket, LOCAL_FL_SET, &val);
00195 if (lanswer != 0)
00196 return BASIC_ERROR;
00197 return ALL_OK;
00198
00199 #endif
00200 }
00201
00202
00203
00204
00205
00206
00207 inline int Socket_IP::SetBlocking()
00208 {
00209 #ifdef BSDBLOCK
00210 int flags = fcntl(_socket, F_GETFL, 0);
00211 flags &= ~O_NONBLOCK;
00212 fcntl(_socket, F_SETFL, flags);
00213 return ALL_OK;
00214 #else
00215 unsigned long val = 0;
00216 unsigned lanswer = 0;
00217 lanswer = SOCKIOCTL(_socket, LOCAL_FL_SET, &val);
00218 if (lanswer != 0)
00219 return BASIC_ERROR;
00220 return ALL_OK;
00221 #endif
00222 }
00223
00224
00225
00226
00227
00228 inline bool Socket_IP::SetReuseAddress(bool flag)
00229 {
00230 int bOption = flag;
00231 if (setsockopt(_socket, SOL_SOCKET, SO_REUSEADDR, (const char *)&bOption, sizeof(bOption)) != 0)
00232 return false;
00233 return true;
00234 }
00235
00236
00237
00238
00239
00240 inline SOCKET Socket_IP::GetSocket()
00241 {
00242 return _socket;
00243 }
00244
00245
00246
00247
00248
00249 inline SOCKET Socket_IP::GetSocket() const
00250 {
00251 return _socket;
00252 }
00253
00254
00255
00256
00257 inline Socket_Address Socket_IP::GetPeerName(void) const
00258 {
00259 sockaddr_in name;
00260 socklen_t name_len = sizeof(name);
00261 memset(&name,0,name_len);
00262
00263 getpeername(_socket,(sockaddr * )&name,&name_len);
00264 return Socket_Address(name);
00265 }
00266
00267
00268 #endif //__SOCKET_IP_H__