Panda3D
Loading...
Searching...
No Matches
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 */
10class EXPCL_PANDA_NATIVENET Socket_UDP_Outgoing : public Socket_IP {
11PUBLISHED:
12 inline Socket_UDP_Outgoing() {}
13
14 // use this interface for a tagreted UDP connection
15 inline bool InitToAddress(const Socket_Address &address);
16public:
17 inline bool Send(const char *data, int len);
18PUBLISHED:
19 inline bool Send(const std::string &data);
20 // use this interface for a none tagreted UDP connection
21 inline bool InitNoAddress();
22public:
23 inline bool SendTo(const char *data, int len, const Socket_Address &address);
24PUBLISHED:
25 inline bool SendTo(const std::string &data, const Socket_Address &address);
26 inline bool SetToBroadCast();
27
28public:
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
42private:
43 static TypeHandle _type_handle;
44};
45
46/**
47 * Ask the OS to let us receive broadcast packets on this port.
48 */
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 */
63InitToAddress(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 */
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 */
93Send(const char *data, int len) {
94 return (DO_SOCKET_WRITE(_socket, data, len) == len);
95}
96
97/**
98 * Send data to connected address
99 */
101Send(const std::string &data) {
102 return Send(data.data(), data.size());
103}
104
105/**
106 * Send data to specified address
107 */
109SendTo(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 */
117SendTo(const std::string &data, const Socket_Address &address) {
118 return SendTo(data.data(), data.size(), address);
119}
120
121#endif //__SOCKET_UDP_OUTGOING_H__
A simple place to store and manipulate tcp and port address for communication layer.
Base functionality for a INET domain Socket This call should be the starting point for all other unix...
Definition socket_ip.h:27
void Close()
Closes a socket if it is open (allocated).
Definition socket_ip.h:128
Base functionality for a UDP sending socket.
bool SendTo(const char *data, int len, const Socket_Address &address)
Send data to specified address.
bool Send(const char *data, int len)
Send data to connected address.
bool SetToBroadCast()
Ask the OS to let us receive broadcast packets on this port.
bool InitToAddress(const Socket_Address &address)
Connects the Socket to a specified address.
bool InitNoAddress()
This will set a udp up for targeted sends.
TypeHandle is the identifier used to differentiate C++ class types.
Definition typeHandle.h:81
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void register_type(TypeHandle &type_handle, const std::string &name)
This inline function is just a convenient way to call TypeRegistry::register_type(),...