00001 #ifndef __SOCKET_UDP_OUTGOING_H__
00002 #define __SOCKET_UDP_OUTGOING_H__
00003
00004
00005
00006
00007
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
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
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
00050
00051
00052
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
00064
00065
00066
00067
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
00081
00082
00083
00084
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
00098
00099
00100
00101
00102
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
00111
00112
00113
00114
00115
00116 inline bool Socket_UDP_Outgoing::Send(const string &data)
00117 {
00118 return Send(data.data(), data.size());
00119 }
00120
00121
00122
00123
00124
00125
00126
00127
00128
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
00137
00138
00139
00140
00141
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__