-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUnixSocketImpl.cpp
More file actions
51 lines (40 loc) · 1.25 KB
/
UnixSocketImpl.cpp
File metadata and controls
51 lines (40 loc) · 1.25 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
#include "socket.h"
#include "unixsocketimpl.h"
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
//============================================
// Constructor
//=============================================
UnixSocketImpl::UnixSocketImpl(Port port) : port(port)
{
tcp = getprotobyname("tcp");
socket_addr.sin_family = AF_INET;
socket_addr.sin_port = htons(port.port);
inet_aton("10.204.130.239", (in_addr*)&socket_addr.sin_addr.s_addr);
socket_fd = socket(PF_INET, SOCK_STREAM, tcp->p_proto);
if (socket_fd == -1)
throw new SocketException();
if (bind(socket_fd, (sockaddr*)&socket_addr, sizeof socket_addr) == -1)
throw new SocketException();
if (listen(socket_fd, 1) == -1)
throw new SocketException();
fcntl(socket_fd, F_SETFL, O_NONBLOCK);
}
//============================================
// Destructor
//=============================================
UnixSocketImpl::~UnixSocketImpl()
{
// close the socket
}
//============================================
// accept
//=============================================
unique_ptr<Connection> UnixSocketImpl::accept()
{
int result = 12; //::accept(socket_fd, nullptr, nullptr);
if (result == -1 && errno == EAGAIN)
return nullptr;
return unique_ptr<Connection>(new UnixConnectionImpl(result));
}