-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathUnixDatagramSocket.cpp
More file actions
57 lines (42 loc) · 1.52 KB
/
Copy pathUnixDatagramSocket.cpp
File metadata and controls
57 lines (42 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "UnixDatagramSocket.h"
#include "TempFailure.h"
#include <sys/socket.h>
#include <sys/un.h>
#include <poll.h>
using namespace NET;
UnixDatagramSocket::UnixDatagramSocket()
: UnixSocket( DATAGRAM, 0)
{}
void UnixDatagramSocket::sendTo( const void* buffer, size_t len, std::string_view foreignPath)
{
sockaddr_un destAddr;
fillAddress( foreignPath, destAddr);
int sent = TEMP_FAILURE_RETRY (::sendto( m_socket, (const raw_type*)buffer, len, 0, (sockaddr*)&destAddr, sizeof(destAddr)));
// Write out the whole buffer as a single message
if( sent != (int)len)
throw SocketException("Send failed (sendto)");
}
int UnixDatagramSocket::receiveFrom( void* buffer, size_t len, std::string& sourcePath)
{
sockaddr_un clientAddr;
socklen_t addr_len = sizeof(clientAddr);
int ret = TEMP_FAILURE_RETRY (::recvfrom( m_socket, (raw_type*)buffer, len, 0, (sockaddr*)&clientAddr, &addr_len));
if( ret < 0)
throw SocketException("Receive failed (recvfrom)");
sourcePath = extractPath( clientAddr, addr_len);
return ret;
}
int UnixDatagramSocket::timedReceiveFrom( void* buffer, size_t len, std::string& sourcePath, int timeout)
{
struct pollfd poll;
poll.fd = m_socket;
poll.events = POLLIN | POLLPRI | POLLRDHUP;
int ret = TEMP_FAILURE_RETRY (::poll( &poll, 1, timeout));
if( ret == 0) return 0;
if( ret < 0) throw SocketException("Receive failed (poll)");
if( poll.revents & POLLRDHUP)
m_peerDisconnected = true;
if( poll.revents & POLLIN || poll.revents & POLLPRI)
return receiveFrom( buffer, len, sourcePath);
return 0;
}