Panda3D
|
00001 #ifndef __SOCKET_UDP_OUTGOING_H__ 00002 #define __SOCKET_UDP_OUTGOING_H__ 00003 00004 ///////////////////////////////////////////////////////////////////// 00005 // Class : Socket_UDP_Outgoing 00006 // 00007 // Description : Base functionality for a UDP Sending Socket 00008 // 00009 // 00010 ///////////////////////////////////////////////////////////////////// 00011 class EXPCL_PANDA_NATIVENET Socket_UDP_Outgoing : public Socket_IP 00012 { 00013 public: 00014 PUBLISHED: 00015 inline Socket_UDP_Outgoing() { } 00016 00017 // use this interface for a tagreted UDP connection 00018 inline bool InitToAddress(const Socket_Address & address); 00019 public: 00020 inline bool Send(const char * data, int len); 00021 PUBLISHED: 00022 inline bool Send(const string &data); 00023 // use this interface for a none tagreted UDP connection 00024 inline bool InitNoAddress(); 00025 public: 00026 inline bool SendTo(const char * data, int len, const Socket_Address & address); 00027 PUBLISHED: 00028 inline bool SendTo(const string &data, const Socket_Address & address); 00029 inline bool SetToBroadCast(); 00030 00031 public: 00032 static TypeHandle get_class_type() { 00033 return _type_handle; 00034 } 00035 static void init_type() { 00036 Socket_IP::init_type(); 00037 register_type(_type_handle, "Socket_UDP_Outgoing", 00038 Socket_IP::get_class_type()); 00039 } 00040 virtual TypeHandle get_type() const { 00041 return get_class_type(); 00042 } 00043 virtual TypeHandle force_init_type() {init_type(); return get_class_type();} 00044 00045 private: 00046 static TypeHandle _type_handle; 00047 }; 00048 ////////////////////////////////////////////////////////////// 00049 // Function name : Socket_UDP_Outgoing:SetToBroadCast 00050 // Description : Ask the OS to let us receive BROADCASt packets on this port.. 00051 // Return type : bool 00052 // Argument : void 00053 ////////////////////////////////////////////////////////////// 00054 inline bool Socket_UDP_Outgoing::SetToBroadCast() 00055 { 00056 int optval = 1; 00057 00058 if (setsockopt(_socket, SOL_SOCKET, SO_BROADCAST, (char *)&optval, sizeof(optval)) != 0) 00059 return false; 00060 return true; 00061 } 00062 //////////////////////////////////////////////////////////////////// 00063 // Function name : Socket_UDP_Outgoing::InitToAddress 00064 // Description : Connects the Socket to a Specified address 00065 // 00066 // Return type : inline bool 00067 // Argument : NetAddress & address 00068 //////////////////////////////////////////////////////////////////// 00069 inline bool Socket_UDP_Outgoing::InitToAddress(const Socket_Address & address) 00070 { 00071 if (InitNoAddress() != true) 00072 return false; 00073 00074 if (DO_CONNECT(_socket, &address.GetAddressInfo()) != 0) 00075 return ErrorClose(); 00076 00077 return true; 00078 } 00079 //////////////////////////////////////////////////////////////////// 00080 // Function name : Socket_UDP_Outgoing::InitNoAddress 00081 // Description : This will set a udp up for targeted sends.. 00082 // 00083 // Return type : inline bool 00084 // Argument : void 00085 //////////////////////////////////////////////////////////////////// 00086 inline bool Socket_UDP_Outgoing::InitNoAddress() 00087 { 00088 Close(); 00089 _socket = DO_NEWUDP(); 00090 if (_socket == BAD_SOCKET) 00091 return false; 00092 00093 return true; 00094 } 00095 00096 //////////////////////////////////////////////////////////////////// 00097 // Function name : Socket_UDP_Outgoing::Send 00098 // Description : Send data to connected address 00099 // 00100 // Return type : inline bool 00101 // Argument : char * data 00102 // Argument : int len 00103 //////////////////////////////////////////////////////////////////// 00104 inline bool Socket_UDP_Outgoing::Send(const char * data, int len) 00105 { 00106 return (DO_SOCKET_WRITE(_socket, data, len) == len); 00107 } 00108 00109 //////////////////////////////////////////////////////////////////// 00110 // Function name : Socket_UDP_Outgoing::Send 00111 // Description : Send data to connected address 00112 // 00113 // Return type : inline bool 00114 // Argument : const string &data 00115 //////////////////////////////////////////////////////////////////// 00116 inline bool Socket_UDP_Outgoing::Send(const string &data) 00117 { 00118 return Send(data.data(), data.size()); 00119 } 00120 00121 //////////////////////////////////////////////////////////////////// 00122 // Function name : Socket_UDP_Outgoing::SendTo 00123 // Description : Send data to specified address 00124 // 00125 // Return type : inline bool 00126 // Argument : char * data 00127 // Argument : int len 00128 // Argument : NetAddress & address 00129 //////////////////////////////////////////////////////////////////// 00130 inline bool Socket_UDP_Outgoing::SendTo(const char * data, int len, const Socket_Address & address) 00131 { 00132 return (DO_SOCKET_WRITE_TO(_socket, data, len, &address.GetAddressInfo()) == len); 00133 } 00134 00135 //////////////////////////////////////////////////////////////////// 00136 // Function name : Socket_UDP_Outgoing::SendTo 00137 // Description : Send data to specified address 00138 // 00139 // Return type : inline bool 00140 // Argument : const string &data 00141 // Argument : NetAddress & address 00142 //////////////////////////////////////////////////////////////////// 00143 inline bool Socket_UDP_Outgoing::SendTo(const string &data, const Socket_Address & address) 00144 { 00145 return SendTo(data.data(), data.size(), address); 00146 } 00147 00148 #endif //__SOCKET_UDP_OUTGOING_H__