-
Notifications
You must be signed in to change notification settings - Fork 18
/
PosixNamedPipe.cpp
132 lines (112 loc) · 2.7 KB
/
PosixNamedPipe.cpp
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#ifndef _WIN32
#include "PosixNamedPipe.h"
#include <errno.h>
#include <sstream>
#include <unistd.h>
#include <stdlib.h>
#define THROW_ERROR(X) std::stringstream s; \
s<<X<<strerror(errno); \
throw std::runtime_error(s.str());
PosixNamedPipe::PosixNamedPipe(const std::string& name,bool server):INamedPipe("/tmp/",name,server)
{
memset(&desc, 0, sizeof(struct sockaddr_un));
}
PosixNamedPipe::PosixNamedPipe(int pipe)
{
sock=pipe;
_server=false;
memset(&desc, 0, sizeof(struct sockaddr_un));
}
void PosixNamedPipe::open()
{
sock= socket(AF_UNIX, SOCK_STREAM, 0);
if(sock == -1) {
THROW_ERROR("Create_socket failed: ");
}
unlink(_name.c_str());
desc.sun_family = AF_UNIX;
strcpy(desc.sun_path, _name.c_str());
if (bind(sock, (sockaddr*)&desc, sizeof(struct sockaddr_un)) == -1) {
THROW_ERROR("Connection failed(bind): ");
}
if (listen(sock,SOMAXCONN) == -1) {
THROW_ERROR("Connection failed(listen): ");
}
}
void PosixNamedPipe::connect()
{
sock= socket(AF_UNIX, SOCK_STREAM, 0);
if(sock == -1)
{
THROW_ERROR("Create_socket failed: ");
}
desc.sun_family = AF_UNIX;
strcpy(desc.sun_path, _name.c_str());
if (::connect(sock, (sockaddr*)&desc, sizeof(struct sockaddr_un)) == -1)
{
THROW_ERROR("Connection failed(connect): ");
}
}
void PosixNamedPipe::internalReadBytes(void* buf,size_t size)
{
if ((recv(sock, buf, size, MSG_WAITALL)) == -1) {
THROW_ERROR("Error while reading: ");
}
}
void PosixNamedPipe::internalWriteBytes(const void* buf,size_t size)
{
size_t ret=-1;
if ((ret = send(sock, buf, size, 0)) == -1||ret!=size) {
THROW_ERROR("Error while sending: ");
}
}
void PosixNamedPipe::internalFlush()
{
}
void PosixNamedPipe::Close()
{
if(_server)
unlink(desc.sun_path);
close(sock);
}
PosixNamedPipe* PosixNamedPipe::WaitForConnection()
{
int client=accept(sock,NULL,NULL);
if(client!=-1)
return new PosixNamedPipe(client);
else {
THROW_ERROR("Accept error: ");
}
}
PosixNamedPipe* PosixNamedPipe::WaitForConnection(unsigned int timeout)
{
int nsock;
int retour;
fd_set readf;
fd_set writef;
struct timeval to;
FD_ZERO(&readf);
FD_ZERO(&writef);
FD_SET(sock, &readf);
FD_SET(sock, &writef);
to.tv_usec = timeout*1000;
retour = select(sock+1, &readf, &writef, 0, &to);
if (retour == 0)
{
return NULL;
}
if ( (FD_ISSET(sock, &readf)) || (FD_ISSET(sock,&writef)))
{
nsock = accept(sock, NULL, NULL);
return new PosixNamedPipe(nsock);
}
else
{
throw std::runtime_error("invalid socket descriptor!\n");
}
}
PosixNamedPipe::~PosixNamedPipe()
{
Close();
}
#endif