Panda3D
socket_udp.h
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file socket_udp.h
10  * @author drose
11  * @date 2007-03-01
12  */
13 
14 #ifndef __SOCKET_UDP_H__
15 #define __SOCKET_UDP_H__
16 
17 #include "socket_udp_incoming.h"
18 
19 /**
20  * Base functionality for a combination UDP Reader and Writer. This
21  * duplicates code from Socket_UDP_Outgoing, to avoid the problems of multiple
22  * inheritance.
23  */
24 class EXPCL_PANDA_NATIVENET Socket_UDP : public Socket_UDP_Incoming {
25 public:
26 PUBLISHED:
27  inline Socket_UDP() {}
28 
29  // use this interface for a tagreted UDP connection
30  inline bool InitToAddress(const Socket_Address &address);
31 public:
32  inline bool Send(const char *data, int len);
33 PUBLISHED:
34  inline bool Send(const std::string &data);
35 public:
36  inline bool SendTo(const char *data, int len, const Socket_Address &address);
37 PUBLISHED:
38  inline bool SendTo(const std::string &data, const Socket_Address &address);
39  inline bool SetToBroadCast();
40 
41 public:
42  static TypeHandle get_class_type() {
43  return _type_handle;
44  }
45  static void init_type() {
46  Socket_UDP_Incoming::init_type();
47  register_type(_type_handle, "Socket_UDP",
48  Socket_UDP_Incoming::get_class_type());
49  }
50  virtual TypeHandle get_type() const {
51  return get_class_type();
52  }
53  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
54 
55 private:
56  static TypeHandle _type_handle;
57 };
58 
59 /**
60  * Ask the OS to let us receive broadcast packets on this port.
61  */
62 inline bool Socket_UDP::
64  int optval = 1;
65 
66  if (setsockopt(_socket, SOL_SOCKET, SO_BROADCAST, (char *)&optval, sizeof(optval)) != 0) {
67  return false;
68  }
69  return true;
70 }
71 
72 /**
73  * Connects the socket to a Specified address
74  */
75 inline bool Socket_UDP::
76 InitToAddress(const Socket_Address &address) {
77  if (InitNoAddress() != true) {
78  return false;
79  }
80 
81  if (DO_CONNECT(_socket, &address.GetAddressInfo()) != 0) {
82  return ErrorClose();
83  }
84  return true;
85 }
86 
87 /**
88  * Send data to connected address
89  */
90 inline bool Socket_UDP::
91 Send(const char *data, int len) {
92  return (DO_SOCKET_WRITE(_socket, data, len) == len);
93 }
94 
95 /**
96  * Send data to connected address
97  */
98 inline bool Socket_UDP::
99 Send(const std::string &data) {
100  return Send(data.data(), data.size());
101 }
102 
103 /**
104  * Send data to specified address
105  */
106 inline bool Socket_UDP::
107 SendTo(const char *data, int len, const Socket_Address &address) {
108  return (DO_SOCKET_WRITE_TO(_socket, data, len, &address.GetAddressInfo()) == len);
109 }
110 
111 /**
112  * Send data to specified address
113  */
114 inline bool Socket_UDP::
115 SendTo(const std::string &data, const Socket_Address &address) {
116  return SendTo(data.data(), data.size(), address);
117 }
118 
119 #endif //__SOCKET_UDP_H__
bool InitToAddress(const Socket_Address &address)
Connects the socket to a Specified address.
Definition: socket_udp.h:76
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
bool Send(const char *data, int len)
Send data to connected address.
Definition: socket_udp.h:91
Base functionality for a combination UDP Reader and Writer.
Definition: socket_udp.h:24
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:63
A simple place to store and manipulate tcp and port address for communication layer.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
bool SendTo(const char *data, int len, const Socket_Address &address)
Send data to specified address.
Definition: socket_udp.h:107
bool InitNoAddress()
Set this socket to work without a bound external address.
bool SetToBroadCast()
Flips the OS bits that allow for brodcast packets to come in on this port.