JackMachServerChannel.cpp 4.55 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
/*
Copyright (C) 2004-2006 Grame  

This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 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 General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include "JackMachServerChannel.h"
#include "JackRPCEngineServer.c"
#include "JackError.h"
#include "JackServer.h"
#include "JackMachThread.h"
#include "JackEngine.h"
26
#include "JackNotification.h"
sletz's avatar
sletz committed
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

using namespace std;

namespace Jack
{

map<mach_port_t, JackMachServerChannel*> JackMachServerChannel::fPortTable;

JackMachServerChannel::JackMachServerChannel()
{
    fThread = new JackMachThread(this);
}

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

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

    if (!fServerPort.AllocatePort(jack_server_entry, 16)) { // 16 is the max possible value
        jack_error("Cannot check in Jack server");
        return -1;
    }

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

    fServer = server;
    fPortTable[fServerPort.GetPort()] = this;
    return 0;
}

void JackMachServerChannel::Close()
{
    JackLog("JackMachServerChannel::Close\n");
    fThread->Kill();
    fServerPort.DestroyPort();
}

JackEngine* JackMachServerChannel::GetEngine()
{
    return fServer->GetEngine();
}

JackServer* JackMachServerChannel::GetServer()
{
    return fServer;
}

81
82
83
84
85
86
void JackMachServerChannel::ClientCheck(char* name, char* name_res, int options, int* status, int* result)
{
	*result = GetEngine()->ClientCheck(name, name_res, options, status);
}

void JackMachServerChannel::ClientOpen(char* name, mach_port_t* private_port, int* shared_engine, int* shared_client, int* shared_graph, int* result)
sletz's avatar
sletz committed
87
88
{
    int refnum = -1;
sletz's avatar
sletz committed
89
	*result = GetEngine()->ClientExternalOpen(name, &refnum, shared_engine, shared_client, shared_graph);
sletz's avatar
sletz committed
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105

    if (*result == 0) {
        mach_port_t port = fServerPort.AddPort();
        if (port != 0) {
            fClientTable[port] = refnum;
            fPortTable[port] = this;
            *private_port = port;
        } else {
            jack_error("Cannot create private client mach port");
            *result = -1;
        }
    } else {
        jack_error("Cannot create new client");
    }
}

106
void JackMachServerChannel::ClientClose(mach_port_t private_port, int refnum)
sletz's avatar
sletz committed
107
{
sletz's avatar
sletz committed
108
	GetEngine()->ClientExternalClose(refnum);
sletz's avatar
sletz committed
109
110
111
112
113
114
115
116
117
    fClientTable.erase(private_port);

    // Hum, hum....
    kern_return_t res;
    if ((res = mach_port_destroy(mach_task_self(), private_port)) != KERN_SUCCESS) {
        jack_error("server_rpc_jack_client_close mach_port_destroy %s", mach_error_string(res));
    }
}

118
void JackMachServerChannel::ClientKill(mach_port_t private_port)
sletz's avatar
sletz committed
119
{
120
    JackLog("JackMachServerChannel::ClientKill\n");
sletz's avatar
sletz committed
121
122
    int refnum = fClientTable[private_port];
    assert(refnum > 0);
123
    fServer->Notify(refnum, kDeadClient, 0);
sletz's avatar
sletz committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
    fClientTable.erase(private_port);

    // Hum, hum....
    kern_return_t res;
    if ((res = mach_port_destroy(mach_task_self(), private_port)) != KERN_SUCCESS) {
        jack_error("server_rpc_jack_client_close mach_port_destroy %s", mach_error_string(res));
    }
}

boolean_t JackMachServerChannel::MessageHandler(mach_msg_header_t* Request, mach_msg_header_t* Reply)
{
    if (Request->msgh_id == MACH_NOTIFY_NO_SENDERS) {
        JackLog("MACH_NOTIFY_NO_SENDERS %ld\n", Request->msgh_local_port);
        JackMachServerChannel* channel = JackMachServerChannel::fPortTable[Request->msgh_local_port];
        assert(channel);
139
        channel->ClientKill(Request->msgh_local_port);
sletz's avatar
sletz committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
    } else {
        JackRPCEngine_server(Request, Reply);
    }
    return true;
}

bool JackMachServerChannel::Execute()
{
    kern_return_t res;
    if ((res = mach_msg_server(MessageHandler, 1024, fServerPort.GetPortSet(), 0)) != KERN_SUCCESS) {
        JackLog("JackMachServerChannel::Execute: err = %s\n", mach_error_string(res));
    }
    //return (res == KERN_SUCCESS);  mach_msg_server can fail if the client reply port is not valid anymore (crashed client)
    return true;
}

} // end of namespace