jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

287 lines
7.8KB

  1. /*
  2. Copyright (C) 2004-2008 Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2.1 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #include "JackSocket.h"
  16. #include "JackError.h"
  17. #include <string.h>
  18. #include <stdio.h>
  19. #include <pthread.h>
  20. namespace Jack
  21. {
  22. JackClientSocket::JackClientSocket(int socket): fSocket(socket)
  23. {}
  24. void JackClientSocket::SetReadTimeOut(long sec)
  25. {
  26. struct timeval timout;
  27. timout.tv_sec = sec;
  28. timout.tv_usec = 0;
  29. if (setsockopt(fSocket, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timout, sizeof(timeval)) < 0) {
  30. jack_error("SetReadTimeOut fd = %ld err = %s", fSocket, strerror(errno));
  31. }
  32. }
  33. void JackClientSocket::SetWriteTimeOut(long sec)
  34. {
  35. struct timeval timout;
  36. timout.tv_sec = sec ;
  37. timout.tv_usec = 0;
  38. if (setsockopt(fSocket, SOL_SOCKET, SO_SNDTIMEO, (const char*)&timout, sizeof(timeval)) < 0) {
  39. jack_error("SetWriteTimeOut fd = %ld err = %s", fSocket, strerror(errno));
  40. }
  41. }
  42. void JackClientSocket::SetNonBlocking(bool onoff)
  43. {
  44. int flag = (onoff) ? 1 : 0;
  45. if (ioctl(fSocket, FIONBIO, &flag) < 0) {
  46. jack_error("SetNonBlocking fd = %ld err = %s", fSocket, strerror(errno));
  47. }
  48. }
  49. int JackClientSocket::Connect(const char* dir, const char* name, int which) // A revoir : utilisation de "which"
  50. {
  51. struct sockaddr_un addr;
  52. if ((fSocket = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
  53. jack_error("Cannot create socket err = %s", strerror(errno));
  54. return -1;
  55. }
  56. addr.sun_family = AF_UNIX;
  57. snprintf(addr.sun_path, sizeof(addr.sun_path) - 1, "%s/jack_%s_%d", dir, name, which);
  58. jack_log("Connect: addr.sun_path %s", addr.sun_path);
  59. if (connect(fSocket, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
  60. jack_error("Cannot connect to server socket err = %s", strerror(errno));
  61. close(fSocket);
  62. return -1;
  63. }
  64. #ifdef __APPLE__
  65. int on = 1 ;
  66. if (setsockopt(fSocket, SOL_SOCKET, SO_NOSIGPIPE, (const char*)&on, sizeof(on)) < 0) {
  67. jack_log("setsockopt SO_NOSIGPIPE fd = %ld err = %s", fSocket, strerror(errno));
  68. }
  69. #endif
  70. return 0;
  71. }
  72. int JackClientSocket::Connect(const char* dir, int which)
  73. {
  74. struct sockaddr_un addr;
  75. if ((fSocket = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
  76. jack_error("Cannot create socket err = %s", strerror(errno));
  77. return -1;
  78. }
  79. addr.sun_family = AF_UNIX;
  80. snprintf(addr.sun_path, sizeof(addr.sun_path) - 1, "%s/jack_%d", dir, which);
  81. jack_log("Connect: addr.sun_path %s", addr.sun_path);
  82. if (connect(fSocket, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
  83. jack_error("Cannot connect to server socket err = %s", strerror(errno));
  84. close(fSocket);
  85. return -1;
  86. }
  87. #ifdef __APPLE__
  88. int on = 1 ;
  89. if (setsockopt(fSocket, SOL_SOCKET, SO_NOSIGPIPE, (const char*)&on, sizeof(on)) < 0) {
  90. jack_log("setsockopt SO_NOSIGPIPE fd = %ld err = %s", fSocket, strerror(errno));
  91. }
  92. #endif
  93. return 0;
  94. }
  95. int JackClientSocket::Close()
  96. {
  97. jack_log("JackClientSocket::Close");
  98. if (fSocket > 0) {
  99. shutdown(fSocket, SHUT_RDWR);
  100. close(fSocket);
  101. fSocket = -1;
  102. return 0;
  103. } else {
  104. return -1;
  105. }
  106. }
  107. int JackClientSocket::Read(void* data, int len)
  108. {
  109. if (read(fSocket, data, len) != len) {
  110. jack_error("Cannot read socket fd = %d err = %s", fSocket, strerror(errno));
  111. if (errno == EWOULDBLOCK) {
  112. jack_error("JackClientSocket::Read time out");
  113. return 0; // For a non blocking socket, a read failure is not considered as an error
  114. } else {
  115. return -1;
  116. }
  117. } else {
  118. return 0;
  119. }
  120. }
  121. int JackClientSocket::Write(void* data, int len)
  122. {
  123. if (write(fSocket, data, len) != len) {
  124. jack_error("Cannot write socket fd = %ld err = %s", fSocket, strerror(errno));
  125. if (errno == EWOULDBLOCK) {
  126. jack_log("JackClientSocket::Write time out");
  127. return 0; // For a non blocking socket, a write failure is not considered as an error
  128. } else {
  129. return -1;
  130. }
  131. } else {
  132. return 0;
  133. }
  134. }
  135. int JackServerSocket::Bind(const char* dir, const char* name, int which) // A revoir : utilisation de "which"
  136. {
  137. struct sockaddr_un addr;
  138. if ((fSocket = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
  139. jack_error("Cannot create server socket err = %s", strerror(errno));
  140. return -1;
  141. }
  142. addr.sun_family = AF_UNIX;
  143. // TO CORRECT: always reuse the same name for now...
  144. snprintf(fName, sizeof(addr.sun_path) - 1, "%s/jack_%s_%d", dir, name, which);
  145. strncpy(addr.sun_path, fName, sizeof(addr.sun_path) - 1);
  146. /*
  147. if (access(addr.sun_path, F_OK) == 0) {
  148. goto error;
  149. }
  150. */
  151. jack_log("Bind: addr.sun_path %s", addr.sun_path);
  152. unlink(fName); // Security...
  153. if (bind(fSocket, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
  154. jack_error("Cannot bind server to socket err = %s", strerror(errno));
  155. goto error;
  156. }
  157. if (listen(fSocket, 1) < 0) {
  158. jack_error("Cannot enable listen on server socket err = %s", strerror(errno));
  159. goto error;
  160. }
  161. return 0;
  162. error:
  163. unlink(fName);
  164. close(fSocket);
  165. return -1;
  166. }
  167. int JackServerSocket::Bind(const char* dir, int which) // A revoir : utilisation de "which"
  168. {
  169. struct sockaddr_un addr;
  170. if ((fSocket = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
  171. jack_error("Cannot create server socket err = %s", strerror(errno));
  172. return -1;
  173. }
  174. addr.sun_family = AF_UNIX;
  175. /*
  176. for (int i = 0; i < 999; i++) {
  177. snprintf(addr.sun_path, sizeof(addr.sun_path) - 1,"%s/jack_%d", dir, i);
  178. snprintf(fName, sizeof(addr.sun_path) - 1,"%s/jack_%d", dir, i);
  179. if (access(addr.sun_path, F_OK) != 0) {
  180. break;
  181. }
  182. }
  183. */
  184. // TO CORRECT: always reuse the same name for now...
  185. snprintf(fName, sizeof(addr.sun_path) - 1, "%s/jack_%d", dir, which);
  186. strncpy(addr.sun_path, fName, sizeof(addr.sun_path) - 1);
  187. /*
  188. if (access(addr.sun_path, F_OK) == 0) {
  189. goto error;
  190. }
  191. */
  192. jack_log("Bind: addr.sun_path %s", addr.sun_path);
  193. unlink(fName); // Security...
  194. if (bind(fSocket, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
  195. jack_error("Cannot bind server to socket err = %s", strerror(errno));
  196. goto error;
  197. }
  198. if (listen(fSocket, 1) < 0) {
  199. jack_error("Cannot enable listen on server socket err = %s", strerror(errno));
  200. goto error;
  201. }
  202. return 0;
  203. error:
  204. unlink(fName);
  205. close(fSocket);
  206. return -1;
  207. }
  208. JackClientSocket* JackServerSocket::Accept()
  209. {
  210. struct sockaddr_un client_addr;
  211. socklen_t client_addrlen;
  212. memset(&client_addr, 0, sizeof(client_addr));
  213. client_addrlen = sizeof(client_addr);
  214. int fd = accept(fSocket, (struct sockaddr*) & client_addr, &client_addrlen);
  215. if (fd < 0) {
  216. jack_error("Cannot accept new connection err = %s", strerror(errno));
  217. return 0;
  218. } else {
  219. return new JackClientSocket(fd);
  220. }
  221. }
  222. int JackServerSocket::Close()
  223. {
  224. if (fSocket > 0) {
  225. jack_log("JackServerSocket::Close %s", fName);
  226. shutdown(fSocket, SHUT_RDWR);
  227. close(fSocket);
  228. unlink(fName);
  229. fSocket = -1;
  230. return 0;
  231. } else {
  232. return -1;
  233. }
  234. }
  235. } // end of namespace