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
|
/*
* Ivy League
*
* Buffered streams
*
* Copyright 1990-2000
* Laboratoire de Recherche en Informatique (LRI)
* Centre d'Etudes de la Navigation Aerienne (CENA)
*
* by Stephane Chatty
* portions of code by Michel Beaudouin-Lafon
*
* $Id$
*
*/
#include "BufStream.h"
#include "error.h"
/*?
These constructors are similar to those of class \typ{IvlSocket}.
?*/
IvlBufStream :: IvlBufStream (IvlAddress* bindTo, IvlAddress* connectTo)
: IvlStream (bindTo, connectTo),
InBuffer (),
OutBuffer (),
OutSize (128),
Sync (false)
{
}
// *** this copy constructor might be automatically generated
/*?nodoc?*/
IvlBufStream :: IvlBufStream (const IvlBufStream& ms)
: IvlStream (ms),
InBuffer (),
OutBuffer (),
OutSize (ms.OutSize),
Sync (ms.Sync)
{
}
/*?nodoc?*/
IvlBufStream :: ~IvlBufStream ()
{
Flush ();
InBuffer.Clear ();
OutBuffer.Clear ();
}
/*?nextdoc?*/
void
IvlBufStream :: InputBuffer (int min, int grow, int max)
{
InBuffer.SetSizes (min, grow, max);
}
/*?
Set the input and output buffer sizes.
Default sizes are used if these functions are not called.
?*/
void
IvlBufStream :: OutputBuffer (int min, int grow, int max)
{
OutBuffer.SetSizes (min, grow, max);
OutSize = max;
}
/*?
Flush the output buffer.
This function is called automatically when the buffer exceeds its flush size,
or after each message when this stream is in synchronous mode.
It is also called from \fun{Ask} to wait for the answer.
?*/
void
IvlBufStream :: Flush ()
{
if (OutBuffer.BufLength () == 0)
return;
int n = Write (OutBuffer);
OutBuffer.Flush (n);
}
/*?nodoc?*/
void
IvlBufStream :: HandleWrite ()
{
Flush ();
}
// read stream into input buffer
// delete the stream when an eof is received
// return the number of bytes read
//
/*?hidden?*/
int
IvlBufStream :: ReadInput ()
{
/* read in buffer */
if (InBuffer.BufLength () == 0)
InBuffer.NeedSize (128);
int n = Read (InBuffer);
/* handle errors */
if (n == -2) {
InBuffer.Grow ();
n = Read (InBuffer);
}
#if 0
if (n < 0)
SysError (ErrWarn, "IvlBufStream::ReadInput");
if (n <= 0)
Closing (n < 0 ? false : true);
return n;
#else
/* new code taken from BusAccess.cc (27/11/2000) */
if (n <= 0)
delete this;
return n;
#endif
}
/*?
This virtual function is called when an end of file is read,
meaning that the communication is terminated.
It is also called if an error occurs while reading.
You can check the value of the global variable \var{errno}:
if it is non zero, \fun{Closing} was called because of an error. % wrong
By default this function does nothing.
?*/
void
IvlBufStream :: Closing (bool)
{
}
void
IvlBufStream :: WriteLong (lword l)
{
OutBuffer << l;
if (Sync || OutBuffer.BufLength () >= OutSize)
Flush ();
}
void
IvlBufStream :: WriteShort (sword s)
{
OutBuffer << s;
if (Sync || OutBuffer.BufLength () >= OutSize)
Flush ();
}
void
IvlBufStream :: WriteByte (byte b)
{
OutBuffer << b;
if (Sync || OutBuffer.BufLength () >= OutSize)
Flush ();
}
void
IvlBufStream :: WriteChar (char c)
{
OutBuffer << c;
if (Sync || OutBuffer.BufLength () >= OutSize)
Flush ();
}
void
IvlBufStream :: WriteString (const char* s)
{
OutBuffer << s;
if (Sync || OutBuffer.BufLength () >= OutSize)
Flush ();
}
void
IvlBufStream :: WriteBuf (const byte* b, int n)
{
OutBuffer.WriteBuf (b, n);
if (Sync || OutBuffer.BufLength () >= OutSize)
Flush ();
}
bool
IvlBufStream :: ReadLong (lword& l)
{
return InBuffer.ReadLong (l);
}
bool
IvlBufStream :: ReadShort (sword& s)
{
return InBuffer.ReadShort (s);
}
bool
IvlBufStream :: ReadByte (byte& b)
{
return InBuffer.ReadByte (b);
}
bool
IvlBufStream :: ReadChar (char& c)
{
return InBuffer.ReadChar (c);
}
int
IvlBufStream :: ReadString (char* s, int n)
{
return InBuffer.ReadString (s, n);
}
int
IvlBufStream :: ReadString (IvlString& s)
{
return InBuffer.ReadString (s);
}
bool
IvlBufStream :: ReadBuf (byte* b, int n)
{
return InBuffer.ReadBuf (b, n);
}
#ifdef DOC
/*?
This function defines the size of the output buffer that triggers automatic flushing
in asynchronous mode. By default the flush size is the maximum size of the
output buffer. As a consequence, it is changed by \fun{OutBuffer}.
?*/
void
IvlBufStream :: FlushSize (int n)
{ }
/*?nextdoc?*/
void
IvlBufStream :: SetSyncMode (bool s)
{ }
/*?
A buffered stream can be in synchronous or asynchronous mode.
In asynchronous mode output is buffered while in synchronous mode it is not.
Synchronous mode is usually less efficient than asynchronous mode
because it makes more system calls to transfer data;
however synchronous mode can be useful for debugging applications.
?*/
bool
IvlBufStream :: GetSyncMode ()
{ }
#endif /* DOC */
|