summaryrefslogtreecommitdiff
path: root/innobase/com/com0com.c
blob: 94585d9f269f3029064586d58e125a98d2630ccd (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
280
281
282
283
284
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
/******************************************************
The communication primitives

(c) 1995 Innobase Oy

Created 9/23/1995 Heikki Tuuri
*******************************************************/

#include "com0com.h"
#ifdef UNIV_NONINL
#include "com0com.ic"
#endif

#include "mem0mem.h"
#include "com0shm.h"

/*
	IMPLEMENTATION OF COMMUNICATION PRIMITIVES
	==========================================

The primitives provide a uniform function interface for
use in communication. The primitives have been modeled
after the Windows Sockets interface. Below this uniform
API, the precise methods of communication, for example,
shared memory or Berkeley sockets, can be implemented
as subroutines.
*/

struct com_endpoint_struct{
	ulint	type;		/* endpoint type */
	void*	par;		/* type-specific data structures */
	ibool	bound;		/* TRUE if the endpoint has been
				bound to an address */
};

/*************************************************************************
Accessor functions for an endpoint */
UNIV_INLINE
ulint
com_endpoint_get_type(
/*==================*/
	com_endpoint_t*	ep)
{
	ut_ad(ep);
	return(ep->type);
}

UNIV_INLINE
void
com_endpoint_set_type(
/*==================*/
	com_endpoint_t*	ep,
	ulint		type)
{
	ut_ad(ep);
	ut_ad(type == COM_SHM);

	ep->type = type;
}

UNIV_INLINE
void*
com_endpoint_get_par(
/*=================*/
	com_endpoint_t*	ep)
{
	ut_ad(ep);
	return(ep->par);
}

UNIV_INLINE
void
com_endpoint_set_par(
/*=================*/
	com_endpoint_t*	ep,
	void*		par)
{
	ut_ad(ep);
	ut_ad(par);

	ep->par = par;
}

UNIV_INLINE
ibool
com_endpoint_get_bound(
/*===================*/
	com_endpoint_t*	ep)
{
	ut_ad(ep);
	return(ep->bound);
}

UNIV_INLINE
void
com_endpoint_set_bound(
/*===================*/
	com_endpoint_t*	ep,
	ibool		bound)
{
	ut_ad(ep);

	ep->bound = bound;
}


/*************************************************************************
Creates a communications endpoint. */

com_endpoint_t*
com_endpoint_create(
/*================*/
			/* out, own: communications endpoint, NULL if
			did not succeed */
	ulint	type)	/* in: communication type of endpoint:
			only COM_SHM supported */
{
	com_endpoint_t*	ep;
	void*		par;

	ep = mem_alloc(sizeof(com_endpoint_t));
	
	com_endpoint_set_type(ep, type);
	com_endpoint_set_bound(ep, FALSE);
	
	if (type == COM_SHM) {
		par = com_shm_endpoint_create();
		com_endpoint_set_par(ep, par);
	} else {
		par = NULL;
		ut_error;
	}

	if (par != NULL) {
		return(ep);
	} else {
		mem_free(ep);
		return(NULL);
	}
}

/*************************************************************************
Frees a communications endpoint. */

ulint
com_endpoint_free(
/*==============*/
				/* out: O if succeed, else error number */
	com_endpoint_t*	ep)	/* in, own: communications endpoint */
{
	ulint	type;
	ulint	ret;
	void*	par;

	type = com_endpoint_get_type(ep);
	par  = com_endpoint_get_par(ep);

	if (type == COM_SHM) {
		ret = com_shm_endpoint_free((com_shm_endpoint_t*)par);
	} else {
		ret = 0;
		ut_error;
	}

	if (ret) {
		return(ret);
	} else {
		mem_free(ep);
		return(0);
	}
}

/*************************************************************************
Sets an option, like the maximum datagram size for an endpoint.
The options may vary depending on the endpoint type. */

ulint
com_endpoint_set_option(
/*====================*/
				/* out: 0 if succeed, else error number */
	com_endpoint_t*	ep,	/* in: endpoint */
	ulint		optno,	/* in: option number, only
				COM_OPT_MAX_DGRAM_SIZE currently supported */
	byte*		optval,	/* in: pointer to a buffer containing the
				option value to set */
	ulint		optlen)	/* in: option value buffer length */
{
	ulint	type;
	ulint	ret;
	void*	par;

	type = com_endpoint_get_type(ep);
	par  = com_endpoint_get_par(ep);

	if (type == COM_SHM) {
		ret = com_shm_endpoint_set_option((com_shm_endpoint_t*)par,
						  optno, optval, optlen);
	} else {
		ret = 0;
		ut_error;
	}

	return(ret);
}
	
/*************************************************************************
Binds a communications endpoint to the specified address. */

ulint
com_bind(
/*=====*/
				/* out: 0 if succeed, else error number */
	com_endpoint_t*	ep,	/* in: communications endpoint */
	char*		name,	/* in: address name */
	ulint		len)	/* in: name length */
{
	ulint	type;
	ulint	ret;
	void*	par;

	ut_ad(len <= COM_MAX_ADDR_LEN);

	if (com_endpoint_get_bound(ep)) {
		return(COM_ERR_ALREADY_BOUND);
	}

	type = com_endpoint_get_type(ep);
	par  = com_endpoint_get_par(ep);

	if (type == COM_SHM) {
		ret = com_shm_bind((com_shm_endpoint_t*)par, name, len);
	} else {
		ret = 0;
		ut_error;
	}

	if (ret == 0) {
		com_endpoint_set_bound(ep, TRUE);
	}		
	
	return(ret);
}

/*************************************************************************
Waits for a datagram to arrive at an endpoint. */

ulint
com_recvfrom(
/*=========*/
				/* out: 0 if succeed, else error number */
	com_endpoint_t*	ep,	/* in: communications endpoint */
	byte*		buf,	/* out: datagram buffer; the buffer is
				supplied by the caller */
	ulint		buf_len,/* in: datagram buffer length */
	ulint*		len,	/* out: datagram length */
	char*		from,	/* out: address name buffer; the buffer is
				supplied by the caller */
	ulint		from_len,/* in: address name buffer length */
	ulint*		addr_len)/* out: address name length */
{
	ulint	type;
	ulint	ret;
	void*	par;

	if (!com_endpoint_get_bound(ep)) {

		return(COM_ERR_NOT_BOUND);
	}

	type = com_endpoint_get_type(ep);
	par  = com_endpoint_get_par(ep);

	if (type == COM_SHM) {
		ret = com_shm_recvfrom((com_shm_endpoint_t*)par,
					buf, buf_len, len, from, from_len,
					addr_len);
	} else {
		ret = 0;

		ut_error;
	}

	return(ret);
}

/*************************************************************************
Sends a datagram to the specified destination. */

ulint
com_sendto(
/*=======*/
				/* out: 0 if succeed, else error number */
	com_endpoint_t*	ep,	/* in: communications endpoint */
	byte*		buf,	/* in: datagram buffer */
	ulint		len,	/* in: datagram length */
	char*		to,	/* in: address name buffer */
	ulint		tolen)	/* in: address name length */
{
	ulint	type;
	ulint	ret;
	void*	par;

	if (!com_endpoint_get_bound(ep)) {
		return(COM_ERR_NOT_BOUND);
	}

	type = com_endpoint_get_type(ep);
	par  = com_endpoint_get_par(ep);

	if (type == COM_SHM) {
		ret = com_shm_sendto((com_shm_endpoint_t*)par, buf, len,
								to, tolen);
	} else {
		ret = 0;
		ut_error;
	}

	return(ret);
}

/*************************************************************************
Gets the maximum datagram size for an endpoint. */

ulint
com_endpoint_get_max_size(
/*======================*/
				/* out: maximum size */
	com_endpoint_t*	ep)	/* in: endpoint */
{
	ulint	type;
	ulint	ret;
	void*	par;

	type = com_endpoint_get_type(ep);
	par  = com_endpoint_get_par(ep);

	if (type == COM_SHM) {
		ret = com_shm_endpoint_get_size((com_shm_endpoint_t*)par);
	} else {
		ret = 0;
		ut_error;
	}

	return(ret);
}