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 "JackSocketServerChannel.h"
21 #include "JackRequest.h"
22 #include "JackServer.h"
23 #include "JackLockedEngine.h"
24 #include "JackGlobals.h"
25 #include "JackServerGlobals.h"
26 #include "JackClient.h"
27 #include "JackTools.h"
28 #include "JackNotification.h"
29 #include "JackException.h"
39 JackSocketServerChannel::JackSocketServerChannel():
40 fThread(this), fDecoder(NULL
)
46 JackSocketServerChannel::~JackSocketServerChannel()
51 int JackSocketServerChannel::Open(const char* server_name
, JackServer
* server
)
53 jack_log("JackSocketServerChannel::Open");
55 // Prepare request socket
56 if (fRequestListenSocket
.Bind(jack_server_dir
, server_name
, 0) < 0) {
57 jack_log("JackSocketServerChannel::Open : cannot create result listen socket");
64 fDecoder
= new JackRequestDecoder(server
, this);
69 void JackSocketServerChannel::Close()
71 fRequestListenSocket
.Close();
73 // Close remaining client sockets
74 std::map
<int, std::pair
<int, JackClientSocket
*> >::iterator it
;
76 for (it
= fSocketTable
.begin(); it
!= fSocketTable
.end(); it
++) {
77 pair
<int, JackClientSocket
*> elem
= (*it
).second
;
78 JackClientSocket
* socket
= elem
.second
;
88 int JackSocketServerChannel::Start()
90 if (fThread
.Start() != 0) {
91 jack_error("Cannot start Jack server listener");
98 void JackSocketServerChannel::Stop()
103 void JackSocketServerChannel::ClientCreate()
105 jack_log("JackSocketServerChannel::ClientCreate socket");
106 JackClientSocket
* socket
= fRequestListenSocket
.Accept();
108 fSocketTable
[socket
->GetFd()] = make_pair(-1, socket
);
111 jack_error("Client socket cannot be created");
115 int JackSocketServerChannel::GetFd(JackClientSocket
* socket_aux
)
117 std::map
<int, std::pair
<int, JackClientSocket
*> >::iterator it
;
119 for (it
= fSocketTable
.begin(); it
!= fSocketTable
.end(); it
++) {
120 pair
<int, JackClientSocket
*> elem
= (*it
).second
;
121 JackClientSocket
* socket
= elem
.second
;
122 if (socket_aux
== socket
) {
129 void JackSocketServerChannel::ClientAdd(detail::JackChannelTransactionInterface
* socket_aux
, JackClientOpenRequest
* req
, JackClientOpenResult
*res
)
132 res
->fResult
= fServer
->GetEngine()->ClientExternalOpen(req
->fName
, req
->fPID
, req
->fUUID
, &refnum
, &res
->fSharedEngine
, &res
->fSharedClient
, &res
->fSharedGraph
);
133 if (res
->fResult
== 0) {
134 JackClientSocket
* socket
= dynamic_cast<JackClientSocket
*>(socket_aux
);
136 int fd
= GetFd(socket
);
138 fSocketTable
[fd
].first
= refnum
;
140 jack_log("JackSocketServerChannel::ClientAdd ref = %d fd = %d", refnum
, fd
);
143 if (setsockopt(fd
, SOL_SOCKET
, SO_NOSIGPIPE
, (const char*)&on
, sizeof(on
)) < 0) {
144 jack_log("JackSocketServerChannel::ClientAdd : setsockopt SO_NOSIGPIPE fd = %ld err = %s", fd
, strerror(errno
));
148 jack_error("Cannot create new client");
152 void JackSocketServerChannel::ClientRemove(detail::JackChannelTransactionInterface
* socket_aux
, int refnum
)
154 JackClientSocket
* socket
= dynamic_cast<JackClientSocket
*>(socket_aux
);
156 int fd
= GetFd(socket
);
159 jack_log("JackSocketServerChannel::ClientRemove ref = %d fd = %d", refnum
, fd
);
160 fSocketTable
.erase(fd
);
166 void JackSocketServerChannel::ClientKill(int fd
)
168 pair
<int, JackClientSocket
*> elem
= fSocketTable
[fd
];
169 JackClientSocket
* socket
= elem
.second
;
170 int refnum
= elem
.first
;
173 jack_log("JackSocketServerChannel::ClientKill ref = %d fd = %d", refnum
, fd
);
174 if (refnum
== -1) { // Should never happen... correspond to a client that started the socket but never opened...
175 jack_log("Client was not opened : probably correspond to server_check");
177 fServer
->GetEngine()->ClientKill(refnum
);
180 fSocketTable
.erase(fd
);
186 void JackSocketServerChannel::BuildPoolTable()
191 fPollTable
= new pollfd
[fSocketTable
.size() + 1];
193 jack_log("JackSocketServerChannel::BuildPoolTable size = %d", fSocketTable
.size() + 1);
195 // First fd is the server request socket
196 fPollTable
[0].fd
= fRequestListenSocket
.GetFd();
197 fPollTable
[0].events
= POLLIN
| POLLERR
;
199 // Next fd for clients
200 map
<int, pair
<int, JackClientSocket
*> >::iterator it
;
203 for (i
= 1, it
= fSocketTable
.begin(); it
!= fSocketTable
.end(); it
++, i
++) {
204 jack_log("JackSocketServerChannel::BuildPoolTable fSocketTable i = %ld fd = %ld", i
, it
->first
);
205 fPollTable
[i
].fd
= it
->first
;
206 fPollTable
[i
].events
= POLLIN
| POLLPRI
| POLLERR
| POLLHUP
| POLLNVAL
;
211 bool JackSocketServerChannel::Init()
215 sigaddset(&set
, SIGPIPE
);
216 pthread_sigmask(SIG_BLOCK
, &set
, 0);
220 bool JackSocketServerChannel::Execute()
225 if ((poll(fPollTable
, fSocketTable
.size() + 1, 10000) < 0) && (errno
!= EINTR
)) {
226 jack_error("JackSocketServerChannel::Execute : engine poll failed err = %s request thread quits...", strerror(errno
));
231 for (unsigned int i
= 1; i
< fSocketTable
.size() + 1; i
++) {
232 int fd
= fPollTable
[i
].fd
;
233 jack_log("JackSocketServerChannel::Execute : fPollTable i = %ld fd = %ld", i
, fd
);
234 if (fPollTable
[i
].revents
& ~POLLIN
) {
235 jack_log("JackSocketServerChannel::Execute : poll client error err = %s", strerror(errno
));
237 } else if (fPollTable
[i
].revents
& POLLIN
) {
238 JackClientSocket
* socket
= fSocketTable
[fd
].second
;
241 if (header
.Read(socket
) < 0) {
242 jack_log("JackSocketServerChannel::Execute : cannot decode header");
246 // Result is not needed here
247 fDecoder
->HandleRequest(socket
, header
.fType
);
252 // Check the server request socket */
253 if (fPollTable
[0].revents
& POLLERR
) {
254 jack_error("Error on server request socket err = %s", strerror(errno
));
257 if (fPollTable
[0].revents
& POLLIN
) {
265 } catch (JackQuitException
& e
) {
266 jack_log("JackSocketServerChannel::Execute : JackQuitException");
271 } // end of namespace