forked from kbengine/kbengine_unity3d_plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkInterface.cs
More file actions
200 lines (168 loc) · 5.07 KB
/
NetworkInterface.cs
File metadata and controls
200 lines (168 loc) · 5.07 KB
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
namespace KBEngine
{
using UnityEngine;
using System;
using System.Net.Sockets;
using System.Net;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using MessageID = System.UInt16;
using MessageLength = System.UInt16;
/*
网络模块
处理连接、收发数据
*/
public class NetworkInterface
{
public const int TCP_PACKET_MAX = 1460;
public delegate void ConnectCallback(string ip, int port, bool success, object userData);
Socket _socket = null;
PacketReceiver _packetReceiver = null;
PacketSender _packetSender = null;
public class ConnectState
{
// for connect
public string connectIP = "";
public int connectPort = 0;
public ConnectCallback connectCB = null;
public object userData = null;
public Socket socket = null;
public NetworkInterface networkInterface = null;
public string error = "";
}
public NetworkInterface()
{
reset();
}
public void reset()
{
if(valid())
{
_socket.Close(0);
}
_socket = null;
_packetReceiver = null;
_packetSender = null;
}
public Socket sock()
{
return _socket;
}
public PacketReceiver packetReceiver()
{
return _packetReceiver;
}
public bool valid()
{
return ((_socket != null) && (_socket.Connected == true));
}
public void _onConnectStatus(ConnectState state)
{
KBEngine.Event.deregisterIn(this);
bool success = (state.error == "" && valid());
if(success)
{
Dbg.DEBUG_MSG(string.Format("NetworkInterface::_onConnectStatus(), connect to {0} is success!", state.socket.RemoteEndPoint.ToString()));
_packetReceiver = new PacketReceiver(this);
_packetReceiver.startRecv();
}
else
{
Dbg.ERROR_MSG(string.Format("NetworkInterface::_onConnectStatus(), connect is error! ip: {0}:{1}, err: {2}", state.connectIP, state.connectPort, state.error));
}
Event.fireOut("onConnectStatus", new object[]{success});
if (state.connectCB != null)
state.connectCB(state.connectIP, state.connectPort, success, state.userData);
}
private static void connectCB(IAsyncResult ar)
{
ConnectState state = null;
try
{
// Retrieve the socket from the state object.
state = (ConnectState) ar.AsyncState;
// Complete the connection.
state.socket.EndConnect(ar);
Event.fireIn("_onConnectStatus", new object[]{state});
}
catch (Exception e)
{
state.error = e.ToString();
Event.fireIn("_onConnectStatus", new object[]{state});
}
}
public void connectTo(string ip, int port, ConnectCallback callback, object userData)
{
if (valid())
throw new InvalidOperationException( "Have already connected!" );
if(!(new Regex( @"((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))))")).IsMatch(ip))
{
IPHostEntry ipHost = Dns.GetHostEntry (ip);
ip = ipHost.AddressList[0].ToString();
}
// Security.PrefetchSocketPolicy(ip, 843);
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_socket.SetSocketOption (System.Net.Sockets.SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, KBEngineApp.app.getInitArgs().getRecvBufferSize() * 2);
_socket.SetSocketOption(System.Net.Sockets.SocketOptionLevel.Socket, SocketOptionName.SendBuffer, KBEngineApp.app.getInitArgs().getSendBufferSize() * 2);
_socket.NoDelay = true;
ConnectState state = new ConnectState();
state.connectIP = ip;
state.connectPort = port;
state.connectCB = callback;
state.userData = userData;
state.socket = _socket;
state.networkInterface = this;
Dbg.DEBUG_MSG("connect to " + ip + ":" + port + " ...");
// 先注册一个事件回调,该事件在当前线程触发
Event.registerIn("_onConnectStatus", this, "_onConnectStatus");
try
{
_socket.BeginConnect(new IPEndPoint(IPAddress.Parse(ip), port), new AsyncCallback(connectCB), state);
}
catch (Exception e)
{
state.error = e.ToString();
Event.fireIn("_onConnectStatus", new object[]{state});
}
}
public void close()
{
if(_socket != null)
{
_socket.Close(0);
_socket = null;
Event.fireAll("onDisableConnect", new object[]{});
}
_socket = null;
}
public bool send(byte[] datas)
{
if(!valid())
{
throw new ArgumentException ("invalid socket!");
}
if(_packetSender == null)
_packetSender = new PacketSender(this);
try
{
return _packetSender.send(datas);
}
catch (SocketException err)
{
Dbg.ERROR_MSG(string.Format("NetworkInterface::send(): socket error(" + err.ErrorCode + ")!"));
close();
}
return false;
}
public void process()
{
if(!valid())
return;
if(_packetReceiver != null)
_packetReceiver.process();
}
}
}