Belle II Software development
UDPSocket Class Reference
Inheritance diagram for UDPSocket:
FileDescriptor

Public Member Functions

 UDPSocket (unsigned int port)
 
 UDPSocket (unsigned int port, const std::string &hostname, bool boardcast=false)
 
 UDPSocket (unsigned int port, unsigned int addr, bool boardcast=false)
 
int bind ()
 
int bind (unsigned int port, const std::string &hostname="", bool broadcast=true)
 
const std::string getHostName () const
 
unsigned int getPort () const
 
unsigned int getAddress () const
 
const std::string getRemoteHostName () const
 
unsigned int getRemotePort () const
 
unsigned int getRemoteAddress () const
 
virtual size_t write (const void *v, size_t count)
 
virtual size_t read (void *v, size_t count)
 
int get_fd () const
 
bool select (int sec=-1, int usec=-1)
 
bool select2 (int sec=-1, int usec=-1)
 
bool close ()
 

Protected Attributes

int m_fd
 

Static Private Member Functions

static unsigned int findSubnet (unsigned int addr)
 

Private Attributes

struct sockaddr_in m_addr
 
struct sockaddr_in m_remote_addr
 

Friends

class TCPServerSocket
 

Detailed Description

Definition at line 23 of file UDPSocket.h.

Constructor & Destructor Documentation

◆ UDPSocket() [1/4]

UDPSocket ( )

Definition at line 44 of file UDPSocket.cc.

45{
46 m_addr.sin_family = AF_INET;
47 m_addr.sin_addr.s_addr = INADDR_ANY;
48}

◆ UDPSocket() [2/4]

UDPSocket ( unsigned int  port)

Definition at line 50 of file UDPSocket.cc.

51{
52 m_addr.sin_port = htons(port);
53 m_addr.sin_family = AF_INET;
54 m_addr.sin_addr.s_addr = INADDR_ANY;
55 if ((m_fd = ::socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
56 throw (IOException("Failed to create socket"));
57 }
58}

◆ UDPSocket() [3/4]

UDPSocket ( unsigned int  port,
const std::string &  hostname,
bool  boardcast = false 
)

Definition at line 60 of file UDPSocket.cc.

63{
64 m_addr.sin_port = htons(port);
65 m_addr.sin_family = AF_INET;
66 if (hostname.size() > 0) {
67 struct hostent* host = NULL;
68 host = gethostbyname(hostname.c_str());
69 if (host == NULL) {
70 unsigned long addr = inet_addr(hostname.c_str());
71 if ((signed long) addr < 0) {
72 throw (std::exception());
73 // throw (IOException("Wrong host name or ip")); // TODO which throw is the right one?
74 } else {
75 host = gethostbyaddr((char*)&addr, sizeof(addr), AF_INET);
76 }
77 }
78 m_addr.sin_addr.s_addr = (*(unsigned long*) host->h_addr_list[0]);
79 }
80 if ((m_fd = ::socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
81 throw (IOException("Failed to create socket"));
82 }
83 if (boardcast) {
84 unsigned int addr = m_addr.sin_addr.s_addr;
85 if ((addr = findSubnet(addr)) > 0) {
86 m_addr.sin_addr.s_addr = addr;
87 }
88 int yes = 1;
89 setsockopt(m_fd, SOL_SOCKET, SO_BROADCAST,
90 (char*)&yes, sizeof(yes));
91 }
92}

◆ UDPSocket() [4/4]

UDPSocket ( unsigned int  port,
unsigned int  addr,
bool  boardcast = false 
)

Definition at line 94 of file UDPSocket.cc.

97{
98 m_addr.sin_port = htons(port);
99 m_addr.sin_family = AF_INET;
100 m_addr.sin_addr.s_addr = addr;
101 if ((m_fd = ::socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
102 throw (IOException("Failed to create socket"));
103 }
104 if (boardcast) {
105 addr = m_addr.sin_addr.s_addr;
106 if ((addr = findSubnet(addr)) > 0) {
107 m_addr.sin_addr.s_addr = addr;
108 }
109 int yes = 1;
110 setsockopt(m_fd, SOL_SOCKET, SO_BROADCAST,
111 (char*)&yes, sizeof(yes));
112 }
113}

◆ ~UDPSocket()

virtual ~UDPSocket ( )
inlinevirtual

Definition at line 36 of file UDPSocket.h.

36{}

Member Function Documentation

◆ bind() [1/2]

int bind ( )

Definition at line 145 of file UDPSocket.cc.

146{
147 if (::bind(m_fd, (struct sockaddr*)&m_addr, sizeof(m_addr)) < 0) {
148 close();
149 throw (IOException("Failed to bind socket port=%d",
150 ntohs(m_addr.sin_port)));
151 }
152 return m_fd;
153}

◆ bind() [2/2]

int bind ( unsigned int  port,
const std::string &  hostname = "",
bool  broadcast = true 
)

Definition at line 115 of file UDPSocket.cc.

117{
118 if (m_fd <= 0 && (m_fd = ::socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
119 throw (IOException("Failed to create socket"));
120 }
121 m_addr.sin_port = htons(port);
122 if (hostname.size() > 0) {
123 struct hostent* host = NULL;
124 host = gethostbyname(hostname.c_str());
125 if (host == NULL) {
126 unsigned long addr = inet_addr(hostname.c_str());
127 if ((signed long) addr < 0) {
128 throw (std::exception());
129 // throw (IOException("Wrong host name or ip")); TODO which throw is the right one?
130 } else {
131 host = gethostbyaddr((char*)&addr, sizeof(addr), AF_INET);
132 }
133 }
134 m_addr.sin_addr.s_addr = (*(unsigned long*) host->h_addr_list[0]);
135 if (broadcast) {
136 unsigned int addr = m_addr.sin_addr.s_addr;
137 if ((addr = findSubnet(addr)) > 0) {
138 m_addr.sin_addr.s_addr = addr;
139 }
140 }
141 }
142 return bind();
143}

◆ close()

bool close ( )
inherited

Definition at line 90 of file FileDescriptor.cc.

91{
92 if (m_fd > 0) {
93 if (::close(m_fd) != 0) {
94 return false;
95 }
96 }
97 m_fd = -1;
98 return true;
99}

◆ findSubnet()

unsigned int findSubnet ( unsigned int  addr)
staticprivate

Definition at line 26 of file UDPSocket.cc.

27{
28 struct ifaddrs* ifa_list;
29 struct ifaddrs* ifa;
30 char addrstr[256], netmaskstr[256];
31 if (getifaddrs(&ifa_list) != 0) return 0;
32 for (ifa = ifa_list; ifa != NULL; ifa = ifa->ifa_next) {
33 memset(addrstr, 0, sizeof(addrstr));
34 memset(netmaskstr, 0, sizeof(netmaskstr));
35 if (ifa->ifa_addr->sa_family == AF_INET &&
36 addr == ((struct sockaddr_in*)ifa->ifa_addr)->sin_addr.s_addr) {
37 addr |= ~(((struct sockaddr_in*)ifa->ifa_netmask)->sin_addr.s_addr);
38 }
39 }
40 freeifaddrs(ifa_list);
41 return addr;
42}

◆ get_fd()

int get_fd ( ) const
inherited

Definition at line 33 of file FileDescriptor.cc.

34{
35 return m_fd;
36}

◆ getAddress()

unsigned int getAddress ( ) const

Definition at line 207 of file UDPSocket.cc.

208{
209 return m_addr.sin_addr.s_addr;
210}

◆ getHostName()

const std::string getHostName ( ) const

Definition at line 202 of file UDPSocket.cc.

203{
204 return inet_ntoa(m_addr.sin_addr);
205}

◆ getPort()

unsigned int getPort ( ) const

Definition at line 212 of file UDPSocket.cc.

213{
214 return ntohs(m_addr.sin_port);
215}

◆ getRemoteAddress()

unsigned int getRemoteAddress ( ) const

Definition at line 222 of file UDPSocket.cc.

223{
224 return m_remote_addr.sin_addr.s_addr;
225}

◆ getRemoteHostName()

const std::string getRemoteHostName ( ) const

Definition at line 217 of file UDPSocket.cc.

218{
219 return inet_ntoa(m_remote_addr.sin_addr);
220}

◆ getRemotePort()

unsigned int getRemotePort ( ) const

Definition at line 227 of file UDPSocket.cc.

228{
229 return ntohs(m_remote_addr.sin_port);
230}

◆ read()

size_t read ( void *  v,
size_t  count 
)
virtual

Definition at line 180 of file UDPSocket.cc.

181{
182 size_t c = 0;
183 socklen_t addrlen = sizeof(m_remote_addr);
184 while (true) {
185 errno = 0;
186 int ret = recvfrom(m_fd, ((unsigned char*)buf + c), (count - c), 0,
187 (struct sockaddr*)&m_remote_addr, &addrlen);
188 if (ret <= 0) {
189 switch (errno) {
190 case EINTR: continue;
191 case EAGAIN: continue;
192 default:
193 throw (IOException("Error while reading."));
194 }
195 }
196 c += ret;
197 break;
198 }
199 return c;
200}

◆ select()

bool select ( int  sec = -1,
int  usec = -1 
)
inherited

Definition at line 38 of file FileDescriptor.cc.

39{
40 if (m_fd <= 0) {
41 return false;
42 }
43 fd_set fds;
44 FD_ZERO(&fds);
45 FD_SET(m_fd, &fds);
46 int ret;
47 if (sec >= 0 && usec >= 0) {
48 timeval t = {sec, usec};
49 ret = ::select(FD_SETSIZE, &fds, NULL, NULL, &t);
50 } else {
51 ret = ::select(FD_SETSIZE, &fds, NULL, NULL, NULL);
52 }
53 if (ret < 0) {
54 perror("select");
55 throw (IOException("Failed to select"));
56 }
57 if (FD_ISSET(m_fd, &fds)) {
58 return true;
59 } else {
60 return false;
61 }
62}

◆ select2()

bool select2 ( int  sec = -1,
int  usec = -1 
)
inherited

Definition at line 64 of file FileDescriptor.cc.

65{
66 if (m_fd <= 0) {
67 return false;
68 }
69 fd_set fds;
70 FD_ZERO(&fds);
71 FD_SET(m_fd, &fds);
72 int ret;
73 if (sec >= 0 && usec >= 0) {
74 timeval t = {sec, usec};
75 ret = ::select(FD_SETSIZE, NULL, &fds, NULL, &t);
76 } else {
77 ret = ::select(FD_SETSIZE, NULL, &fds, NULL, NULL);
78 }
79 if (ret < 0) {
80 perror("select");
81 throw (IOException("Failed to select"));
82 }
83 if (FD_ISSET(m_fd, &fds)) {
84 return true;
85 } else {
86 return false;
87 }
88}

◆ write()

size_t write ( const void *  v,
size_t  count 
)
virtual

Definition at line 155 of file UDPSocket.cc.

156{
157 size_t c = 0;
158 while (c < count) {
159 errno = 0;
160 int ret = sendto(m_fd, ((unsigned char*)buf + c),
161 (count - c), 0,
162 (struct sockaddr*)&m_addr, sizeof(m_addr));
163 if (ret <= 0) {
164 switch (errno) {
165 case EINTR: continue;
166 case ENETUNREACH:
167 case EHOSTUNREACH:
168 case ETIMEDOUT:
169 usleep(500);
170 continue;
171 default:
172 throw (IOException("Error while writing"));
173 }
174 }
175 c += ret;
176 }
177 return c;
178}

Friends And Related Function Documentation

◆ TCPServerSocket

friend class TCPServerSocket
friend

Definition at line 25 of file UDPSocket.h.

Member Data Documentation

◆ m_addr

struct sockaddr_in m_addr
private

Definition at line 55 of file UDPSocket.h.

◆ m_fd

int m_fd
protectedinherited

Definition at line 31 of file FileDescriptor.h.

◆ m_remote_addr

struct sockaddr_in m_remote_addr
private

Definition at line 56 of file UDPSocket.h.


The documentation for this class was generated from the following files: