summaryrefslogtreecommitdiff
path: root/config/os/macosx/ipc.h
blob: 8c0e54666ade15ed9b1798ecf5800898cb2070bd (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
/*
    Copyright © Grame 2003

    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.
    
    Grame Research Laboratory, 9, rue du Garet 69001 Lyon - France
    grame@rd.grame.fr
*/

#ifndef __ipc__
#define __ipc__

#include <mach/mach_error.h>
#include <servers/bootstrap.h>
#include "internal.h"
#include "engine.h"
#include "libjack/local.h"		/* JOQ: fix me */

/*
    RPC without time out can put the jack server in a blocked state
    (waiting for the client answer) when a client is killed.  The
    mach_msg function does not return any error in this case. Using
    time out solve the problem but does not seems really satisfactory.
*/

#define WAIT 2500 /* in millisecond */

static inline int 
jack_client_resume(jack_client_internal_t *client)
{
        mach_msg_header_t *head =  &client->message.header;
        int err;
        
        if (!client->running) {
            err = mach_msg (head, MACH_RCV_MSG, 0, sizeof(client->message),
			    client->serverport, 0, MACH_PORT_NULL);
            if (err) {
                jack_error("jack_client_resume: priming receive error: %s\n", 
			   mach_error_string(err));
                return -1;
            }
            client->running = TRUE;
        }else {
            /* remote port is already the send-once he sent us */
            head->msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_MOVE_SEND_ONCE, 0);
            head->msgh_local_port = MACH_PORT_NULL;
            head->msgh_size = sizeof(mach_msg_header_t);
    
            err = mach_msg(head, (MACH_SEND_MSG|MACH_RCV_MSG|
				  MACH_SEND_TIMEOUT|MACH_RCV_TIMEOUT), 
			   sizeof(*head), sizeof(client->message),
			   client->serverport, WAIT, MACH_PORT_NULL);
                
            if (err) {
            
                    /*
                    switch(err) {
                        case MACH_SEND_TIMED_OUT:
                                 jack_error("MACH_SEND_TIMED_OUT %s\n",
				            client->control->name);
                                 break;
                                 
                           case MACH_RCV_TIMED_OUT:
                                 jack_error("MACH_RCV_TIMED_OUT %s\n",
				            client->control->name);
                                 break;
                     
                         case MACH_SEND_INVALID_DEST:
                                 jack_error("MACH_SEND_INVALID_DEST %s\n",
				            client->control->name);
                                 break;
                    }
                    */
                    
                    jack_error("jack_client_resume: send error for %s\n",
			       mach_error_string(err));
                    return err;
            }
        }
        
        return 0;
}

static inline int 
jack_client_suspend(jack_client_t * client)
{
        int err = 0;
        mach_msg_header_t * head = &client->message.header;
     
        head->msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND,
					 MACH_MSG_TYPE_MAKE_SEND_ONCE);
        head->msgh_remote_port = client->serverport;
	head->msgh_local_port = client->replyport;
	head->msgh_size = sizeof(mach_msg_header_t);
     
        err = mach_msg(head, MACH_SEND_MSG|MACH_RCV_MSG|MACH_SEND_TIMEOUT, 
		       sizeof(mach_msg_header_t), sizeof(client->message),
		       client->replyport, WAIT, MACH_PORT_NULL);
            
        if (err) {
            jack_error("jack_client_suspend: RPC error: %s\n",
		       mach_error_string(err));
            return -1;
        }
        
        return 0;
}

static inline void 
allocate_mach_serverport(jack_engine_t * engine, jack_client_internal_t *client)
{
        char buf[256];
        snprintf(buf, 256, "JackMachPort_%d", engine->portnum); 
        
        if (mach_port_allocate(engine->servertask, MACH_PORT_RIGHT_RECEIVE,
			       &client->serverport)){
            jack_error("allocate_mach_serverport: can't allocate mach port");
        }
        
        if (mach_port_insert_right(engine->servertask, client->serverport,
				   client->serverport, MACH_MSG_TYPE_MAKE_SEND)){
            jack_error("allocate_mach_serverport: error inserting mach rights");
        }
        
	if (bootstrap_register(engine->bp, buf, client->serverport)){
            jack_error("allocate_mach_serverport: can't check in mach port");
        }
        
        client->portnum = engine->portnum;
        engine->portnum++;
}

static inline int 
allocate_mach_clientport(jack_client_t * client, int portnum)
{
        char buf[256];
        snprintf(buf, 256, "JackMachPort_%d", portnum); 
        
        if (bootstrap_look_up(client->bp, buf, &client->serverport)){
            jack_error ("allocate_mach_clientport: can't find mach server port");
            return -1;
  	}
        
        if (mach_port_allocate(client->clienttask, MACH_PORT_RIGHT_RECEIVE,
			       &client->replyport)){
            jack_error("allocate_mach_clientport: can't allocate mach port");
            return -1;
        }
        
        return 0;
}

#endif /* __ipc__ */