1 /*
2 * Copyright 1993 Network Computing Devices, Inc.
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name Network Computing Devices, Inc. not be
9 * used in advertising or publicity pertaining to distribution of this
10 * software without specific, written prior permission.
11 *
12 * THIS SOFTWARE IS PROVIDED 'AS-IS'. NETWORK COMPUTING DEVICES, INC.,
13 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT
14 * LIMITATION ALL IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
15 * PARTICULAR PURPOSE, OR NONINFRINGEMENT. IN NO EVENT SHALL NETWORK
16 * COMPUTING DEVICES, INC., BE LIABLE FOR ANY DAMAGES WHATSOEVER, INCLUDING
17 * SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES, INCLUDING LOSS OF USE, DATA,
18 * OR PROFITS, EVEN IF ADVISED OF THE POSSIBILITY THEREOF, AND REGARDLESS OF
19 * WHETHER IN AN ACTION IN CONTRACT, TORT OR NEGLIGENCE, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 *
22 * $NCDId: @(#)AlibAsync.c,v 1.4 1993/01/29 20:47:18 lemke Exp $
23 */
24
25 /* Portions derived from */
26 /* $XConsortium: XlibAsync.c,v 1.3 92/01/21 17:06:50 rws Exp $ */
27 /*
28
29 Copyright 1992 by the Massachusetts Institute of Technology
30
31 Permission to use, copy, modify, distribute, and sell this software and its
32 documentation for any purpose is hereby granted without fee, provided that
33 the above copyright notice appear in all copies and that both that
34 copyright notice and this permission notice appear in supporting
35 documentation, and that the name of M.I.T. not be used in advertising or
36 publicity pertaining to distribution of the software without specific,
37 written prior permission. M.I.T. makes no representations about the
38 suitability of this software for any purpose. It is provided "as is"
39 without express or implied warranty.
40
41 */
42
43 #include <audio/Alibint.h>
44 #include <audio/Aos.h>
45
46 /*ARGSUSED*/
47 AuBool
_AuAsyncErrorHandler(AuServer * aud,auReply * rep,char * buf,int len,AuPointer data)48 _AuAsyncErrorHandler(AuServer *aud, auReply *rep,
49 char *buf, int len, AuPointer data)
50 {
51 _AuAsyncErrorState *state;
52
53 state = (_AuAsyncErrorState *)data;
54 if (rep->generic.type == Au_Error &&
55 (!state->error_code ||
56 rep->error.errorCode == state->error_code) &&
57 (!state->major_opcode ||
58 rep->error.majorCode == state->major_opcode) &&
59 (!state->minor_opcode ||
60 rep->error.minorCode == state->minor_opcode) &&
61 (!state->min_sequence_number ||
62 (state->min_sequence_number <= aud->last_request_read)) &&
63 (!state->max_sequence_number ||
64 (state->max_sequence_number >= aud->last_request_read))) {
65 state->last_error_received = rep->error.errorCode;
66 state->error_count++;
67 return AuTrue;
68 }
69 return AuFalse;
70 }
71
72 void
_AuDoDeqAsyncHandler(AuServer * aud,_AuAsyncHandler * handler)73 _AuDoDeqAsyncHandler(AuServer *aud, _AuAsyncHandler *handler)
74 {
75 _AuAsyncHandler **prev;
76 _AuAsyncHandler *async;
77
78 for (prev = &aud->async_handlers;
79 (async = *prev) && (async != handler);
80 prev = &async->next)
81 /* SUPPRESS 530 */
82 ;
83 if (async)
84 *prev = async->next;
85 }
86
87 char *
_AuGetAsyncReply(AuServer * aud,char * replbuf,auReply * rep,char * buf,int len,int extra,AuBool discard)88 _AuGetAsyncReply(AuServer *aud, char *replbuf,
89 auReply *rep, char *buf, int len, int extra,
90 AuBool discard)
91 {
92 if (extra == 0) {
93 if (discard && (rep->generic.length << 2) > len)
94 _AuEatData (aud, (rep->generic.length << 2) - len);
95 return (char *)rep;
96 }
97
98 if (extra <= rep->generic.length) {
99 int size = SIZEOF(auReply) + (extra << 2);
100 if (size > len) {
101 bcopy(buf, replbuf, len);
102 _AuRead(aud, replbuf + len, size - len);
103 buf = replbuf;
104 len = size;
105 #ifdef MUSTCOPY
106 } else {
107 bcopy(buf, replbuf, size);
108 buf = replbuf;
109 #endif
110 }
111
112 if (discard && rep->generic.length > extra &&
113 (rep->generic.length << 2) > len)
114 _AuEatData (aud, (rep->generic.length << 2) - len);
115
116 return buf;
117 }
118 /*
119 *if we get here, then extra > rep->generic.length--meaning we
120 * read a reply that's shorter than we expected. This is an
121 * error, but we still need to figure out how to handle it...
122 */
123 if ((rep->generic.length << 2) > len)
124 _AuEatData (aud, (rep->generic.length << 2) - len);
125 _AuIOError (aud);
126 return (char *)rep;
127 }
128