-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.cs
220 lines (192 loc) · 6.16 KB
/
Client.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
using ChivaVR.Net.Core;
using ChivaVR.Net.Toolkit;
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
/// <summary>
/// 张志杰 20170117
/// 信息中心
/// </summary>
namespace ChivaVR.Net.IC
{
/// <summary>
/// 信息中心
/// 封装部分
/// </summary>
public sealed partial class Infocenter : IChivaVRNet
{
private static Infocenter _instance = null;
private Socket _client = null;
private Parser _parser;
private IPEndPoint _remoteEP;
private int _id = 0;
private string _ip = "";
private int _port = 0;
private string _user;
private string _pwd;
private bool _connected = false;
MinaQueue _minaQueue = new MinaQueue();
/// <summary>
/// socket数据接收缓冲大小
/// </summary>
private static int _bufferSize = 2048 * 64;
/// <summary>
/// 客户端实例
/// </summary>
/// <param name="ip">信息中心ip,应保证其合法性</param>
/// <param name="port"></param>
private Infocenter(string ip, int port, string user, string pwd)
{
_ip = ip;
_port = port;
_user = user;
_pwd = pwd;
if (string.IsNullOrEmpty(_ip)
|| string.IsNullOrEmpty(_user)
|| string.IsNullOrEmpty(_pwd)
|| _port == 0
)
{
Notified?.Invoke(CVResult.InfocenterParameterHappened, "服务器参数配置问题");
}
_connected = false;
_remoteEP = new IPEndPoint(IPAddress.Parse(ip), port);
//本机特征
_localIp = Kits.GetIp();
_localName = Environment.MachineName;
Log.Added += AddLog;
}
private void _parser_Responded(object obj)
{
Notified?.Invoke(CVResult.ResourceOperationResponded, obj);
}
private Infocenter(Socket c) { _client = c; }
private void _parser_Info(CVResult result, object obj)
{
Notified?.Invoke(result, obj);
}
private void Connect()
{
_client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_client.BeginConnect(_remoteEP, new AsyncCallback(ConnectCallback), _client);
_connected = true;
}
private void ConnectCallback(IAsyncResult ar)
{
try
{
Socket client = (Socket)ar.AsyncState;
client.EndConnect(ar);
//连接成功,创建剖析器
_parser = new InfocenterParser(this);
_parser.Notified += _parser_Info;
//创建日志读写操作
_logTableOp = OpFactory<TableOp>.Create(_instance);
while (true)
{
if (_client != null)
{
byte[] buff = new byte[_bufferSize];
int bytesRead = _client.Receive(buff);
_minaQueue.Enqueue(buff, bytesRead);
}
else
{
break;
}
Thread.Sleep(10);
}
}
catch (SocketException ex)
{
Reset();
Notified?.Invoke(ex.SocketErrorCode == SocketError.TimedOut
? CVResult.ServerConnectTimeouted : CVResult.ConnectFailed, ex);
}
}
private void ParseMinaQueueThread()
{
try
{
while (true)
{
byte[] body = _minaQueue.Dequeue();
if (body != null)
{
_parser.Do(RC4.Convert(body, 4));
}
Thread.Sleep(10);
}
}
catch (SocketException ex)
{
Shutdown();
Notified?.Invoke(CVResult.ReceiveCallbackFailed, ex);
}
catch (ObjectDisposedException)
{
_connected = false;
Notified?.Invoke(CVResult.InfocenterNotConnected, this);
}
catch (Exception ex)
{
Notified?.Invoke(CVResult.ExceptionHappened, ex);
}
}
/// <summary>
/// 发送数据
/// </summary>
/// <param name="buff"></param>
internal void Send(byte[] buff)
{
try
{
if (_client == null)
{
throw new NullReferenceException("未连接");
}
_client.BeginSend(buff, 0, buff.Length, 0, new AsyncCallback(SendCallback), _client);
}
catch (SocketException sex)
{
_connected = false;
Notified?.Invoke(CVResult.SendFailed, sex);
}
catch (NullReferenceException)
{
_connected = false;
Notified?.Invoke(CVResult.InfocenterNotConnected, this);
}
}
private void Reset()
{
this.Id = 0;
_client = null;
_instance = null;
_connected = false;
}
private void Bye()
{
try
{
OpFactory<ByeOp>.Create(_instance).Say(Id, "shutdown itself");
}
catch
{
}
}
internal void Notify(CVResult r, object o)
{
Notified?.Invoke(r, o);
}
private void SendCallback(IAsyncResult ar)
{
Socket client = (Socket)ar.AsyncState;
if (client.Connected)
{
int bytesSent = client.EndSend(ar);
}
}
}
}