summaryrefslogtreecommitdiff
path: root/macosx/JackMachPort.cpp
blob: e511bc27fbe73a805d27b6e865c59c7d3b70a8dd (plain)
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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
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 "JackMachPort.h"
#include "JackError.h"

namespace Jack
{

// Server side : port is published to be accessible from other processes (clients)

bool JackMachPort::AllocatePort(const char* name, int queue)
{
    mach_port_t task = mach_task_self();
    kern_return_t res;

    if ((res = task_get_bootstrap_port(task, &fBootPort)) != KERN_SUCCESS) {
        jack_error("AllocatePort: Can't find bootstrap mach port err = %s", mach_error_string(res));
        return false;
    }

    if ((res = mach_port_allocate(task, MACH_PORT_RIGHT_RECEIVE, &fServerPort)) != KERN_SUCCESS) {
        jack_error("AllocatePort: can't allocate mach port err = %s", mach_error_string(res));
        return false;
    }

    if ((res = mach_port_insert_right(task, fServerPort, fServerPort, MACH_MSG_TYPE_MAKE_SEND)) != KERN_SUCCESS) {
        jack_error("AllocatePort: error inserting mach rights err = %s", mach_error_string(res));
        return false;
    }

    if ((res = bootstrap_register(fBootPort, (char*)name, fServerPort)) != KERN_SUCCESS) {
        jack_error("Allocate: can't check in mach port name = %s err = %s", name, mach_error_string(res));
        return false;
    }

    mach_port_limits_t qlimits;
    mach_msg_type_number_t info_cnt = MACH_PORT_LIMITS_INFO_COUNT;
    if ((res = mach_port_get_attributes(task, fServerPort, MACH_PORT_LIMITS_INFO, (mach_port_info_t) & qlimits, &info_cnt)) != KERN_SUCCESS) {
        jack_error("Allocate: mach_port_get_attributes error err = %s", name, mach_error_string(res));
    }

    JackLog("AllocatePort: queue limit %ld\n", qlimits.mpl_qlimit);

    if (queue > 0 ) {
        qlimits.mpl_qlimit = queue;
        if ((res = mach_port_set_attributes(task, fServerPort, MACH_PORT_LIMITS_INFO, (mach_port_info_t) & qlimits, MACH_PORT_LIMITS_INFO_COUNT)) != KERN_SUCCESS) {
            jack_error("Allocate: mach_port_set_attributes error name = %s err = %s", name, mach_error_string(res));
        }
    }

    return true;
}

// Server side : port is published to be accessible from other processes (clients)

bool JackMachPort::AllocatePort(const char* name)
{
    return AllocatePort(name, -1);
}

// Client side : get the published port from server

bool JackMachPort::ConnectPort(const char* name)
{
    kern_return_t res;

    JackLog("JackMachPort::ConnectPort %s\n", name);

    if ((res = task_get_bootstrap_port(mach_task_self(), &fBootPort)) != KERN_SUCCESS) {
        jack_error("ConnectPort: can't find bootstrap port err = %s", mach_error_string(res));
        return false;
    }

    if ((res = bootstrap_look_up(fBootPort, (char*)name, &fServerPort)) != KERN_SUCCESS) {
        jack_error("ConnectPort: can't find mach server port name = %s err = %s", name, mach_error_string(res));
        return false;
    }

    return true;
}

bool JackMachPort::DisconnectPort()
{
    JackLog("JackMacRPC::DisconnectPort\n");
    kern_return_t res;
    mach_port_t task = mach_task_self();

    if ((res = mach_port_deallocate(task, fBootPort)) != KERN_SUCCESS) {
        jack_error("JackMacRPC::DisconnectPort mach_port_deallocate fBootPort err = %s", mach_error_string(res));
    }

    if ((res = mach_port_deallocate(task, fServerPort)) != KERN_SUCCESS) {
        jack_error("JackMacRPC::DisconnectPort mach_port_deallocate fServerPort err = %s", mach_error_string(res));
    }

    return true;
}

bool JackMachPort::DestroyPort()
{
    JackLog("JackMacRPC::DisconnectPort\n");
    kern_return_t res;
    mach_port_t task = mach_task_self();

    if ((res = mach_port_deallocate(task, fBootPort)) != KERN_SUCCESS) {
        jack_error("JackMacRPC::DisconnectPort mach_port_deallocate fBootPort err = %s", mach_error_string(res));
    }

    if ((res = mach_port_destroy(task, fServerPort)) != KERN_SUCCESS) {
        jack_error("JackMacRPC::DisconnectPort mach_port_destroy fServerPort err = %s", mach_error_string(res));
    }

    return true;
}

mach_port_t JackMachPort::GetPort()
{
    return fServerPort;
}

bool JackMachPortSet::AllocatePort(const char* name, int queue)
{
    kern_return_t res;
    mach_port_t task = mach_task_self();

    JackLog("JackMachPortSet::AllocatePort\n");

    if ((res = task_get_bootstrap_port(task, &fBootPort)) != KERN_SUCCESS) {
        jack_error("AllocatePort: Can't find bootstrap mach port err = %s", mach_error_string(res));
        return false;
    }

    if ((res = mach_port_allocate(task, MACH_PORT_RIGHT_RECEIVE, &fServerPort)) != KERN_SUCCESS) {
        jack_error("AllocatePort: can't allocate mach port err = %s", mach_error_string(res));
        return false;
    }

    if ((res = mach_port_insert_right(task, fServerPort, fServerPort, MACH_MSG_TYPE_MAKE_SEND)) != KERN_SUCCESS) {
        jack_error("AllocatePort: error inserting mach rights err = %s", mach_error_string(res));
        return false;
    }

    if ((res = mach_port_allocate(task, MACH_PORT_RIGHT_PORT_SET, &fPortSet)) != KERN_SUCCESS) {
        jack_error("AllocatePort: can't allocate mach port err = %s", mach_error_string(res));
        return false;
    }

    if ((res = mach_port_move_member(task, fServerPort, fPortSet)) != KERN_SUCCESS) {
        jack_error("AllocatePort: error in mach_port_move_member err = %s", mach_error_string(res));
        return false;
    }

    if ((res = bootstrap_register(fBootPort, (char*)name, fServerPort)) != KERN_SUCCESS) {
        jack_error("Allocate: can't check in mach port name = %s err = %s", name, mach_error_string(res));
        return false;
    }

    mach_port_limits_t qlimits;
    mach_msg_type_number_t info_cnt = MACH_PORT_LIMITS_INFO_COUNT;
    if ((res = mach_port_get_attributes(task, fServerPort, MACH_PORT_LIMITS_INFO, (mach_port_info_t) & qlimits, &info_cnt)) != KERN_SUCCESS) {
        jack_error("Allocate: mach_port_get_attributes error name = %s err = %s", name, mach_error_string(res));
    }

    JackLog("AllocatePort: queue limit = %ld\n", qlimits.mpl_qlimit);

    if (queue > 0 ) {
        qlimits.mpl_qlimit = queue;

        if ((res = mach_port_set_attributes(task, fServerPort, MACH_PORT_LIMITS_INFO, (mach_port_info_t) & qlimits, MACH_PORT_LIMITS_INFO_COUNT)) != KERN_SUCCESS) {
            jack_error("Allocate: mach_port_set_attributes error name = %s err = %s", name, mach_error_string(res));
        }
    }

    return true;
}

// Server side : port is published to be accessible from other processes (clients)

bool JackMachPortSet::AllocatePort(const char* name)
{
    return AllocatePort(name, -1);
}

bool JackMachPortSet::DisconnectPort()
{
    kern_return_t res;
    mach_port_t task = mach_task_self();

    JackLog("JackMachPortSet::DisconnectPort\n");

    if ((res = mach_port_deallocate(task, fBootPort)) != KERN_SUCCESS) {
        jack_error("JackMachPortSet::DisconnectPort mach_port_deallocate fBootPort err = %s", mach_error_string(res));
    }

    if ((res = mach_port_deallocate(task, fServerPort)) != KERN_SUCCESS) {
        jack_error("JackMachPortSet::DisconnectPort mach_port_deallocate fServerPort err = %s", mach_error_string(res));
    }

    return true;
}

bool JackMachPortSet::DestroyPort()
{
    kern_return_t res;
    mach_port_t task = mach_task_self();

    JackLog("JackMachPortSet::DisconnectPort\n");

    if ((res = mach_port_deallocate(task, fBootPort)) != KERN_SUCCESS) {
        jack_error("JackMachPortSet::DisconnectPort mach_port_deallocate err = %s", mach_error_string(res));
    }

    if ((res = mach_port_destroy(task, fServerPort)) != KERN_SUCCESS) {
        jack_error("JackMachPortSet::DisconnectPort mach_port_destroy fServerPort err = %s", mach_error_string(res));
    }

    return true;
}

mach_port_t JackMachPortSet::GetPortSet()
{
    return fPortSet;
}

mach_port_t JackMachPortSet::AddPort()
{
    kern_return_t res;
    mach_port_t task = mach_task_self();
    mach_port_t old_port, result = 0;

    JackLog("JackMachPortSet::AddPort\n");

    if ((res = mach_port_allocate(task, MACH_PORT_RIGHT_RECEIVE, &result)) != KERN_SUCCESS) {
        jack_error("AddPort: can't allocate mach port err = %s", mach_error_string(res));
        goto error;
    }

    if ((res = mach_port_request_notification(task, result, MACH_NOTIFY_NO_SENDERS,
               1, result, MACH_MSG_TYPE_MAKE_SEND_ONCE, &old_port)) != KERN_SUCCESS) {
        jack_error("AddPort: error in mach_port_request_notification err = %s", mach_error_string(res));
        goto error;
    }

    if ((res = mach_port_move_member(task, result, fPortSet)) != KERN_SUCCESS) {
        jack_error("AddPort: error in mach_port_move_member err = %s", mach_error_string(res));
        goto error;
    }

    return result;

error:
    if (result) {
        if ((res = mach_port_destroy(task, result)) != KERN_SUCCESS) {
            jack_error("JackMacRPC::DisconnectPort mach_port_destroy err = %s", mach_error_string(res));
        }
    }
    return 0;
}


} // end of namespace