-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathLink.cs
executable file
·131 lines (117 loc) · 3.33 KB
/
Link.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace ssdb
{
class Link
{
private TcpClient sock;
private MemoryStream recv_buf = new MemoryStream(8 * 1024);
public Link(string host, int port) {
sock = new TcpClient(host, port);
sock.NoDelay = true;
sock.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
}
~Link() {
this.close();
}
public void close() {
if(sock != null){
sock.Close();
}
sock = null;
}
public List<byte[]> request(string cmd, params string[] args) {
List<byte[]> req = new List<byte[]>(1 + args.Length);
req.Add(Encoding.Default.GetBytes(cmd));
foreach(string s in args) {
req.Add(Encoding.Default.GetBytes(s));
}
return this.request(req);
}
public List<byte[]> request(string cmd, params byte[][] args) {
List<byte[]> req = new List<byte[]>(1 + args.Length);
req.Add(Encoding.Default.GetBytes(cmd));
req.AddRange(args);
return this.request(req);
}
public List<byte[]> request(List<byte[]> req) {
MemoryStream buf = new MemoryStream();
foreach(byte[] p in req) {
byte[] len = Encoding.Default.GetBytes(p.Length.ToString());
buf.Write(len, 0, len.Length);
buf.WriteByte((byte)'\n');
buf.Write(p, 0, p.Length);
buf.WriteByte((byte)'\n');
}
buf.WriteByte((byte)'\n');
byte[] bs = buf.GetBuffer();
sock.GetStream().Write(bs, 0, (int)buf.Length);
//Console.Write(Encoding.Default.GetString(bs, 0, (int)buf.Length));
return recv();
}
private List<byte[]> recv() {
while(true) {
List<byte[]> ret = parse();
if(ret != null) {
return ret;
}
byte[] bs = new byte[8192];
int len = sock.GetStream().Read(bs, 0, bs.Length);
//Console.WriteLine("<< " + Encoding.Default.GetString(bs));
recv_buf.Write(bs, 0, len);
}
}
private static int memchr(byte[] bs, byte b, int offset) {
for(int i = offset; i < bs.Length; i++) {
if(bs[i] == b) {
return i;
}
}
return -1;
}
private List<byte[]> parse() {
List<byte[]> list = new List<byte[]>();
byte[] buf = recv_buf.GetBuffer();
int idx = 0;
while(true) {
int pos = memchr(buf, (byte)'\n', idx);
//System.out.println("pos: " + pos + " idx: " + idx);
if(pos == -1) {
break;
}
if(pos == idx || (pos == idx + 1 && buf[idx] == '\r')) {
idx += 1; // if '\r', next time will skip '\n'
// ignore empty leading lines
if(list.Count == 0) {
continue;
} else {
int left = (int)recv_buf.Length - idx;
recv_buf = new MemoryStream(8192);
if(left > 0) {
recv_buf.Write(buf, idx, left);
}
return list;
}
}
byte[] lens = new byte[pos - idx];
Array.Copy(buf, idx, lens, 0, lens.Length);
int len = Int32.Parse(Encoding.Default.GetString(lens));
idx = pos + 1;
if(idx + len >= recv_buf.Length) {
break;
}
byte[] data = new byte[len];
Array.Copy(buf, idx, data, 0, (int)data.Length);
//Console.WriteLine("len: " + len + " data: " + Encoding.Default.GetString(data));
idx += len + 1; // skip '\n'
list.Add(data);
}
return null;
}
}
}