Panda3D

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 * Base functionality for a UDP sending socket
9 */
10 class EXPCL_PANDA_NATIVENET Socket_UDP_Outgoing : public Socket_IP {
11 PUBLISHED:
12 inline Socket_UDP_Outgoing() {}
13
14 // use this interface for a tagreted UDP connection
15 inline bool InitToAddress(const Socket_Address &address);
16 public:
17 inline bool Send(const char *data, int len);
18 PUBLISHED:
19 inline bool Send(const std::string &data);
20 // use this interface for a none tagreted UDP connection
21 inline bool InitNoAddress();
22 public:
23 inline bool SendTo(const char *data, int len, const Socket_Address &address);
24 PUBLISHED:
25 inline bool SendTo(const std::string &data, const Socket_Address &address);
26 inline bool SetToBroadCast();
27
28 public:
29 static TypeHandle get_class_type() {
30 return _type_handle;
31 }
32 static void init_type() {
33 Socket_IP::init_type();
34 register_type(_type_handle, "Socket_UDP_Outgoing",
35 Socket_IP::get_class_type());
36 }
37 virtual TypeHandle get_type() const {
38 return get_class_type();
39 }
40 virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
41
42 private:
43 static TypeHandle _type_handle;
44 };
45
46 /**
47 * Ask the OS to let us receive broadcast packets on this port.
48 */
49 inline bool Socket_UDP_Outgoing::
50 SetToBroadCast() {
51 int optval = 1;
52
53 if (setsockopt(_socket, SOL_SOCKET, SO_BROADCAST, (char *)&optval, sizeof(optval)) != 0) {
54 return false;
55 }
56 return true;
57 }
58
59 /**
60 * Connects the Socket to a specified address
61 */
62 inline bool Socket_UDP_Outgoing::
63 InitToAddress(const Socket_Address &address) {
64 if (InitNoAddress() != true) {
65 return false;
66 }
67
68 if (DO_CONNECT(_socket, &address.GetAddressInfo()) != 0) {
69 return ErrorClose();
70 }
71
72 return true;
73 }
74
75 /**
76 * This will set a udp up for targeted sends.
77 */
78 inline bool Socket_UDP_Outgoing::
79 InitNoAddress() {
80 Close();
81 _socket = DO_NEWUDP(AF_INET);
82 if (_socket == BAD_SOCKET) {
83 return false;
84 }
85
86 return true;
87 }
88
89 /**
90 * Send data to connected address
91 */
92 inline bool Socket_UDP_Outgoing::
93 Send(const char *data, int len) {
94 return (DO_SOCKET_WRITE(_socket, data, len) == len);
95 }
96
97 /**
98 * Send data to connected address
99 */
100 inline bool Socket_UDP_Outgoing::
101 Send(const std::string &data) {
102 return Send(data.data(), data.size());
103 }
104
105 /**
106 * Send data to specified address
107 */
108 inline bool Socket_UDP_Outgoing::
109 SendTo(const char *data, int len, const Socket_Address &address) {
110 return (DO_SOCKET_WRITE_TO(_socket, data, len, &address.GetAddressInfo()) == len);
111 }
112
113 /**
114 * Send data to specified address
115 */
116 inline bool Socket_UDP_Outgoing::
117 SendTo(const std::string &data, const Socket_Address &address) {
118 return SendTo(data.data(), data.size(), address);
119 }
120
121 #endif //__SOCKET_UDP_OUTGOING_H__
Socket_Address
A simple place to store and manipulate tcp and port address for communication layer.
Definition: socket_address.h:25
Socket_IP
Base functionality for a INET domain Socket This call should be the starting point for all other unix...
Definition: socket_ip.h:27
Socket_IP::Close
void Close()
Closes a socket if it is open (allocated).
Definition: socket_ip.h:128
Socket_UDP_Outgoing
Base functionality for a UDP sending socket.
Definition: socket_udp_outgoing.h:10
Socket_UDP_Outgoing::SendTo
bool SendTo(const char *data, int len, const Socket_Address &address)
Send data to specified address.
Definition: socket_udp_outgoing.h:109
Socket_UDP_Outgoing::Send
bool Send(const char *data, int len)
Send data to connected address.
Definition: socket_udp_outgoing.h:93
Socket_UDP_Outgoing::SetToBroadCast
bool SetToBroadCast()
Ask the OS to let us receive broadcast packets on this port.
Definition: socket_udp_outgoing.h:50
Socket_UDP_Outgoing::InitToAddress
bool InitToAddress(const Socket_Address &address)
Connects the Socket to a specified address.
Definition: socket_udp_outgoing.h:63
Socket_UDP_Outgoing::InitNoAddress
bool InitNoAddress()
This will set a udp up for targeted sends.
Definition: socket_udp_outgoing.h:79
TypeHandle
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
config_nativenet.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
register_type
void register_type(TypeHandle &type_handle, const std::string &name)
This inline function is just a convenient way to call TypeRegistry::register_type(),...
Definition: register_type.I:22