Panda3D
 All Classes Functions Variables Enumerations
socket_udp_incoming.h
1 #ifndef __SOCKET_UDP_INCOMING_H__
2 #define __SOCKET_UDP_INCOMING_H__
3 
4 #include "pandabase.h"
5 #include "socket_ip.h"
6 
7 /////////////////////////////////////////////////////////////////////
8 // Class : Socket_UDP_Incoming
9 //
10 // Description : Base functionality for a UDP Reader
11 //
12 //
13 /////////////////////////////////////////////////////////////////////
14 class EXPCL_PANDA_NATIVENET Socket_UDP_Incoming : public Socket_IP
15 {
16 PUBLISHED:
17  inline Socket_UDP_Incoming() { }
18 
19  inline bool OpenForInput(const Socket_Address & address);
20  inline bool OpenForInputMCast(const Socket_Address & address );
21  inline bool GetPacket(char * data, int *max_len, Socket_Address & address);
22  inline bool SendTo(const char * data, int len, const Socket_Address & address);
23  inline bool InitNoAddress();
24  inline bool SetToBroadCast();
25 
26 public:
27  static TypeHandle get_class_type() {
28  return _type_handle;
29  }
30  static void init_type() {
31  Socket_IP::init_type();
32  register_type(_type_handle, "Socket_UDP_Incoming",
33  Socket_IP::get_class_type());
34  }
35  virtual TypeHandle get_type() const {
36  return get_class_type();
37  }
38  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
39 
40 private:
41  static TypeHandle _type_handle;
42 };
43 
44 //////////////////////////////////////////////////////////////
45 // Function name : Socket_UDP_Incoming::tToBroadCast
46 // Description : Flips the OS bits that allow for brodcast
47 // packets to com in on this port
48 //
49 // Return type : bool
50 // Argument : void
51 //////////////////////////////////////////////////////////////
53 {
54  int optval = 1;
55 
56  if (setsockopt(_socket, SOL_SOCKET, SO_BROADCAST, (char *)&optval, sizeof(optval)) != 0)
57  return false;
58  return true;
59 }
60 //////////////////////////////////////////////////////////////
61 // Function name : Socket_UDP_Incoming::InitNoAddress
62 // Description : Set this socket to work with out a bound external address..
63 // Return type : inline bool
64 // Argument : void
65 //////////////////////////////////////////////////////////////
67 {
68  Close();
69  _socket = DO_NEWUDP();
70  if (_socket == BAD_SOCKET)
71  return false;
72 
73  return true;
74 }
75 
76 ////////////////////////////////////////////////////////////////////
77 // Function name : Socket_UDP_Incoming::OpenForInput
78 // Description : Starts a UDP socket listening on a port
79 //
80 // Return type : bool
81 // Argument : NetAddress & address
82 ////////////////////////////////////////////////////////////////////
84 {
85  Close();
86  _socket = DO_NEWUDP();
87  if (_socket == BAD_SOCKET)
88  return ErrorClose();
89 
90  if (DO_BIND(_socket, &address.GetAddressInfo()) != 0)
91  return ErrorClose();
92 
93  return true;
94 }
95 
96 ////////////////////////////////////////////////////////////////////
97 // Function name : Socket_UDP_Incoming::OpenForInput
98 // Description : Starts a UDP socket listening on a port
99 //
100 // Return type : bool
101 // Argument : NetAddress & address
102 ////////////////////////////////////////////////////////////////////
104 {
105  Close();
106  _socket = DO_NEWUDP();
107  if (_socket == BAD_SOCKET)
108  return ErrorClose();
109 
110  Socket_Address wa1(address.get_port());
111  if (DO_BIND(_socket, &wa1.GetAddressInfo()) != 0)
112  return ErrorClose();
113 
114  struct ip_mreq imreq;
115  memset(&imreq,0,sizeof(imreq));
116  imreq.imr_multiaddr.s_addr = address.GetAddressInfo().sin_addr.s_addr;
117  imreq.imr_interface.s_addr = INADDR_ANY; // use DEFAULT interface
118 
119  int status = setsockopt(GetSocket(), IPPROTO_IP, IP_ADD_MEMBERSHIP,
120  (const char *)&imreq, sizeof(struct ip_mreq));
121 
122  if(status != 0)
123  return false;
124  return true;
125 }
126 
127 ////////////////////////////////////////////////////////////////////
128 // Function name : Socket_UDP_Incoming::GetPacket
129 // Description : Grabs a dataset off the listening UDP socket
130 // and fills in the source address information
131 //
132 // Return type : bool
133 // Argument : char * data
134 // Argument : int *max_len
135 // Argument : NetAddress & address
136 ////////////////////////////////////////////////////////////////////
137 inline bool Socket_UDP_Incoming::GetPacket(char * data, int *max_len, Socket_Address & address)
138 {
139  int val = DO_RECV_FROM(_socket, data, *max_len, &address.GetAddressInfo());
140  *max_len = 0;
141 
142  if (val <= 0)
143  {
144  if (GetLastError() != LOCAL_BLOCKING_ERROR) // im treating a blocking error as a 0 lenght read
145  return false;
146  } else
147  *max_len = val;
148 
149  return true;
150 }
151 
152 ////////////////////////////////////////////////////////////////////
153 // Function name : SocketUDP_Outgoing::SendTo
154 // Description : Send data to specified address
155 //
156 // Return type : inline bool
157 // Argument : char * data
158 // Argument : int len
159 // Argument : NetAddress & address
160 ////////////////////////////////////////////////////////////////////
161 inline bool Socket_UDP_Incoming::SendTo(const char * data, int len, const Socket_Address & address)
162 {
163  return (DO_SOCKET_WRITE_TO(_socket, data, len, &address.GetAddressInfo()) == len);
164 }
165 
166 
167 
168 
169 #endif //__SOCKET_UDP_INCOMING_H__
bool OpenForInputMCast(const Socket_Address &address)
Starts a UDP socket listening on a port.
Base functionality for a INET domain Socket this call should be the starting point for all other unix...
Definition: socket_ip.h:34
bool GetPacket(char *data, int *max_len, Socket_Address &address)
Grabs a dataset off the listening UDP socket and fills in the source address information.
unsigned short get_port() const
Get the port portion as an integer.
bool SendTo(const char *data, int len, const Socket_Address &address)
Send data to specified address.
Base functionality for a UDP Reader.
A simple place to store and munipulate tcp and port address for communication layer.
static int GetLastError()
gets the last errcode from a socket operation
Definition: socket_ip.h:152
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 OpenForInput(const Socket_Address &address)
Starts a UDP socket listening on a port.
SOCKET GetSocket()
Gets the base socket type.
Definition: socket_ip.h:240
bool InitNoAddress()
Set this socket to work with out a bound external address.
bool SetToBroadCast()
Flips the OS bits that allow for brodcast packets to com in on this port.