Panda3D
 All Classes Functions Variables Enumerations
socket_udp_outgoing.h
1 #ifndef __SOCKET_UDP_OUTGOING_H__
2 #define __SOCKET_UDP_OUTGOING_H__
3 
4 #include "config_nativenet.h"
5 #include "socket_ip.h"
6 
7 /////////////////////////////////////////////////////////////////////
8 // Class : Socket_UDP_Outgoing
9 //
10 // Description : Base functionality for a UDP Sending Socket
11 //
12 //
13 /////////////////////////////////////////////////////////////////////
14 class EXPCL_PANDA_NATIVENET Socket_UDP_Outgoing : public Socket_IP
15 {
16 public:
17 PUBLISHED:
18  inline Socket_UDP_Outgoing() { }
19 
20  // use this interface for a tagreted UDP connection
21  inline bool InitToAddress(const Socket_Address & address);
22 public:
23  inline bool Send(const char * data, int len);
24 PUBLISHED:
25  inline bool Send(const string &data);
26  // use this interface for a none tagreted UDP connection
27  inline bool InitNoAddress();
28 public:
29  inline bool SendTo(const char * data, int len, const Socket_Address & address);
30 PUBLISHED:
31  inline bool SendTo(const string &data, const Socket_Address & address);
32  inline bool SetToBroadCast();
33 
34 public:
35  static TypeHandle get_class_type() {
36  return _type_handle;
37  }
38  static void init_type() {
39  Socket_IP::init_type();
40  register_type(_type_handle, "Socket_UDP_Outgoing",
41  Socket_IP::get_class_type());
42  }
43  virtual TypeHandle get_type() const {
44  return get_class_type();
45  }
46  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
47 
48 private:
49  static TypeHandle _type_handle;
50 };
51 //////////////////////////////////////////////////////////////
52 // Function name : Socket_UDP_Outgoing:SetToBroadCast
53 // Description : Ask the OS to let us receive BROADCASt packets on this port..
54 // Return type : bool
55 // Argument : void
56 //////////////////////////////////////////////////////////////
58 {
59  int optval = 1;
60 
61  if (setsockopt(_socket, SOL_SOCKET, SO_BROADCAST, (char *)&optval, sizeof(optval)) != 0)
62  return false;
63  return true;
64 }
65 ////////////////////////////////////////////////////////////////////
66 // Function name : Socket_UDP_Outgoing::InitToAddress
67 // Description : Connects the Socket to a Specified address
68 //
69 // Return type : inline bool
70 // Argument : NetAddress & address
71 ////////////////////////////////////////////////////////////////////
73 {
74  if (InitNoAddress() != true)
75  return false;
76 
77  if (DO_CONNECT(_socket, &address.GetAddressInfo()) != 0)
78  return ErrorClose();
79 
80  return true;
81 }
82 ////////////////////////////////////////////////////////////////////
83 // Function name : Socket_UDP_Outgoing::InitNoAddress
84 // Description : This will set a udp up for targeted sends..
85 //
86 // Return type : inline bool
87 // Argument : void
88 ////////////////////////////////////////////////////////////////////
90 {
91  Close();
92  _socket = DO_NEWUDP();
93  if (_socket == BAD_SOCKET)
94  return false;
95 
96  return true;
97 }
98 
99 ////////////////////////////////////////////////////////////////////
100 // Function name : Socket_UDP_Outgoing::Send
101 // Description : Send data to connected address
102 //
103 // Return type : inline bool
104 // Argument : char * data
105 // Argument : int len
106 ////////////////////////////////////////////////////////////////////
107 inline bool Socket_UDP_Outgoing::Send(const char * data, int len)
108 {
109  return (DO_SOCKET_WRITE(_socket, data, len) == len);
110 }
111 
112 ////////////////////////////////////////////////////////////////////
113 // Function name : Socket_UDP_Outgoing::Send
114 // Description : Send data to connected address
115 //
116 // Return type : inline bool
117 // Argument : const string &data
118 ////////////////////////////////////////////////////////////////////
119 inline bool Socket_UDP_Outgoing::Send(const string &data)
120 {
121  return Send(data.data(), data.size());
122 }
123 
124 ////////////////////////////////////////////////////////////////////
125 // Function name : Socket_UDP_Outgoing::SendTo
126 // Description : Send data to specified address
127 //
128 // Return type : inline bool
129 // Argument : char * data
130 // Argument : int len
131 // Argument : NetAddress & address
132 ////////////////////////////////////////////////////////////////////
133 inline bool Socket_UDP_Outgoing::SendTo(const char * data, int len, const Socket_Address & address)
134 {
135  return (DO_SOCKET_WRITE_TO(_socket, data, len, &address.GetAddressInfo()) == len);
136 }
137 
138 ////////////////////////////////////////////////////////////////////
139 // Function name : Socket_UDP_Outgoing::SendTo
140 // Description : Send data to specified address
141 //
142 // Return type : inline bool
143 // Argument : const string &data
144 // Argument : NetAddress & address
145 ////////////////////////////////////////////////////////////////////
146 inline bool Socket_UDP_Outgoing::SendTo(const string &data, const Socket_Address & address)
147 {
148  return SendTo(data.data(), data.size(), address);
149 }
150 
151 #endif //__SOCKET_UDP_OUTGOING_H__
bool InitNoAddress()
This will set a udp up for targeted sends.
Base functionality for a INET domain Socket this call should be the starting point for all other unix...
Definition: socket_ip.h:34
bool SetToBroadCast()
Ask the OS to let us receive BROADCASt packets on this port.
bool Send(const char *data, int len)
Send data to connected address.
Base functionality for a UDP Sending Socket.
bool InitToAddress(const Socket_Address &address)
Connects the Socket to a Specified address.
A simple place to store and munipulate tcp and port address for communication layer.
void Close()
closes a socket if it is open (allocated)
Definition: socket_ip.h:141
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
bool SendTo(const char *data, int len, const Socket_Address &address)
Send data to specified address.