00001 // Filename: socket_udp.h 00002 // Created by: drose (01Mar07) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #ifndef __SOCKET_UDP_H__ 00016 #define __SOCKET_UDP_H__ 00017 00018 #include "socket_udp_incoming.h" 00019 00020 ///////////////////////////////////////////////////////////////////// 00021 // Class : Socket_UDP 00022 // 00023 // Description : Base functionality for a combination UDP Reader and 00024 // Writer. This duplicates code from 00025 // Socket_UDP_Outgoing, to avoid the problems of 00026 // multiple inheritance. 00027 ///////////////////////////////////////////////////////////////////// 00028 class EXPCL_PANDA_NATIVENET Socket_UDP : public Socket_UDP_Incoming 00029 { 00030 public: 00031 PUBLISHED: 00032 inline Socket_UDP() { } 00033 00034 // use this interface for a tagreted UDP connection 00035 inline bool InitToAddress(const Socket_Address & address); 00036 public: 00037 inline bool Send(const char * data, int len); 00038 PUBLISHED: 00039 inline bool Send(const string &data); 00040 // use this interface for a none tagreted UDP connection 00041 inline bool InitNoAddress(); 00042 public: 00043 inline bool SendTo(const char * data, int len, const Socket_Address & address); 00044 PUBLISHED: 00045 inline bool SendTo(const string &data, const Socket_Address & address); 00046 inline bool SetToBroadCast(); 00047 00048 public: 00049 static TypeHandle get_class_type() { 00050 return _type_handle; 00051 } 00052 static void init_type() { 00053 Socket_UDP_Incoming::init_type(); 00054 register_type(_type_handle, "Socket_UDP", 00055 Socket_UDP_Incoming::get_class_type()); 00056 } 00057 virtual TypeHandle get_type() const { 00058 return get_class_type(); 00059 } 00060 virtual TypeHandle force_init_type() {init_type(); return get_class_type();} 00061 00062 private: 00063 static TypeHandle _type_handle; 00064 }; 00065 ////////////////////////////////////////////////////////////// 00066 // Function name : Socket_UDP:SetToBroadCast 00067 // Description : Ask the OS to let us receive BROADCASt packets on this port.. 00068 // Return type : bool 00069 // Argument : void 00070 ////////////////////////////////////////////////////////////// 00071 inline bool Socket_UDP::SetToBroadCast() 00072 { 00073 int optval = 1; 00074 00075 if (setsockopt(_socket, SOL_SOCKET, SO_BROADCAST, (char *)&optval, sizeof(optval)) != 0) 00076 return false; 00077 return true; 00078 } 00079 //////////////////////////////////////////////////////////////////// 00080 // Function name : Socket_UDP::InitToAddress 00081 // Description : Connects the Socket to a Specified address 00082 // 00083 // Return type : inline bool 00084 // Argument : NetAddress & address 00085 //////////////////////////////////////////////////////////////////// 00086 inline bool Socket_UDP::InitToAddress(const Socket_Address & address) 00087 { 00088 if (InitNoAddress() != true) 00089 return false; 00090 00091 if (DO_CONNECT(_socket, &address.GetAddressInfo()) != 0) 00092 return ErrorClose(); 00093 00094 return true; 00095 } 00096 //////////////////////////////////////////////////////////////////// 00097 // Function name : Socket_UDP::InitNoAddress 00098 // Description : This will set a udp up for targeted sends.. 00099 // 00100 // Return type : inline bool 00101 // Argument : void 00102 //////////////////////////////////////////////////////////////////// 00103 inline bool Socket_UDP::InitNoAddress() 00104 { 00105 Close(); 00106 _socket = DO_NEWUDP(); 00107 if (_socket == BAD_SOCKET) 00108 return false; 00109 00110 return true; 00111 } 00112 00113 //////////////////////////////////////////////////////////////////// 00114 // Function name : Socket_UDP::Send 00115 // Description : Send data to connected address 00116 // 00117 // Return type : inline bool 00118 // Argument : char * data 00119 // Argument : int len 00120 //////////////////////////////////////////////////////////////////// 00121 inline bool Socket_UDP::Send(const char * data, int len) 00122 { 00123 return (DO_SOCKET_WRITE(_socket, data, len) == len); 00124 } 00125 00126 //////////////////////////////////////////////////////////////////// 00127 // Function name : Socket_UDP::Send 00128 // Description : Send data to connected address 00129 // 00130 // Return type : inline bool 00131 // Argument : const string &data 00132 //////////////////////////////////////////////////////////////////// 00133 inline bool Socket_UDP::Send(const string &data) 00134 { 00135 return Send(data.data(), data.size()); 00136 } 00137 00138 //////////////////////////////////////////////////////////////////// 00139 // Function name : Socket_UDP::SendTo 00140 // Description : Send data to specified address 00141 // 00142 // Return type : inline bool 00143 // Argument : char * data 00144 // Argument : int len 00145 // Argument : NetAddress & address 00146 //////////////////////////////////////////////////////////////////// 00147 inline bool Socket_UDP::SendTo(const char * data, int len, const Socket_Address & address) 00148 { 00149 return (DO_SOCKET_WRITE_TO(_socket, data, len, &address.GetAddressInfo()) == len); 00150 } 00151 00152 //////////////////////////////////////////////////////////////////// 00153 // Function name : Socket_UDP::SendTo 00154 // Description : Send data to specified address 00155 // 00156 // Return type : inline bool 00157 // Argument : const string &data 00158 // Argument : NetAddress & address 00159 //////////////////////////////////////////////////////////////////// 00160 inline bool Socket_UDP::SendTo(const string &data, const Socket_Address & address) 00161 { 00162 return SendTo(data.data(), data.size(), address); 00163 } 00164 00165 #endif //__SOCKET_UDP_H__