Panda3D
 All Classes Functions Variables Enumerations
socket_udp.h
1 // Filename: socket_udp.h
2 // Created by: drose (01Mar07)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef __SOCKET_UDP_H__
16 #define __SOCKET_UDP_H__
17 
18 #include "socket_udp_incoming.h"
19 
20 /////////////////////////////////////////////////////////////////////
21 // Class : Socket_UDP
22 //
23 // Description : Base functionality for a combination UDP Reader and
24 // Writer. This duplicates code from
25 // Socket_UDP_Outgoing, to avoid the problems of
26 // multiple inheritance.
27 /////////////////////////////////////////////////////////////////////
28 class EXPCL_PANDA_NATIVENET Socket_UDP : public Socket_UDP_Incoming
29 {
30 public:
31 PUBLISHED:
32  inline Socket_UDP() { }
33 
34  // use this interface for a tagreted UDP connection
35  inline bool InitToAddress(const Socket_Address & address);
36 public:
37  inline bool Send(const char * data, int len);
38 PUBLISHED:
39  inline bool Send(const string &data);
40  // use this interface for a none tagreted UDP connection
41  inline bool InitNoAddress();
42 public:
43  inline bool SendTo(const char * data, int len, const Socket_Address & address);
44 PUBLISHED:
45  inline bool SendTo(const string &data, const Socket_Address & address);
46  inline bool SetToBroadCast();
47 
48 public:
49  static TypeHandle get_class_type() {
50  return _type_handle;
51  }
52  static void init_type() {
53  Socket_UDP_Incoming::init_type();
54  register_type(_type_handle, "Socket_UDP",
55  Socket_UDP_Incoming::get_class_type());
56  }
57  virtual TypeHandle get_type() const {
58  return get_class_type();
59  }
60  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
61 
62 private:
63  static TypeHandle _type_handle;
64 };
65 //////////////////////////////////////////////////////////////
66 // Function name : Socket_UDP:SetToBroadCast
67 // Description : Ask the OS to let us receive BROADCASt packets on this port..
68 // Return type : bool
69 // Argument : void
70 //////////////////////////////////////////////////////////////
72 {
73  int optval = 1;
74 
75  if (setsockopt(_socket, SOL_SOCKET, SO_BROADCAST, (char *)&optval, sizeof(optval)) != 0)
76  return false;
77  return true;
78 }
79 ////////////////////////////////////////////////////////////////////
80 // Function name : Socket_UDP::InitToAddress
81 // Description : Connects the Socket to a Specified address
82 //
83 // Return type : inline bool
84 // Argument : NetAddress & address
85 ////////////////////////////////////////////////////////////////////
86 inline bool Socket_UDP::InitToAddress(const Socket_Address & address)
87 {
88  if (InitNoAddress() != true)
89  return false;
90 
91  if (DO_CONNECT(_socket, &address.GetAddressInfo()) != 0)
92  return ErrorClose();
93 
94  return true;
95 }
96 ////////////////////////////////////////////////////////////////////
97 // Function name : Socket_UDP::InitNoAddress
98 // Description : This will set a udp up for targeted sends..
99 //
100 // Return type : inline bool
101 // Argument : void
102 ////////////////////////////////////////////////////////////////////
104 {
105  Close();
106  _socket = DO_NEWUDP();
107  if (_socket == BAD_SOCKET)
108  return false;
109 
110  return true;
111 }
112 
113 ////////////////////////////////////////////////////////////////////
114 // Function name : Socket_UDP::Send
115 // Description : Send data to connected address
116 //
117 // Return type : inline bool
118 // Argument : char * data
119 // Argument : int len
120 ////////////////////////////////////////////////////////////////////
121 inline bool Socket_UDP::Send(const char * data, int len)
122 {
123  return (DO_SOCKET_WRITE(_socket, data, len) == len);
124 }
125 
126 ////////////////////////////////////////////////////////////////////
127 // Function name : Socket_UDP::Send
128 // Description : Send data to connected address
129 //
130 // Return type : inline bool
131 // Argument : const string &data
132 ////////////////////////////////////////////////////////////////////
133 inline bool Socket_UDP::Send(const string &data)
134 {
135  return Send(data.data(), data.size());
136 }
137 
138 ////////////////////////////////////////////////////////////////////
139 // Function name : Socket_UDP::SendTo
140 // Description : Send data to specified address
141 //
142 // Return type : inline bool
143 // Argument : char * data
144 // Argument : int len
145 // Argument : NetAddress & address
146 ////////////////////////////////////////////////////////////////////
147 inline bool Socket_UDP::SendTo(const char * data, int len, const Socket_Address & address)
148 {
149  return (DO_SOCKET_WRITE_TO(_socket, data, len, &address.GetAddressInfo()) == len);
150 }
151 
152 ////////////////////////////////////////////////////////////////////
153 // Function name : Socket_UDP::SendTo
154 // Description : Send data to specified address
155 //
156 // Return type : inline bool
157 // Argument : const string &data
158 // Argument : NetAddress & address
159 ////////////////////////////////////////////////////////////////////
160 inline bool Socket_UDP::SendTo(const string &data, const Socket_Address & address)
161 {
162  return SendTo(data.data(), data.size(), address);
163 }
164 
165 #endif //__SOCKET_UDP_H__
bool InitToAddress(const Socket_Address &address)
Connects the Socket to a Specified address.
Definition: socket_udp.h:86
bool InitNoAddress()
This will set a udp up for targeted sends.
Definition: socket_udp.h:103
bool Send(const char *data, int len)
Send data to connected address.
Definition: socket_udp.h:121
Base functionality for a combination UDP Reader and Writer.
Definition: socket_udp.h:28
bool SendTo(const char *data, int len, const Socket_Address &address)
Send data to specified address.
Base functionality for a UDP Reader.
bool SetToBroadCast()
Ask the OS to let us receive BROADCASt packets on this port.
Definition: socket_udp.h:71
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.
Definition: socket_udp.h:147
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.