-
Notifications
You must be signed in to change notification settings - Fork 0
/
SocketWrap.cs
169 lines (143 loc) · 3.2 KB
/
SocketWrap.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
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace Comm
{
class SocketWrap
{
public delegate void ConnectCallback();
public delegate void ListenCallback();
public delegate void DisconnectCallback();
public delegate void ReceiveCallback(string data);
public delegate void ErrorCallback(Exception errdesc);
private Socket _sock, _outSock;
private ConnectCallback _ccb;
private ReceiveCallback _rcb;
private ErrorCallback _ecb;
private ListenCallback _lcb;
private DisconnectCallback _dcb;
private byte[] _recvBuf;
public SocketWrap(
ConnectCallback ccb,
ListenCallback lcb,
ReceiveCallback rcb,
DisconnectCallback dcb,
ErrorCallback ecb)
{
_ccb = ccb;
_rcb = rcb;
_ecb = ecb;
_lcb = lcb;
_dcb = dcb;
_recvBuf = new byte[1024];
}
public EndPoint RemoteAddr
{
get
{
return _outSock.RemoteEndPoint;
}
}
void ReceiveData(IAsyncResult iar)
{
if(!_outSock.Connected){
Disconnect();
return;
}
int len;
try{
len = _outSock.EndReceive(iar);
}catch(Exception){
Disconnect();
return;
}
if(len == 0)
return;
char[] str = new char[len];
for(int i = 0; i < len; i++)
str[i] = (char)_recvBuf[i];
_rcb(new string(str));
StartRecv();
}
void StartRecv()
{
_outSock.BeginReceive(
_recvBuf, 0, _recvBuf.Length,
SocketFlags.None,
ReceiveData,
null);
}
void Connected(IAsyncResult iar)
{
try{
_sock.EndConnect(iar);
_outSock = _sock;
StartRecv();
_ccb();
}catch(Exception e){
_ecb(e);
}
}
public void Lookup(string addr, short port, out EndPoint ep, out Socket sock)
{
ep = new IPEndPoint(Dns.GetHostEntry(addr).AddressList[0], port);
sock = new Socket(ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
}
public void Connect(string addr, short port)
{
EndPoint end;
Lookup(addr, port, out end, out _sock);
_sock.BeginConnect(end, Connected, null);
}
void Accept(IAsyncResult ar)
{
if(_sock == null)
return;
try{
_outSock = _sock.EndAccept(ar);
}catch(Exception e){
_outSock = null;
if(_sock != null){
_sock.Close();
_sock = null;
}
_ecb(e);
return;
}
_sock.Close();
_sock = null;
StartRecv();
_lcb();
}
public void Listen(short port)
{
EndPoint end;
Lookup("localhost", port, out end, out _sock);
_sock.Bind(end);
_sock.Listen(1);
_sock.BeginAccept(Accept, null);
}
public void Disconnect()
{
if(_outSock != null){
_outSock.Disconnect(/*reuse=*/true);
_outSock.Close();
_outSock = null;
_dcb(); // only disconnected in this case
}else if(_sock != null){
_sock.Close();
_sock = null;
}
}
public void SendData(string line)
{
char[] chArr = line.ToCharArray();
int bufLen = chArr.Length;
byte[] buffer = new byte[bufLen];
for(int i = 0; i < bufLen; i++)
buffer[i] = (byte)chArr[i];
_outSock.Send(buffer, 0, bufLen, SocketFlags.None);
}
}
}