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.

471 lines
16KB

  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 "JackSocketServerChannel.h"
  16. #include "JackRequest.h"
  17. #include "JackServer.h"
  18. #include "JackLockedEngine.h"
  19. #include "JackGlobals.h"
  20. #include "JackClient.h"
  21. #include "JackNotification.h"
  22. #include <assert.h>
  23. #include <signal.h>
  24. using namespace std;
  25. namespace Jack
  26. {
  27. JackSocketServerChannel::JackSocketServerChannel():
  28. fThread(this)
  29. {
  30. fPollTable = NULL;
  31. fRebuild = true;
  32. }
  33. JackSocketServerChannel::~JackSocketServerChannel()
  34. {
  35. delete[] fPollTable;
  36. }
  37. int JackSocketServerChannel::Open(const char* server_name, JackServer* server)
  38. {
  39. jack_log("JackSocketServerChannel::Open ");
  40. fServer = server;
  41. // Prepare request socket
  42. if (fRequestListenSocket.Bind(jack_server_dir, server_name, 0) < 0) {
  43. jack_log("JackSocketServerChannel::Open : cannot create result listen socket");
  44. return -1;
  45. }
  46. // Prepare for poll
  47. BuildPoolTable();
  48. // Start listening
  49. if (fThread.Start() != 0) {
  50. jack_error("Cannot start Jack server listener");
  51. goto error;
  52. }
  53. return 0;
  54. error:
  55. fRequestListenSocket.Close();
  56. return -1;
  57. }
  58. void JackSocketServerChannel::Close()
  59. {
  60. fThread.Kill();
  61. fRequestListenSocket.Close();
  62. // Close remaining client sockets
  63. std::map<int, std::pair<int, JackClientSocket*> >::iterator it;
  64. for (it = fSocketTable.begin(); it != fSocketTable.end(); it++) {
  65. pair<int, JackClientSocket*> elem = (*it).second;
  66. JackClientSocket* socket = elem.second;
  67. assert(socket);
  68. socket->Close();
  69. delete socket;
  70. }
  71. }
  72. void JackSocketServerChannel::ClientCreate()
  73. {
  74. jack_log("JackSocketServerChannel::ClientCreate socket");
  75. JackClientSocket* socket = fRequestListenSocket.Accept();
  76. if (socket) {
  77. fSocketTable[socket->GetFd()] = make_pair( -1, socket);
  78. fRebuild = true;
  79. } else {
  80. jack_error("Client socket cannot be created");
  81. }
  82. }
  83. void JackSocketServerChannel::ClientAdd(int fd, char* name, int pid, int* shared_engine, int* shared_client, int* shared_graph, int* result)
  84. {
  85. jack_log("JackSocketServerChannel::ClientAdd");
  86. int refnum = -1;
  87. *result = fServer->GetEngine()->ClientExternalOpen(name, pid, &refnum, shared_engine, shared_client, shared_graph);
  88. if (*result == 0) {
  89. fSocketTable[fd].first = refnum;
  90. fRebuild = true;
  91. } else {
  92. jack_error("Cannot create new client");
  93. }
  94. }
  95. void JackSocketServerChannel::ClientRemove(int fd, int refnum)
  96. {
  97. pair<int, JackClientSocket*> elem = fSocketTable[fd];
  98. JackClientSocket* socket = elem.second;
  99. assert(socket);
  100. jack_log("JackSocketServerChannel::ClientRemove ref = %d", refnum);
  101. fSocketTable.erase(fd);
  102. socket->Close();
  103. delete socket;
  104. fRebuild = true;
  105. }
  106. void JackSocketServerChannel::ClientKill(int fd)
  107. {
  108. pair<int, JackClientSocket*> elem = fSocketTable[fd];
  109. JackClientSocket* socket = elem.second;
  110. int refnum = elem.first;
  111. assert(socket);
  112. jack_log("JackSocketServerChannel::ClientKill ref = %d", refnum);
  113. if (refnum == -1) { // Should never happen... correspond to a client that started the socket but never opened...
  114. jack_log("Client was not opened : probably correspond to server_check");
  115. } else {
  116. fServer->ClientKill(refnum);
  117. }
  118. fSocketTable.erase(fd);
  119. socket->Close();
  120. delete socket;
  121. fRebuild = true;
  122. }
  123. bool JackSocketServerChannel::HandleRequest(int fd)
  124. {
  125. pair<int, JackClientSocket*> elem = fSocketTable[fd];
  126. JackClientSocket* socket = elem.second;
  127. assert(socket);
  128. // Read header
  129. JackRequest header;
  130. if (header.Read(socket) < 0) {
  131. jack_error("HandleRequest: cannot read header");
  132. return false;
  133. }
  134. // Read data
  135. switch (header.fType) {
  136. case JackRequest::kClientCheck: {
  137. jack_log("JackRequest::kClientCheck");
  138. JackClientCheckRequest req;
  139. JackClientCheckResult res;
  140. if (req.Read(socket) == 0)
  141. res.fResult = fServer->GetEngine()->ClientCheck(req.fName, res.fName, req.fProtocol, req.fOptions, &res.fStatus);
  142. if (res.Write(socket) < 0)
  143. jack_error("JackRequest::kClientCheck write error name = %s", req.fName);
  144. break;
  145. }
  146. case JackRequest::kClientOpen: {
  147. jack_log("JackRequest::ClientOpen");
  148. JackClientOpenRequest req;
  149. JackClientOpenResult res;
  150. if (req.Read(socket) == 0)
  151. ClientAdd(fd, req.fName, req.fPID, &res.fSharedEngine, &res.fSharedClient, &res.fSharedGraph, &res.fResult);
  152. if (res.Write(socket) < 0)
  153. jack_error("JackRequest::ClientOpen write error name = %s", req.fName);
  154. break;
  155. }
  156. case JackRequest::kClientClose: {
  157. jack_log("JackRequest::ClientClose");
  158. JackClientCloseRequest req;
  159. JackResult res;
  160. if (req.Read(socket) == 0)
  161. res.fResult = fServer->GetEngine()->ClientExternalClose(req.fRefNum);
  162. if (res.Write(socket) < 0)
  163. jack_error("JackRequest::ClientClose write error ref = %d", req.fRefNum);
  164. ClientRemove(fd, req.fRefNum);
  165. break;
  166. }
  167. case JackRequest::kActivateClient: {
  168. JackActivateRequest req;
  169. JackResult res;
  170. jack_log("JackRequest::ActivateClient");
  171. if (req.Read(socket) == 0)
  172. res.fResult = fServer->GetEngine()->ClientActivate(req.fRefNum, req.fState);
  173. if (res.Write(socket) < 0)
  174. jack_error("JackRequest::ActivateClient write error ref = %d", req.fRefNum);
  175. break;
  176. }
  177. case JackRequest::kDeactivateClient: {
  178. jack_log("JackRequest::DeactivateClient");
  179. JackDeactivateRequest req;
  180. JackResult res;
  181. if (req.Read(socket) == 0)
  182. res.fResult = fServer->GetEngine()->ClientDeactivate(req.fRefNum);
  183. if (res.Write(socket) < 0)
  184. jack_error("JackRequest::DeactivateClient write error ref = %d", req.fRefNum);
  185. break;
  186. }
  187. case JackRequest::kRegisterPort: {
  188. jack_log("JackRequest::RegisterPort");
  189. JackPortRegisterRequest req;
  190. JackPortRegisterResult res;
  191. if (req.Read(socket) == 0)
  192. res.fResult = fServer->GetEngine()->PortRegister(req.fRefNum, req.fName, req.fPortType, req.fFlags, req.fBufferSize, &res.fPortIndex);
  193. if (res.Write(socket) < 0)
  194. jack_error("JackRequest::RegisterPort write error ref = %d", req.fRefNum);
  195. break;
  196. }
  197. case JackRequest::kUnRegisterPort: {
  198. jack_log("JackRequest::UnRegisterPort");
  199. JackPortUnRegisterRequest req;
  200. JackResult res;
  201. if (req.Read(socket) == 0)
  202. res.fResult = fServer->GetEngine()->PortUnRegister(req.fRefNum, req.fPortIndex);
  203. if (res.Write(socket) < 0)
  204. jack_error("JackRequest::UnRegisterPort write error ref = %d", req.fRefNum);
  205. break;
  206. }
  207. case JackRequest::kConnectNamePorts: {
  208. jack_log("JackRequest::ConnectPorts");
  209. JackPortConnectNameRequest req;
  210. JackResult res;
  211. if (req.Read(socket) == 0)
  212. res.fResult = fServer->GetEngine()->PortConnect(req.fRefNum, req.fSrc, req.fDst);
  213. if (res.Write(socket) < 0)
  214. jack_error("JackRequest::ConnectNamePorts write error ref = %d", req.fRefNum);
  215. break;
  216. }
  217. case JackRequest::kDisconnectNamePorts: {
  218. jack_log("JackRequest::DisconnectPorts");
  219. JackPortDisconnectNameRequest req;
  220. JackResult res;
  221. if (req.Read(socket) == 0)
  222. res.fResult = fServer->GetEngine()->PortDisconnect(req.fRefNum, req.fSrc, req.fDst);
  223. if (res.Write(socket) < 0)
  224. jack_error("JackRequest::DisconnectNamePorts write error ref = %d", req.fRefNum);
  225. break;
  226. }
  227. case JackRequest::kConnectPorts: {
  228. jack_log("JackRequest::ConnectPorts");
  229. JackPortConnectRequest req;
  230. JackResult res;
  231. if (req.Read(socket) == 0)
  232. res.fResult = fServer->GetEngine()->PortConnect(req.fRefNum, req.fSrc, req.fDst);
  233. if (res.Write(socket) < 0)
  234. jack_error("JackRequest::ConnectPorts write error ref = %d", req.fRefNum);
  235. break;
  236. }
  237. case JackRequest::kDisconnectPorts: {
  238. jack_log("JackRequest::DisconnectPorts");
  239. JackPortDisconnectRequest req;
  240. JackResult res;
  241. if (req.Read(socket) == 0)
  242. res.fResult = fServer->GetEngine()->PortDisconnect(req.fRefNum, req.fSrc, req.fDst);
  243. if (res.Write(socket) < 0)
  244. jack_error("JackRequest::DisconnectPorts write error ref = %d", req.fRefNum);
  245. break;
  246. }
  247. case JackRequest::kPortRename: {
  248. jack_log("JackRequest::kPortRename");
  249. JackPortRenameRequest req;
  250. JackResult res;
  251. if (req.Read(socket) == 0)
  252. res.fResult = fServer->GetEngine()->PortRename(req.fRefNum, req.fPort, req.fName);
  253. if (res.Write(socket) < 0)
  254. jack_error("JackRequest::PortRename write error ref = %d", req.fRefNum);
  255. break;
  256. }
  257. case JackRequest::kSetBufferSize: {
  258. jack_log("JackRequest::SetBufferSize");
  259. JackSetBufferSizeRequest req;
  260. JackResult res;
  261. if (req.Read(socket) == 0)
  262. res.fResult = fServer->SetBufferSize(req.fBufferSize);
  263. if (res.Write(socket) < 0)
  264. jack_error("JackRequest::SetBufferSize write error");
  265. break;
  266. }
  267. case JackRequest::kSetFreeWheel: {
  268. jack_log("JackRequest::SetFreeWheel");
  269. JackSetFreeWheelRequest req;
  270. JackResult res;
  271. if (req.Read(socket) == 0)
  272. res.fResult = fServer->SetFreewheel(req.fOnOff);
  273. if (res.Write(socket) < 0)
  274. jack_error("JackRequest::SetFreeWheel write error");
  275. break;
  276. }
  277. case JackRequest::kReleaseTimebase: {
  278. jack_log("JackRequest::kReleaseTimebase");
  279. JackReleaseTimebaseRequest req;
  280. JackResult res;
  281. if (req.Read(socket) == 0)
  282. res.fResult = fServer->ReleaseTimebase(req.fRefNum);
  283. if (res.Write(socket) < 0)
  284. jack_error("JackRequest::kReleaseTimebase write error ref = %d", req.fRefNum);
  285. break;
  286. }
  287. case JackRequest::kSetTimebaseCallback: {
  288. jack_log("JackRequest::kSetTimebaseCallback");
  289. JackSetTimebaseCallbackRequest req;
  290. JackResult res;
  291. if (req.Read(socket) == 0)
  292. res.fResult = fServer->SetTimebaseCallback(req.fRefNum, req.fConditionnal);
  293. if (res.Write(socket) < 0)
  294. jack_error("JackRequest::SetTimebaseCallback write error ref = %d", req.fRefNum);
  295. break;
  296. }
  297. case JackRequest::kGetInternalClientName: {
  298. jack_log("JackRequest::kGetInternalClientName");
  299. JackGetInternalClientNameRequest req;
  300. JackGetInternalClientNameResult res;
  301. if (req.Read(socket) == 0)
  302. res.fResult = fServer->GetEngine()->GetInternalClientName(req.fIntRefNum, res.fName);
  303. if (res.Write(socket) < 0)
  304. jack_error("JackRequest::GetInternalClientName write error ref = %d", req.fRefNum);
  305. break;
  306. }
  307. case JackRequest::kInternalClientHandle: {
  308. jack_log("JackRequest::kInternalClientHandle");
  309. JackInternalClientHandleRequest req;
  310. JackInternalClientHandleResult res;
  311. if (req.Read(socket) == 0)
  312. res.fResult = fServer->GetEngine()->InternalClientHandle(req.fName, &res.fStatus, &res.fIntRefNum);
  313. if (res.Write(socket) < 0)
  314. jack_error("JackRequest::InternalClientHandle write error ref = %d", req.fRefNum);
  315. break;
  316. }
  317. case JackRequest::kInternalClientLoad: {
  318. jack_log("JackRequest::kInternalClientLoad");
  319. JackInternalClientLoadRequest req;
  320. JackInternalClientLoadResult res;
  321. if (req.Read(socket) == 0)
  322. res.fResult = fServer->InternalClientLoad(req.fName, req.fDllName, req.fLoadInitName, req.fOptions, &res.fIntRefNum, &res.fStatus);
  323. if (res.Write(socket) < 0)
  324. jack_error("JackRequest::InternalClientLoad write error name = %s", req.fName);
  325. break;
  326. }
  327. case JackRequest::kInternalClientUnload: {
  328. jack_log("JackRequest::kInternalClientUnload");
  329. JackInternalClientUnloadRequest req;
  330. JackInternalClientUnloadResult res;
  331. if (req.Read(socket) == 0)
  332. res.fResult = fServer->GetEngine()->InternalClientUnload(req.fIntRefNum, &res.fStatus);
  333. if (res.Write(socket) < 0)
  334. jack_error("JackRequest::InternalClientUnload write error ref = %d", req.fRefNum);
  335. break;
  336. }
  337. case JackRequest::kNotification: {
  338. jack_log("JackRequest::Notification");
  339. JackClientNotificationRequest req;
  340. if (req.Read(socket) == 0)
  341. fServer->Notify(req.fRefNum, req.fNotify, req.fValue);
  342. break;
  343. }
  344. default:
  345. jack_log("Unknown request %ld", header.fType);
  346. break;
  347. }
  348. return true;
  349. }
  350. void JackSocketServerChannel::BuildPoolTable()
  351. {
  352. if (fRebuild) {
  353. fRebuild = false;
  354. delete[] fPollTable;
  355. fPollTable = new pollfd[fSocketTable.size() + 1];
  356. jack_log("JackSocketServerChannel::BuildPoolTable size = %d", fSocketTable.size() + 1);
  357. // First fd is the server request socket
  358. fPollTable[0].fd = fRequestListenSocket.GetFd();
  359. fPollTable[0].events = POLLIN | POLLERR;
  360. // Next fd for clients
  361. map<int, pair<int, JackClientSocket*> >::iterator it;
  362. int i;
  363. for (i = 1, it = fSocketTable.begin(); it != fSocketTable.end(); it++, i++) {
  364. jack_log("fSocketTable i = %ld fd = %ld", i, it->first);
  365. fPollTable[i].fd = it->first;
  366. fPollTable[i].events = POLLIN | POLLPRI | POLLERR | POLLHUP | POLLNVAL;
  367. }
  368. }
  369. }
  370. bool JackSocketServerChannel::Init()
  371. {
  372. sigset_t set;
  373. sigemptyset(&set);
  374. sigaddset(&set, SIGPIPE);
  375. pthread_sigmask(SIG_BLOCK, &set, 0);
  376. return true;
  377. }
  378. bool JackSocketServerChannel::Execute()
  379. {
  380. // Global poll
  381. if ((poll(fPollTable, fSocketTable.size() + 1, 10000) < 0) && (errno != EINTR)) {
  382. jack_error("Engine poll failed err = %s request thread quits...", strerror(errno));
  383. return false;
  384. } else {
  385. // Poll all clients
  386. for (unsigned int i = 1; i < fSocketTable.size() + 1; i++) {
  387. int fd = fPollTable[i].fd;
  388. jack_log("fPollTable i = %ld fd = %ld", i, fd);
  389. if (fPollTable[i].revents & ~POLLIN) {
  390. jack_log("Poll client error err = %s", strerror(errno));
  391. ClientKill(fd);
  392. } else if (fPollTable[i].revents & POLLIN) {
  393. if (!HandleRequest(fd))
  394. jack_error("Could not handle external client request");
  395. }
  396. }
  397. // Check the server request socket */
  398. if (fPollTable[0].revents & POLLERR)
  399. jack_error("Error on server request socket err = %s", strerror(errno));
  400. if (fPollTable[0].revents & POLLIN)
  401. ClientCreate();
  402. }
  403. BuildPoolTable();
  404. return true;
  405. }
  406. } // end of namespace