1 /* This module supports sending and receiving data objects over a
4 Copyright 1993 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
22 static char udr_c
[]="@(#)udr.c 2.8 Daniel Mann";
23 static char udr_c_AMD
[]="@(#)udr.c 2.3, AMD";
25 * All data is serialised into a character stream,
26 * and de-serialised back into the approproiate objects.
27 ********************************************************************** HISTORY
30 #include <sys/types.h>
32 /* This used to say sys/fcntl.h, but the only systems I know of that
33 require that are old (pre-4.3, at least) BSD systems, which we
34 probably don't need to worry about. */
37 #include <sys/socket.h>
42 extern char* malloc();
44 /* local type decs. and macro defs. not in a .h file ************* MACRO/TYPE
47 /* global dec/defs. which are not in a .h file ************* EXPORT DEC/DEFS
49 int udr_errno
; /* error occurs during UDR service */
51 /* local dec/defs. which are not in a .h file *************** LOCAL DEC/DEFS
54 /****************************************************************** UDR_CREATE
55 * Build UDR structure for character stream processing.
57 int udr_create(udrs
, sd
, size
)
63 if(!udrs
->buff
) udrs
->buff
= malloc(size
);
64 udrs
->getbytes
= udrs
->buff
; /* set the buffer to the start */
65 udrs
->putbytes
= udrs
->buff
;
66 udrs
->putend
= udrs
->buff
;
67 udrs
->udr_op
= -1; /* don't know the direction */
68 udrs
->previous_op
= -1; /* don't know the direction */
73 /******************************************************************** UDR_FREE
74 * Free USR structure and close socket.
84 /****************************************************************** UDR_SIGNAL
85 * Send a signal to the process at the other end of the socket,
86 * indicating that it should expect to recieve a new message shortly.
91 if(send(udrs
->sd
, "I", 1, MSG_OOB
) == -1)
92 { perror("ERROR, udr_signal(), send(...MSG_OOB)");
93 udr_errno
= UDIErrorIPCInternal
;
94 return -1; /* return error code */
99 /***************************************************************** UDR_SENDNOW
100 * used to flush the current character stream buffer to
101 * the associated socket. */
102 int udr_sendnow(udrs
)
105 int size
= (UDIUInt32
)(udrs
->putend
) - (UDIUInt32
)(udrs
->buff
);
106 if(udrs
->previous_op
== 0)
107 { udr_errno
= UDIErrorIPCInternal
;
110 udrs
->putbytes
= udrs
->buff
;
111 udrs
->putend
= udrs
->buff
;
112 if (write(udrs
->sd
, udrs
->buff
, size
) == -1)
113 { perror("ERROR, udr_sendnow(), write() call: ");
114 udr_errno
= UDIErrorIPCInternal
;
115 return -1; /* return error code */
120 /******************************************************************** UDR_WORK
121 * Function to send or recieve data from the buffers supporting
122 * socket communication. The buffer contains serialised objects
123 * sent/recieved over a socket connection.
125 int udr_work(udrs
, object_p
, size
)
132 if(udrs
->udr_op
!= udrs
->previous_op
)
133 { if(udrs
->previous_op
== 0)
134 { udr_errno
= UDIErrorIPCInternal
;
137 udrs
->previous_op
= udrs
->udr_op
;
138 udrs
->putbytes
= udrs
->buff
;
139 udrs
->getbytes
= udrs
->buff
;
142 if(udrs
->udr_op
== UDR_ENCODE
)
143 { /* write data into character stream buffer */
144 if( (UDIUInt32
)(udrs
->putbytes
) + size
>
145 (UDIUInt32
)(udrs
->buff
) + (UDIUInt32
)(udrs
->bufsize
) )
146 { udr_errno
= UDIErrorIPCInternal
;
149 bcopy((char*)object_p
, udrs
->putbytes
, size
);
150 udrs
->putbytes
+= size
;
151 if(udrs
->putbytes
> udrs
->putend
) udrs
->putend
= udrs
->putbytes
;
153 else if(udrs
->udr_op
== UDR_DECODE
)
155 if( (UDIUInt32
)(udrs
->putbytes
)-(UDIUInt32
)(udrs
->getbytes
) < size
)
156 { /* need more data in character stream buffer */
157 remain
= (UDIUInt32
)(udrs
->bufsize
) -
158 ( (UDIUInt32
)(udrs
->putbytes
)-(UDIUInt32
)(udrs
->buff
) );
159 if( ((UDIUInt32
)(udrs
->bufsize
) + (UDIUInt32
)(udrs
->buff
)
160 - (UDIUInt32
)(udrs
->getbytes
)) < size
)
161 { udr_errno
= UDIErrorIPCInternal
;
164 cnt
= read(udrs
->sd
, (char*)udrs
->putbytes
, remain
);
165 if(cnt
== -1) perror("ERROR udr_work(), read() failure: ");
166 udrs
->putbytes
+= cnt
;
167 if( (UDIUInt32
)(udrs
->putbytes
)-(UDIUInt32
)(udrs
->getbytes
) < size
)
168 { udr_errno
= UDIErrorIPCInternal
;
169 return -1; /* return error code */
171 } /* read data from character stream buffer */
172 bcopy(udrs
->getbytes
, (char*)object_p
, size
);
173 udrs
->getbytes
+= size
;
176 { udr_errno
= UDIErrorIPCInternal
;
182 /************************************************************* UDR_UDIResource
184 int udr_UDIResource(udrs
, object_p
)
186 UDIResource
* object_p
;
190 retval
= udr_CPUSpace(udrs
, &object_p
->Space
);
191 retval
= retval
| udr_CPUOffset(udrs
, &object_p
->Offset
);
195 /**************************************************************** UDR_UDIRange
197 int udr_UDIRange(udrs
, object_p
)
203 retval
= udr_CPUOffset(udrs
, &object_p
->Low
);
204 retval
= retval
| udr_CPUOffset(udrs
, &object_p
->High
);
208 /********************************************************** UDR_UDIMemoryRange
210 int udr_UDIMemoryRange(udrs
, object_p
)
212 UDIMemoryRange
* object_p
;
216 retval
= udr_CPUSpace(udrs
, &object_p
->Space
);
217 retval
= retval
| udr_CPUOffset(udrs
, &object_p
->Offset
);
218 retval
= retval
| udr_CPUSizeT(udrs
, &object_p
->Size
);
222 /****************************************************************** UDR_string
224 int udr_string(udrs
, sp
)
230 if(udrs
->udr_op
== UDR_ENCODE
)
233 { len
= strlen(sp
) + 1;
234 retval
= udr_UDIInt32(udrs
, &len
);
235 retval
= retval
| udr_work(udrs
, sp
, len
);
237 else /* deal with NULL pointer */
239 retval
= udr_UDIInt32(udrs
, &len
);
242 else if(udrs
->udr_op
== UDR_DECODE
)
244 retval
= udr_UDIInt32(udrs
, &len
);
246 retval
= retval
| udr_work(udrs
, sp
, len
);
247 else *sp
= '\0'; /* terminate string */
250 { udr_errno
= UDIErrorIPCInternal
;
256 /******************************************************************* UDR_BYTES
258 int udr_bytes(udrs
, ptr
, len
)
263 return udr_work(udrs
, ptr
, len
);
266 /********************************************************************* UDR_INT
268 int udr_int(udrs
, int_p
)
273 UDIInt32 udr_obj
; /* object of know size */
275 if(udrs
->udr_op
== UDR_ENCODE
)
277 udr_obj
= *int_p
; /* copy into know object size */
278 return udr_UDIInt32(udrs
, &udr_obj
);
280 else if(udrs
->udr_op
== UDR_DECODE
)
282 ret_val
= udr_UDIInt32(udrs
, &udr_obj
); /* get object of known size */
287 { udr_errno
= UDIErrorIPCInternal
;
292 /****************************************************************** UDR_INLINE
294 char* udr_inline(udrs
, size
)
298 if(udrs
->udr_op
!= udrs
->previous_op
)
299 { if(udrs
->previous_op
== 0)
300 { udr_errno
= UDIErrorIPCInternal
;
303 udrs
->previous_op
= udrs
->udr_op
;
304 udrs
->putbytes
= udrs
->buff
;
305 udrs
->getbytes
= udrs
->buff
;
307 if(udrs
->udr_op
== UDR_ENCODE
)
309 if(udrs
->putbytes
+ size
> udrs
->bufsize
+ udrs
->buff
)
311 udrs
->putbytes
+= size
;
312 return udrs
->putbytes
- size
;
314 else if(udrs
->udr_op
== UDR_DECODE
)
316 if(udrs
->getbytes
+ size
> udrs
->bufsize
+ udrs
->buff
)
318 udrs
->getbytes
+= size
;
319 return udrs
->getbytes
- size
;
322 { udr_errno
= UDIErrorIPCInternal
;
327 /****************************************************************** UDR_GETPOS
329 char* udr_getpos(udrs
)
332 if(udrs
->udr_op
== UDR_ENCODE
)
334 return udrs
->putbytes
;
336 else if(udrs
->udr_op
== UDR_DECODE
)
338 return udrs
->getbytes
;
341 { udr_errno
= UDIErrorIPCInternal
;
346 /****************************************************************** UDR_SETPOS
348 int udr_setpos(udrs
, pos
)
352 if( ((UDIUInt32
)pos
> (UDIUInt32
)(udrs
->buff
) + (UDIUInt32
)(udrs
->bufsize
))
353 || ((UDIUInt32
)pos
< (UDIUInt32
)(udrs
->buff
) ) )
354 { udr_errno
= UDIErrorIPCInternal
;
357 if(udrs
->udr_op
== UDR_ENCODE
)
359 udrs
->putbytes
= pos
;
362 else if(udrs
->udr_op
== UDR_DECODE
)
364 udrs
->getbytes
= pos
;
368 { udr_errno
= UDIErrorIPCInternal
;
373 /***************************************************************** UDR_READNOW
374 * Try and ensure "size" bytes are available in the
375 * receive buffer character stream.
377 int udr_readnow(udrs
, size
)
383 if(udrs
->udr_op
== UDR_ENCODE
)
385 udr_errno
= UDIErrorIPCInternal
;
388 else if(udrs
->udr_op
== UDR_DECODE
)
390 if( (UDIUInt32
)(udrs
->putbytes
)-(UDIUInt32
)(udrs
->getbytes
) < size
)
391 { /* need more data in character stream buffer */
392 remain
= (UDIUInt32
)(udrs
->bufsize
) -
393 ( (UDIUInt32
)(udrs
->putbytes
)-(UDIUInt32
)(udrs
->buff
) );
394 cnt
= read(udrs
->sd
, (char*)udrs
->putbytes
, remain
);
395 if(cnt
== -1) perror("ERROR udr_work(), read() failure: ");
396 udrs
->putbytes
+= cnt
;
397 if( (UDIUInt32
)(udrs
->putbytes
)-(UDIUInt32
)(udrs
->getbytes
) < size
)
398 { fprintf(stderr
,"ERROR, udr_readnow() too few bytes in stream\n");
399 return -1; /* return error code */
404 { udr_errno
= UDIErrorIPCInternal
;
410 /******************************************************************* UDR_ALIGN
412 int udr_align(udrs
, size
)
419 align
= udr_getpos(udrs
);
420 offset
= size
- ((int)align
& (size
-1));
421 offset
= offset
& (size
-1);
422 if(offset
) udr_setpos(udrs
, align
+ offset
);
This page took 0.038264 seconds and 4 git commands to generate.