simple-socket library 1.5.0
Loading...
Searching...
No Matches
UnixSocket.cpp
1#include "UnixSocket.h"
2#include "SockAddress.h"
3
4#include <sys/socket.h>
5#include <unistd.h>
6
7using namespace NET;
8
9UnixSocket::UnixSocket( int type, int protocol)
10: SimpleSocket( AF_UNIX, type, protocol)
11{}
12
14: SimpleSocket(sockfd)
15{}
16
17UnixSocket::~UnixSocket()
18{
19 if( !m_boundPath.empty())
20 ::unlink( m_boundPath.c_str());
21}
22
23void UnixSocket::connect( std::string_view foreignPath)
24{
25 SockAddress addr( foreignPath);
26
27 if( ::connect( m_socket, addr.data(), addr.length()) < 0)
28 throw SocketException("Connect failed (connect)");
29
30 m_peerDisconnected = false;
31}
32
33void UnixSocket::bind( std::string_view localPath)
34{
35 SockAddress addr( localPath);
36
37 if( ::bind( m_socket, addr.data(), addr.length()) < 0)
38 throw SocketException("Set of local path failed (bind)");
39
40 m_boundPath = localPath;
41}
42
43std::string UnixSocket::getLocalPath() const
44{
45 SockAddress addr;
46
47 if( ::getsockname( m_socket, addr.data(), addr.lengthPtr()) < 0)
48 throw SocketException("Fetch of local path failed (getsockname)");
49
50 return addr.address();
51}
52
53std::string UnixSocket::getForeignPath() const
54{
55 SockAddress addr;
56
57 if( ::getpeername( m_socket, addr.data(), addr.lengthPtr()) < 0)
58 throw SocketException("Fetch of foreign path failed (getpeername)");
59
60 return addr.address();
61}
Signals a problem with the execution of a socket call.
void connect(std::string_view foreignPath)
std::string getForeignPath() const
UnixSocket(int type, int protocol)
allows a subclass to create new socket
Definition UnixSocket.cpp:9
void bind(std::string_view localPath)
std::string getLocalPath() const