simple-socket library
1.5.0
Loading...
Searching...
No Matches
UnixDatagramSocket.cpp
1
#include "UnixDatagramSocket.h"
2
#include "SockAddress.h"
3
#include "TempFailure.h"
4
5
#include <sys/socket.h>
6
#include <poll.h>
7
8
using namespace
NET;
9
10
UnixDatagramSocket::UnixDatagramSocket
()
11
:
UnixSocket
( SOCK_DGRAM, 0)
12
{}
13
14
void
UnixDatagramSocket::sendTo
(
const
void
* buffer,
size_t
len, std::string_view foreignPath)
15
{
16
SockAddress
addr( foreignPath);
17
18
int
sent = TEMP_FAILURE_RETRY (::sendto( m_socket, (
const
raw_type*)buffer, len, 0, addr.data(), addr.length()));
19
20
// Write out the whole buffer as a single message
21
if
( sent != (
int
)len)
22
throw
SocketException
(
"Send failed (sendto)"
);
23
}
24
25
int
UnixDatagramSocket::receiveFrom
(
void
* buffer,
size_t
len, std::string& sourcePath)
26
{
27
SockAddress
addr;
28
29
int
ret = TEMP_FAILURE_RETRY (::recvfrom( m_socket, (raw_type*)buffer, len, 0, addr.data(), addr.lengthPtr()));
30
if
( ret < 0)
31
throw
SocketException
(
"Receive failed (recvfrom)"
);
32
33
sourcePath = addr.address();
34
return
ret;
35
}
36
37
int
UnixDatagramSocket::timedReceiveFrom
(
void
* buffer,
size_t
len, std::string& sourcePath,
int
timeout)
38
{
39
struct
pollfd poll;
40
poll.fd = m_socket;
41
poll.events = POLLIN | POLLPRI | POLLRDHUP;
42
43
int
ret = TEMP_FAILURE_RETRY (::poll( &poll, 1, timeout));
44
45
if
( ret == 0)
return
0;
46
if
( ret < 0)
throw
SocketException
(
"Receive failed (poll)"
);
47
48
if
( poll.revents & POLLRDHUP || poll.revents & POLLHUP)
49
m_peerDisconnected =
true
;
50
51
if
( poll.revents & POLLIN || poll.revents & POLLPRI)
52
return
receiveFrom
( buffer, len, sourcePath);
53
54
if
( poll.revents & (POLLERR | POLLNVAL))
55
throw
SocketException
(
"timedReceive failed (poll)"
);
56
57
return
0;
58
}
NET::SockAddress
Definition
SockAddress.h:15
NET::SocketException
Signals a problem with the execution of a socket call.
Definition
SimpleSocket.h:12
NET::UnixDatagramSocket::sendTo
void sendTo(const void *buffer, size_t len, std::string_view foreignPath)
Definition
UnixDatagramSocket.cpp:14
NET::UnixDatagramSocket::timedReceiveFrom
int timedReceiveFrom(void *buffer, size_t len, std::string &sourcePath, int timeout)
Definition
UnixDatagramSocket.cpp:37
NET::UnixDatagramSocket::UnixDatagramSocket
UnixDatagramSocket()
Definition
UnixDatagramSocket.cpp:10
NET::UnixDatagramSocket::receiveFrom
int receiveFrom(void *buffer, size_t len, std::string &sourcePath)
Definition
UnixDatagramSocket.cpp:25
NET::UnixSocket::UnixSocket
UnixSocket(int type, int protocol)
allows a subclass to create new socket
Definition
UnixSocket.cpp:9
src
UnixDatagramSocket.cpp
Generated by
1.16.1