Merge branch 'master' into port_register_notification_defer
[jack2.git] / posix / JackSocket.cpp
blobec891acba791d78dbf326b08c4070b357219e31e
1 /*
2 Copyright (C) 2004-2008 Grame
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #include "JackSocket.h"
21 #include "JackConstants.h"
22 #include "JackTools.h"
23 #include "JackError.h"
24 #include <string.h>
25 #include <stdio.h>
26 #include <pthread.h>
27 #include <fcntl.h>
29 namespace Jack
32 static void BuildName(const char* client_name, char* res, const char* dir, int which)
34 char ext_client_name[JACK_CLIENT_NAME_SIZE + 1];
35 JackTools::RewriteName(client_name, ext_client_name);
36 sprintf(res, "%s/jack_%s_%d_%d", dir, ext_client_name, JackTools::GetUID(), which);
39 JackClientSocket::JackClientSocket(int socket): fSocket(socket),fTimeOut(0)
42 #if defined(__sun__) || defined(sun)
44 void JackClientSocket::SetReadTimeOut(long sec)
46 int flags;
47 fTimeOut = sec;
49 if ((flags = fcntl(fSocket, F_GETFL, 0)) < 0) {
50 jack_error("JackClientSocket::SetReadTimeOut error in fcntl F_GETFL");
51 return;
54 flags |= O_NONBLOCK;
55 if (fcntl(fSocket, F_SETFL, flags) < 0) {
56 jack_error("JackClientSocket::SetReadTimeOut error in fcntl F_SETFL");
57 return;
61 void JackClientSocket::SetWriteTimeOut(long sec)
63 int flags;
64 fTimeOut = sec;
66 if ((flags = fcntl(fSocket, F_GETFL, 0)) < 0) {
67 jack_error("JackClientSocket::SetWriteTimeOut error in fcntl F_GETFL");
68 return;
71 flags |= O_NONBLOCK;
72 if (fcntl(fSocket, F_SETFL, flags) < 0) {
73 jack_error("JackClientSocket::SetWriteTimeOut error in fcntl F_SETFL");
74 return;
78 #else
80 void JackClientSocket::SetReadTimeOut(long sec)
82 struct timeval timout;
83 timout.tv_sec = sec;
84 timout.tv_usec = 0;
85 if (setsockopt(fSocket, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timout, sizeof(timeval)) < 0) {
86 jack_error("SetReadTimeOut fd = %ld err = %s", fSocket, strerror(errno));
90 void JackClientSocket::SetWriteTimeOut(long sec)
92 struct timeval timout;
93 timout.tv_sec = sec ;
94 timout.tv_usec = 0;
95 if (setsockopt(fSocket, SOL_SOCKET, SO_SNDTIMEO, (const char*)&timout, sizeof(timeval)) < 0) {
96 jack_error("SetWriteTimeOut fd = %ld err = %s", fSocket, strerror(errno));
100 #endif
102 void JackClientSocket::SetNonBlocking(bool onoff)
104 if (onoff) {
105 long flags = 0;
106 if (fcntl(fSocket, F_SETFL, flags | O_NONBLOCK) < 0) {
107 jack_error("SetNonBlocking fd = %ld err = %s", fSocket, strerror(errno));
112 int JackClientSocket::Connect(const char* dir, const char* name, int which) // A revoir : utilisation de "which"
114 struct sockaddr_un addr;
116 if ((fSocket = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
117 jack_error("Cannot create socket err = %s", strerror(errno));
118 return -1;
121 addr.sun_family = AF_UNIX;
122 BuildName(name, addr.sun_path, dir, which);
123 jack_log("Connect: addr.sun_path %s", addr.sun_path);
125 if (connect(fSocket, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
126 jack_error("Cannot connect to server socket err = %s", strerror(errno));
127 close(fSocket);
128 return -1;
131 #ifdef __APPLE__
132 int on = 1 ;
133 if (setsockopt(fSocket, SOL_SOCKET, SO_NOSIGPIPE, (const char*)&on, sizeof(on)) < 0) {
134 jack_log("setsockopt SO_NOSIGPIPE fd = %ld err = %s", fSocket, strerror(errno));
136 #endif
138 return 0;
141 int JackClientSocket::Close()
143 jack_log("JackClientSocket::Close");
144 if (fSocket > 0) {
145 shutdown(fSocket, SHUT_RDWR);
146 close(fSocket);
147 fSocket = -1;
148 return 0;
149 } else {
150 return -1;
154 int JackClientSocket::Read(void* data, int len)
156 int res;
158 #if defined(__sun__) || defined(sun)
159 if (fTimeOut > 0) {
161 struct timeval tv;
162 fd_set fdset;
163 ssize_t res;
165 tv.tv_sec = fTimeOut;
166 tv.tv_usec = 0;
168 FD_ZERO(&fdset);
169 FD_SET(fSocket, &fdset);
171 do {
172 res = select(fSocket + 1, &fdset, NULL, NULL, &tv);
173 } while (res < 0 && errno == EINTR);
175 if (res < 0) {
176 return res;
177 } else if (res == 0) {
178 return -1;
181 #endif
183 if ((res = read(fSocket, data, len)) != len) {
184 if (errno == EWOULDBLOCK) {
185 jack_error("JackClientSocket::Read time out");
186 return 0; // For a non blocking socket, a read failure is not considered as an error
187 } else if (res != 0) {
188 jack_error("Cannot read socket fd = %d err = %s", fSocket, strerror(errno));
189 return 0;
190 } else {
191 return -1;
193 } else {
194 return 0;
198 int JackClientSocket::Write(void* data, int len)
200 int res;
202 #if defined(__sun__) || defined(sun)
203 if (fTimeOut > 0) {
205 struct timeval tv;
206 fd_set fdset;
207 ssize_t res;
209 tv.tv_sec = fTimeOut;
210 tv.tv_usec = 0;
212 FD_ZERO(&fdset);
213 FD_SET(fSocket, &fdset);
215 do {
216 res = select(fSocket + 1, NULL, &fdset, NULL, &tv);
217 } while (res < 0 && errno == EINTR);
219 if (res < 0) {
220 return res;
221 } else if (res == 0) {
222 return -1;
225 #endif
227 if ((res = write(fSocket, data, len)) != len) {
228 if (errno == EWOULDBLOCK) {
229 jack_log("JackClientSocket::Write time out");
230 return 0; // For a non blocking socket, a write failure is not considered as an error
231 } else if (res != 0) {
232 jack_error("Cannot write socket fd = %ld err = %s", fSocket, strerror(errno));
233 return 0;
234 } else {
235 return -1;
237 } else {
238 return 0;
242 int JackServerSocket::Bind(const char* dir, const char* name, int which) // A revoir : utilisation de "which"
244 struct sockaddr_un addr;
246 if ((fSocket = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
247 jack_error("Cannot create server socket err = %s", strerror(errno));
248 return -1;
251 addr.sun_family = AF_UNIX;
252 BuildName(name, fName, dir, which);
253 strncpy(addr.sun_path, fName, sizeof(addr.sun_path) - 1);
255 jack_log("Bind: addr.sun_path %s", addr.sun_path);
256 unlink(fName); // Security...
258 if (bind(fSocket, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
259 jack_error("Cannot bind server to socket err = %s", strerror(errno));
260 goto error;
263 if (listen(fSocket, 1) < 0) {
264 jack_error("Cannot enable listen on server socket err = %s", strerror(errno));
265 goto error;
268 return 0;
270 error:
271 unlink(fName);
272 close(fSocket);
273 return -1;
276 JackClientSocket* JackServerSocket::Accept()
278 struct sockaddr_un client_addr;
279 socklen_t client_addrlen;
281 memset(&client_addr, 0, sizeof(client_addr));
282 client_addrlen = sizeof(client_addr);
284 int fd = accept(fSocket, (struct sockaddr*)&client_addr, &client_addrlen);
285 if (fd < 0) {
286 jack_error("Cannot accept new connection err = %s", strerror(errno));
287 return 0;
288 } else {
289 return new JackClientSocket(fd);
293 int JackServerSocket::Close()
295 if (fSocket > 0) {
296 jack_log("JackServerSocket::Close %s", fName);
297 shutdown(fSocket, SHUT_RDWR);
298 close(fSocket);
299 unlink(fName);
300 fSocket = -1;
301 return 0;
302 } else {
303 return -1;
307 } // end of namespace