JackWinNamedPipeServerChannel.cpp 10.2 KB
Newer Older
sletz's avatar
sletz committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
Copyright (C) 2004-2006 Grame  
  
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as published by
  the Free Software Foundation; either version 2.1 of the License, or
  (at your option) any later version.
  
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU Lesser General Public License for more details.
  
  You should have received a copy of the GNU Lesser General Public License
  along with this program; if not, write to the Free Software 
  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

*/

#include "JackWinNamedPipeServerChannel.h"
#include "JackRequest.h"
#include "JackServer.h"
#include "JackEngine.h"
#include "JackGlobals.h"
#include "JackClient.h"
#include <assert.h>

using namespace std;

namespace Jack
{

sletz's avatar
Cleanup    
sletz committed
33
HANDLE JackClientPipeThread::fMutex = NULL;  // never released....
sletz's avatar
sletz committed
34

35
// fRefNum = -1 correspond to already removed client
sletz's avatar
sletz committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

JackClientPipeThread::JackClientPipeThread(JackWinNamedPipeClient* pipe)
        : fPipe(pipe), fServer(NULL), fRefNum(0)
{
    fThread = JackGlobals::MakeThread(this);
    if (fMutex == NULL)
        fMutex = CreateMutex(NULL, FALSE, NULL);
}

JackClientPipeThread::~JackClientPipeThread()
{
    delete fPipe;
    delete fThread;
}

int JackClientPipeThread::Open(JackServer* server)	// Open the Server/Client connection
{
    fServer = server;

    // Start listening
    if (fThread->Start() != 0){
        jack_error("Cannot start Jack server listener\n");
        return -1;
    } else {
        return 0;
    }
}

void JackClientPipeThread::Close()					// Close the Server/Client connection
{
    fThread->Kill();
}
68
69
70
71
72

bool JackClientPipeThread::Execute()
{
    JackLog("JackClientPipeThread::Execute\n");
	return (HandleRequest() == 0);
sletz's avatar
sletz committed
73
74
}

75
int JackClientPipeThread::HandleRequest()
sletz's avatar
sletz committed
76
77
{
    // Read header
78
79
80
    JackRequest header;
	int res = header.Read(fPipe);
	int ret = 0;
sletz's avatar
sletz committed
81
82
83

    // Lock the global mutex
    if (WaitForSingleObject(fMutex, INFINITE) == WAIT_FAILED)
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
        jack_error("JackClientPipeThread::HandleRequest: mutex wait error");

	if (res < 0) {
		jack_error("HandleRequest: cannot read header");
		KillClient();
		ret = -1;
	} else {

		// Read data
		switch (header.fType) {
			
		case JackRequest::kClientNew: {
			JackLog("JackRequest::ClientNew\n");
			JackClientNewRequest req;
			JackClientNewResult res;
			if (req.Read(fPipe) == 0) 
				AddClient(req.fName, &res.fSharedEngine, &res.fSharedClient, &res.fSharedPorts, &res.fResult);	
			res.Write(fPipe);
			break;
		}
			
		case JackRequest::kClientClose: {
			JackLog("JackRequest::ClientClose\n");
			JackClientCloseRequest req;
			JackResult res;
			if (req.Read(fPipe) == 0) 
				res.fResult = fServer->GetEngine()->ClientClose(req.fRefNum);		
			res.Write(fPipe);
			RemoveClient(req.fRefNum);
			break;
		}
			
		case JackRequest::kActivateClient: {
			JackActivateRequest req;
			JackResult res;
			JackLog("JackRequest::ActivateClient\n");
			if (req.Read(fPipe) == 0) 
				res.fResult = fServer->Activate(req.fRefNum);
			res.Write(fPipe);
			break;
		}
			
		case JackRequest::kDeactivateClient: {
			JackLog("JackRequest::DeactivateClient\n");
			JackDeactivateRequest req;
			JackResult res;
			if (req.Read(fPipe) == 0) 
				res.fResult = fServer->Deactivate(req.fRefNum);	
			res.Write(fPipe);
			break;
		}
			
		case JackRequest::kRegisterPort: {
			JackLog("JackRequest::RegisterPort\n");
			JackPortRegisterRequest req;
			JackPortRegisterResult res;
			if (req.Read(fPipe) == 0) 
				res.fResult = fServer->GetEngine()->PortRegister(req.fRefNum, req.fName, req.fFlags, req.fBufferSize, &res.fPortIndex);		
			res.Write(fPipe);
			break;
		}
			
		case JackRequest::kUnRegisterPort: {
			JackLog("JackRequest::UnRegisterPort\n");
			JackPortUnRegisterRequest req;
			JackResult res;
			if (req.Read(fPipe) == 0) 
				res.fResult = fServer->GetEngine()->PortUnRegister(req.fRefNum, req.fPortIndex);		
			
			res.Write(fPipe);
			break;
		}
			
		case JackRequest::kConnectNamePorts: {
			JackLog("JackRequest::ConnectPorts\n");
			JackPortConnectNameRequest req;
			JackResult res;
			if (req.Read(fPipe) == 0) 
				res.fResult = fServer->GetEngine()->PortConnect(req.fRefNum, req.fSrc, req.fDst);			
			res.Write(fPipe);
			break;
		}
			
		case JackRequest::kDisconnectNamePorts: {
			JackLog("JackRequest::DisconnectPorts\n");
			JackPortDisconnectNameRequest req;
			JackResult res;
			if (req.Read(fPipe) == 0) 
				res.fResult = fServer->GetEngine()->PortDisconnect(req.fRefNum, req.fSrc, req.fDst);
			res.Write(fPipe);
			break;
		}
			
		case JackRequest::kConnectPorts: {
			JackLog("JackRequest::ConnectPorts\n");
			JackPortConnectRequest req;
			JackResult res;
			if (req.Read(fPipe) == 0) 
				res.fResult = fServer->GetEngine()->PortConnect(req.fRefNum, req.fSrc, req.fDst);
			res.Write(fPipe);
			break;
		}
			
		case JackRequest::kDisconnectPorts: {
			JackLog("JackRequest::DisconnectPorts\n");
			JackPortDisconnectRequest req;
			JackResult res;
			if (req.Read(fPipe) == 0) 
				res.fResult = fServer->GetEngine()->PortDisconnect(req.fRefNum, req.fSrc, req.fDst);
			res.Write(fPipe);
			break;
		}
			
		case JackRequest::kSetBufferSize: {
			JackLog("JackRequest::SetBufferSize\n");
			JackSetBufferSizeRequest req;
			JackResult res;
			if (req.Read(fPipe) == 0) 
				res.fResult = fServer->SetBufferSize(req.fBufferSize);
			res.Write(fPipe);
			break;
		}
			
		case JackRequest::kSetFreeWheel: {
			JackLog("JackRequest::SetFreeWheel\n");
			JackSetFreeWheelRequest req;
			JackResult res;
			if (req.Read(fPipe) == 0) 
				res.fResult = fServer->SetFreewheel(req.fOnOff);
			res.Write(fPipe);
			break;
		}
			
		case JackRequest::kReleaseTimebase: {
			JackLog("JackRequest::kReleaseTimebase\n");
			JackReleaseTimebaseRequest req;
			JackResult res;
			if (req.Read(fPipe) == 0) 
				res.fResult = fServer->GetEngine()->ReleaseTimebase(req.fRefNum);
			res.Write(fPipe);
			break;
		}
			
		case JackRequest::kSetTimebaseCallback: {
			JackLog("JackRequest::kSetTimebaseCallback\n");
			JackSetTimebaseCallbackRequest req;
			JackResult res;
			if (req.Read(fPipe) == 0) 
				res.fResult = fServer->GetEngine()->SetTimebaseCallback(req.fRefNum, req.fConditionnal);
			res.Write(fPipe);
			break;
		}
			
		case JackRequest::kNotification: {
			JackLog("JackRequest::Notification\n");
			JackClientNotificationRequest req;
			if (req.Read(fPipe) == 0) 
				fServer->Notify(req.fRefNum, req.fNotify, req.fValue);
			break;
		}
			
		default:
			JackLog("Unknown request %ld\n", header.fType);
			break;
		}
	}
sletz's avatar
sletz committed
250
251
252

    // Unlock the global mutex
    ReleaseMutex(fMutex);
253
    return ret;
sletz's avatar
sletz committed
254
255
}

256
void JackClientPipeThread::AddClient(char* name, int* shared_engine, int* shared_client, int* shared_ports, int* result)
sletz's avatar
sletz committed
257
258
259
260
261
262
{
    JackLog("JackWinNamedPipeServerChannel::AddClient %s\n", name);
    fRefNum = -1;
    *result = fServer->GetEngine()->ClientNew(name, &fRefNum, shared_engine, shared_client, shared_ports);
}

263
void JackClientPipeThread::RemoveClient(int refnum)
sletz's avatar
sletz committed
264
265
266
{
    JackLog("JackWinNamedPipeServerChannel::RemoveClient ref = %d\n", refnum);
    fRefNum = -1;
267
    fPipe->Close();
sletz's avatar
sletz committed
268
269
}

270
void JackClientPipeThread::KillClient()
sletz's avatar
sletz committed
271
272
273
274
275
276
277
278
279
280
281
282
283
{
    JackLog("JackClientPipeThread::KillClient \n");
    JackLog("JackWinNamedPipeServerChannel::KillClient ref = %d\n", fRefNum);

    if (fRefNum == -1) {		// Correspond to an already removed client.
        JackLog("Kill a closed client\n");
	} else if (fRefNum == 0) {  // Correspond to a still not opened client.
        JackLog("Kill a not opened client\n");  
    } else {
        fServer->Notify(fRefNum, JackNotifyChannelInterface::kDeadClient, 0);
    }

    fRefNum = -1;
284
    fPipe->Close();
sletz's avatar
sletz committed
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
}

JackWinNamedPipeServerChannel::JackWinNamedPipeServerChannel()
{
    fThread = JackGlobals::MakeThread(this);
}

JackWinNamedPipeServerChannel::~JackWinNamedPipeServerChannel()
{
    delete fThread;
}

int JackWinNamedPipeServerChannel::Open(JackServer* server)
{
    JackLog("JackWinNamedPipeServerChannel::Open \n");

    fServer = server;

    // Needed for internal connection from JackWinNamedPipeServerNotifyChannel object
    if (fRequestListenPipe.Bind(jack_server_dir, 0) < 0) {
        jack_error("JackWinNamedPipeServerChannel::Open : cannot create result listen pipe");
        return false;
    }

    // Start listening
    if (fThread->Start() != 0) {
        jack_error("Cannot start Jack server listener\n");
        goto error;
    }

    return 0;

error:
    fRequestListenPipe.Close();
    return -1;
}

void JackWinNamedPipeServerChannel::Close()
{
    fThread->Kill();
    fRequestListenPipe.Close();
}

bool JackWinNamedPipeServerChannel::Init()
{
    JackLog("JackWinNamedPipeServerChannel::Init \n");
    JackWinNamedPipeClient* pipe;

	// Accept first client, that is the JackWinNamedPipeServerNotifyChannel object
    if ((pipe = fRequestListenPipe.AcceptClient()) == NULL) {
        jack_error("JackWinNamedPipeServerChannel::Init : cannot connect pipe");
        return false;
    } else {
        AddClient(pipe);
        return true;
    }
}

bool JackWinNamedPipeServerChannel::Execute()
{
    JackWinNamedPipeClient* pipe;

    if (fRequestListenPipe.Bind(jack_server_dir, 0) < 0) {
        jack_error("JackWinNamedPipeServerChannel::Open : cannot create result listen pipe");
        return false;
    }

    if ((pipe = fRequestListenPipe.AcceptClient()) == NULL) {
        jack_error("JackWinNamedPipeServerChannel::Open : cannot connect pipe");
        return false;
    }

    AddClient(pipe);
    return true;
}

void JackWinNamedPipeServerChannel::AddClient(JackWinNamedPipeClient* pipe)
{
363
    // Remove dead (= not running anymore) clients.
sletz's avatar
sletz committed
364
365
366
367
    std::list<JackClientPipeThread*>::iterator it = fClientList.begin();
    JackClientPipeThread* client;
    while (it != fClientList.end()) {
        client = *it;
368
369
370
371
372
        if (client->IsRunning()) {
			it++;         
        } else {  
            JackLog("Remove client from list\n");
            it = fClientList.erase(it);
sletz's avatar
sletz committed
373
374
375
376
377
            delete(client);
        }
    }

    client = new JackClientPipeThread(pipe);
378
379
    client->Open(fServer);
	// Here we are sure that the client is running (because it's thread is in "running" state).
sletz's avatar
sletz committed
380
381
382
383
384
385
    fClientList.push_back(client);
}

} // end of namespace