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"
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
): JackClientRequestInterface(), fSocket(socket
),fTimeOut(0)
42 #if defined(__sun__) || defined(sun)
44 void JackClientSocket::SetReadTimeOut(long sec
)
49 if ((flags
= fcntl(fSocket
, F_GETFL
, 0)) < 0) {
50 jack_error("JackClientSocket::SetReadTimeOut error in fcntl F_GETFL");
55 if (fcntl(fSocket
, F_SETFL
, flags
) < 0) {
56 jack_error("JackClientSocket::SetReadTimeOut error in fcntl F_SETFL");
61 void JackClientSocket::SetWriteTimeOut(long sec
)
66 if ((flags
= fcntl(fSocket
, F_GETFL
, 0)) < 0) {
67 jack_error("JackClientSocket::SetWriteTimeOut error in fcntl F_GETFL");
72 if (fcntl(fSocket
, F_SETFL
, flags
) < 0) {
73 jack_error("JackClientSocket::SetWriteTimeOut error in fcntl F_SETFL");
80 void JackClientSocket::SetReadTimeOut(long sec
)
82 struct timeval timout
;
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
;
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
));
102 void JackClientSocket::SetNonBlocking(bool onoff
)
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
));
121 addr
.sun_family
= AF_UNIX
;
122 BuildName(name
, addr
.sun_path
, dir
, which
);
123 jack_log("JackClientSocket::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
));
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
));
141 int JackClientSocket::Close()
143 jack_log("JackClientSocket::Close");
145 shutdown(fSocket
, SHUT_RDWR
);
154 int JackClientSocket::Read(void* data
, int len
)
158 #if defined(__sun__) || defined(sun)
165 tv
.tv_sec
= fTimeOut
;
169 FD_SET(fSocket
, &fdset
);
172 res
= select(fSocket
+ 1, &fdset
, NULL
, NULL
, &tv
);
173 } while (res
< 0 && errno
== EINTR
);
177 } else if (res
== 0) {
183 if ((res
= read(fSocket
, data
, len
)) != len
) {
184 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
) {
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
));
192 jack_error("Cannot read socket fd = %d err = %s", fSocket
, strerror(errno
));
200 int JackClientSocket::Write(void* data
, int len
)
204 #if defined(__sun__) || defined(sun)
211 tv
.tv_sec
= fTimeOut
;
215 FD_SET(fSocket
, &fdset
);
218 res
= select(fSocket
+ 1, NULL
, &fdset
, NULL
, &tv
);
219 } while (res
< 0 && errno
== EINTR
);
223 } else if (res
== 0) {
229 if ((res
= write(fSocket
, data
, len
)) != len
) {
230 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
) {
231 jack_log("JackClientSocket::Write time out");
232 return 0; // For a non blocking socket, a write failure is not considered as an error
233 } else if (res
!= 0) {
234 jack_error("Cannot write socket fd = %ld err = %s", fSocket
, strerror(errno
));
238 jack_error("Cannot write socket fd = %ld err = %s", fSocket
, strerror(errno
));
246 int JackServerSocket::Bind(const char* dir
, const char* name
, int which
) // A revoir : utilisation de "which"
248 struct sockaddr_un addr
;
250 if ((fSocket
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0) {
251 jack_error("Cannot create server socket err = %s", strerror(errno
));
255 addr
.sun_family
= AF_UNIX
;
256 BuildName(name
, fName
, dir
, which
);
257 strncpy(addr
.sun_path
, fName
, sizeof(addr
.sun_path
) - 1);
259 jack_log("JackServerSocket::Bind : addr.sun_path %s", addr
.sun_path
);
260 unlink(fName
); // Security...
262 if (bind(fSocket
, (struct sockaddr
*)&addr
, sizeof(addr
)) < 0) {
263 jack_error("Cannot bind server to socket err = %s", strerror(errno
));
267 if (listen(fSocket
, 100) < 0) {
268 jack_error("Cannot enable listen on server socket err = %s", strerror(errno
));
280 JackClientSocket
* JackServerSocket::Accept()
282 struct sockaddr_un client_addr
;
283 socklen_t client_addrlen
;
285 memset(&client_addr
, 0, sizeof(client_addr
));
286 client_addrlen
= sizeof(client_addr
);
288 int fd
= accept(fSocket
, (struct sockaddr
*)&client_addr
, &client_addrlen
);
290 jack_error("Cannot accept new connection err = %s", strerror(errno
));
293 return new JackClientSocket(fd
);
297 int JackServerSocket::Close()
300 jack_log("JackServerSocket::Close %s", fName
);
301 shutdown(fSocket
, SHUT_RDWR
);
311 } // end of namespace