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 33 34 35 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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 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
|
/*
Copyright (C) 2004-2008 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 "JackWinNamedPipeClientChannel.h"
#include "JackRequest.h"
#include "JackClient.h"
#include "JackGlobals.h"
#include "JackError.h"
namespace Jack
{
JackWinNamedPipeClientChannel::JackWinNamedPipeClientChannel()
:JackGenericClientChannel(),fThread(this)
{
fRequest = new JackWinNamedPipeClient();
}
JackWinNamedPipeClientChannel::~JackWinNamedPipeClientChannel()
{
delete fRequest;
}
int JackWinNamedPipeClientChannel::Open(const char* server_name, const char* name, int uuid, char* name_res, JackClient* client, jack_options_t options, jack_status_t* status)
{
int result = 0;
jack_log("JackWinNamedPipeClientChannel::Open name = %s", name);
/*
16/08/07: was called before doing "fRequest->Connect" .... still necessary?
if (fNotificationListenPipe.Bind(jack_client_dir, name, 0) < 0) {
jack_error("Cannot bind pipe");
goto error;
}
*/
// Before any server/client call
fClient = client;
if (fRequest->Connect(jack_server_dir, server_name, 0) < 0) {
jack_error("Cannot connect to server pipe");
goto error;
}
// OK so server is there...
JackGlobals::fServerRunning = true;
// Check name in server
ClientCheck(name, uuid, name_res, JACK_PROTOCOL_VERSION, (int)options, (int*)status, &result, true);
if (result < 0) {
int status1 = *status;
if (status1 & JackVersionError) {
jack_error("JACK protocol mismatch %d", JACK_PROTOCOL_VERSION);
} else {
jack_error("Client name = %s conflits with another running client", name);
}
}
if (fNotificationListenPipe.Bind(jack_client_dir, name_res, 0) < 0) {
jack_error("Cannot bind pipe");
goto error;
}
return 0;
error:
fRequest->Close();
fNotificationListenPipe.Close();
return -1;
}
void JackWinNamedPipeClientChannel::Close()
{
fRequest->Close();
fNotificationListenPipe.Close();
// Here the thread will correctly stop when the pipe are closed
fThread.Stop();
}
int JackWinNamedPipeClientChannel::Start()
{
jack_log("JackWinNamedPipeClientChannel::Start");
/*
To be sure notification thread is started before ClientOpen is called.
*/
if (fThread.StartSync() != 0) {
jack_error("Cannot start Jack client listener");
return -1;
} else {
return 0;
}
}
void JackWinNamedPipeClientChannel::Stop()
{
jack_log("JackWinNamedPipeClientChannel::Stop");
fThread.Kill(); // Unsafe on WIN32... TODO : solve WIN32 thread Kill issue
}
bool JackWinNamedPipeClientChannel::Init()
{
jack_log("JackWinNamedPipeClientChannel::Init");
// Setup context
if (!jack_tls_set(JackGlobals::fNotificationThread, this)) {
jack_error("Failed to set thread notification key");
}
if (!fNotificationListenPipe.Accept()) {
jack_error("JackWinNamedPipeClientChannel: cannot establish notification pipe");
return false;
} else {
return true;
}
}
bool JackWinNamedPipeClientChannel::Execute()
{
JackClientNotification event;
JackResult res;
if (event.Read(&fNotificationListenPipe) < 0) {
jack_error("JackWinNamedPipeClientChannel read fail");
goto error;
}
res.fResult = fClient->ClientNotify(event.fRefNum, event.fName, event.fNotify, event.fSync, event.fMessage, event.fValue1, event.fValue2);
if (event.fSync) {
if (res.Write(&fNotificationListenPipe) < 0) {
jack_error("JackWinNamedPipeClientChannel write fail");
goto error;
}
}
return true;
error:
// Close the pipes, server won't be able to create them otherwise.
fNotificationListenPipe.Close();
fRequest->Close();
fClient->ShutDown(jack_status_t(JackFailure | JackServerError), JACK_SERVER_FAILURE);
return false;
}
} // end of namespace
|