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
8using namespace NET;
9
13
14void 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
25int 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
37int 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}
Signals a problem with the execution of a socket call.
void sendTo(const void *buffer, size_t len, std::string_view foreignPath)
int timedReceiveFrom(void *buffer, size_t len, std::string &sourcePath, int timeout)
int receiveFrom(void *buffer, size_t len, std::string &sourcePath)
UnixSocket(int type, int protocol)
allows a subclass to create new socket
Definition UnixSocket.cpp:9