-
Notifications
You must be signed in to change notification settings - Fork 32
/
IO.cs
465 lines (394 loc) · 9.52 KB
/
IO.cs
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/**
* Copyright (C) 2006 Alex Pedenko
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
using System;
using System.IO;
using System.Text;
using System.Threading;
namespace NetSync
{
public enum MsgCode
{
MSG_DONE=5, /* current phase is done */
MSG_REDO=4, /* reprocess indicated flist index */
MSG_ERROR=1, MSG_INFO=2, MSG_LOG=3, /* remote logging */
MSG_DATA=0 /* raw data on the multiplexed stream */
};
public class IOStream
{
private Stream sockOut;
private Stream sockIn;
private bool IOMultiplexingIn = false;
private bool IOMultiplexingOut = false;
private int MPLEX_BASE = 7;
private int IO_BUFFER_SIZE = 4096;
private int iobuf_in_siz = 0;
private int remaining = 0;
private int iobuf_in_ndx = 0;
private byte[] iobuf_in = null;
private byte[] iobuf_out = null;
private int iobuf_out_cnt = 0;
private int no_flush = 0;
private ASCIIEncoding asen = new ASCIIEncoding();
public Thread ClientThread = null;
public IOStream(Stream f)
{
IOSetSocketFds(f,f);
iobuf_in_siz = 2 * IO_BUFFER_SIZE;
}
public void CalculationTotalWritten(int len){
Options.stats.totalWritten += len;
}
public void CalculationTotalRead(int len)
{
Options.stats.totalRead += len;
}
public UInt32 IVAL(byte[] b, int offset)
{
return (UInt32)(b[offset] + (b[offset + 1] << 8) + (b[offset + 2] << 16) + (b[offset + 3] << 24));
}
public void Write(byte[] buf, int offset, int len)
{
try
{
WriteFD(buf, offset, len);
} catch(Exception e)
{
Log.Write(e.Message);
}
CalculationTotalWritten(len);
}
public void WriteFD(byte[] buf, int offset, int len)
{
if (iobuf_out == null)
{
sockOut.Write(buf, offset, len);
return;
}
int off = 0;
while (len > 0)
{
int n = Math.Min(len, IO_BUFFER_SIZE - iobuf_out_cnt);
if (n > 0)
{
Util.MemCpy(iobuf_out,iobuf_out_cnt, buf, offset + off, n);
off += n;
len -= n;
iobuf_out_cnt += n;
}
if (iobuf_out_cnt == IO_BUFFER_SIZE)
Flush();
}
}
public void MplexWrite(MsgCode code, byte[] buf, int len)
{
byte[] buffer = new byte[4096];
int n = len;
CheckSum.SIVAL(ref buffer, 0, (UInt32)(((MPLEX_BASE + (int)code)<<24) + len));
if (n > buffer.Length - 4)
n = buffer.Length - 4;
Util.MemCpy(buffer,4,buf,0, n);
sockOut.Write(buffer, 0, n+4);
len -= n;
if (len > 0)
sockOut.Write(buf, n, len);
}
public void Flush()
{
if (iobuf_out_cnt == 0 || no_flush != 0)
return;
if (IOMultiplexingOut)
MplexWrite(MsgCode.MSG_DATA, iobuf_out, iobuf_out_cnt);
else
sockOut.Write(iobuf_out, 0, iobuf_out_cnt);
iobuf_out_cnt = 0;
}
public void writeInt(int x)
{
byte[] arr = new byte[4];
arr[0] = (byte)(x & 0xFF);
arr[1] = (byte)( (x >> 8)& 0xFF);
arr[2] = (byte)( (x >> 16)& 0xFF);
arr[3] = (byte)( (x >> 24));
Write(arr, 0, 4);
}
public void writeUInt(UInt32 x)
{
byte[] arr = new byte[4];
arr[0] = (byte)(x & 0xFF);
arr[1] = (byte)( (x >> 8)& 0xFF);
arr[2] = (byte)( (x >> 16)& 0xFF);
arr[3] = (byte)( (x >> 24));
Write(arr, 0, 4);
}
public void writeByte(byte val)
{
byte[] data = new byte[1];
data[0] = val;
Write(data,0,1);
}
public void WriteLongInt(Int64 x)
{
byte[] b = new byte[8];
if (x <= 0x7FFFFFFF)
{
writeInt((int)x);
return;
}
writeUInt(0xFFFFFFFF);
CheckSum.SIVAL(ref b,0,(UInt32)(x & 0xFFFFFFFF));
CheckSum.SIVAL(ref b,4,(UInt32)((x>>32) & 0xFFFFFFFF));
Write(b,0,8);
}
public void IOPrintf(string message)
{
//byte[] data = usen.GetBytes(message);
byte[] data = asen.GetBytes(message);
Write(data, 0, data.Length);
}
/*
public void checkTimeOut()
{
if(Options.ioTimeout == 0)
return;
if(Options.lastIO == DateTime.MinValue)
{
Options.lastIO = DateTime.Now;
return;
}
if(DateTime.Now.Ticks - Options.lastIO.Ticks >= Options.ioTimeout * 10000000)
{
if(!Options.amServer && !Options.amDaemon)
Log.WriteLine("io timeout after "+(DateTime.Now.Ticks - Options.lastIO.Ticks)/10000000+" seconds - exiting");
Environment.Exit(0);
}
}
*/
public string readFilesFromLine(Stream fd, Options options)
{
string fileName = "";
bool readingRemotely = options.remoteFilesFromFile != null;
bool nulls = options.eolNulls || readingRemotely;
while(true)
{
while(true)
{
int readByte = fd.ReadByte();
if(readByte==-1)
break;
// ...select
if(nulls ? readByte == '\0' : (readByte == '\r' || readByte == '\n'))
{
if(!readingRemotely && fileName == "")
continue;
break;
}
fileName += readByte;
}
if(fileName == "" || !(fileName[0] == '#' || fileName[0] == ';') )
break;
continue;
}
return fileName;
}
public void IOSetSocketFds(Stream inSock, Stream outSock)
{
sockIn = inSock;
sockOut = outSock;
}
public string ReadLine()
{
StringBuilder data = new StringBuilder();
while(true)
{
int readInt = readByte();
if(readInt == -1)
return data.ToString();
char read = Convert.ToChar(readInt);
if(read != '\r')
data.Append(read);
if(read == '\n')
break;
}
return data.ToString();
}
public string ReadSBuf(int len)
{
byte[] data = new byte[len];
data = ReadBuf(len);
return ASCIIEncoding.ASCII.GetString(data);
}
public byte[] ReadBuf(int len)
{
byte[] data = new byte[len];
int ret;
int total = 0;
while (total < len)
{
try
{
ret = ReadfdUnbuffered(data, total, len-total);
total += ret;
} catch (Exception e)
{
MainClass.Exit("Unable to read data from the transport connection", null);
if(ClientThread != null)
ClientThread.Abort();
return null;
}
}
CalculationTotalRead(len);
return data;
}
public void ReadLoop(byte[] data, int len)
{
int off = 0;
while (len > 0)
{
Flush();
int n = sockIn.Read(data, off, len);
off += n;
len -= n;
}
}
public int ReadfdUnbuffered(byte[] data, int off, int len)
{
int tag, ret = 0;
byte[] line = new byte[1024];
if (iobuf_in == null)
return sockIn.Read(data, off, len);
if (!IOMultiplexingIn && remaining == 0)
{
Flush();
remaining = sockIn.Read(iobuf_in, 0, iobuf_in_siz);
iobuf_in_ndx = 0;
}
while (ret == 0) {
if (remaining != 0) {
len = Math.Min(len, remaining);
Util.MemCpy(data, off, iobuf_in, iobuf_in_ndx, len);
iobuf_in_ndx += len;
remaining -= len;
ret = len;
break;
}
ReadLoop(line, 4);
tag = (int)IVAL(line, 0);
remaining = tag & 0xFFFFFF;
tag = (tag >> 24) - MPLEX_BASE;
switch ((MsgCode)tag) {
case MsgCode.MSG_DATA:
if (remaining > iobuf_in_siz) {
MapFile.ReallocArray(ref iobuf_in, remaining);
iobuf_in_siz = remaining;
}
ReadLoop(iobuf_in, remaining);
iobuf_in_ndx = 0;
break;
case MsgCode.MSG_INFO:
case MsgCode.MSG_ERROR:
if (remaining >= line.Length)
throw new Exception("multiplexing overflow " + tag + ":" + remaining);
ReadLoop(line, remaining);
remaining = 0;
break;
default:
throw new Exception("Read unknown message from stream");
break;
}
}
if (remaining == 0)
Flush();
return ret;
}
public int readInt()
{
byte[] arr = new byte[4];
arr = ReadBuf(4);
return arr[0] + (arr[1] << 8) + (arr[2] << 16) + (arr[3] << 24);
}
public byte readByte()
{
byte data;
byte[] tmp = ReadBuf(1);
data = tmp[0];
if(data == -1)
throw new Exception("Can't read from Stream");
else
return Convert.ToByte(data);
}
public Int64 ReadLongInt()
{
Int64 ret;
byte[] b = new byte[8];
ret = readInt();
if ((UInt32)ret != 0xffffffff)
return ret;
b = ReadBuf(8);
ret = IVAL(b,0) | (((Int64)IVAL(b,4))<<32);
return ret;
}
public void IOStartMultiplexIn()
{
Flush();
IOStartBufferingIn();
IOMultiplexingIn = true;
}
public void IOCloseMultiplexIn()
{
IOMultiplexingIn = false;
}
public void IOStartMultiplexOut()
{
Flush();
IOStartBufferingOut();
IOMultiplexingOut = true;
}
public void IOCloseMultiplexOut()
{
IOMultiplexingOut = false;
}
public void IOStartBufferingIn()
{
if (iobuf_in != null)
return;
iobuf_in_siz = 2 * IO_BUFFER_SIZE;
iobuf_in = new byte[iobuf_in_siz];
}
public void IOEndBuffering()
{
Flush();
if (!IOMultiplexingOut)
{
iobuf_in = null;
iobuf_out = null;
}
}
public void IOStartBufferingOut()
{
if (iobuf_out != null)
return;
iobuf_out = new byte[IO_BUFFER_SIZE];
iobuf_out_cnt = 0;
}
public void Close()
{
sockIn.Close();
sockOut.Close();
}
}
}