Skip to content

Commit f334f2e

Browse files
committed
unity sdk .net4.x支持 #1148
1 parent bcfce90 commit f334f2e

File tree

3 files changed

+12
-16
lines changed

3 files changed

+12
-16
lines changed

NetworkInterface.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
using System.Text;
1010
using System.Text.RegularExpressions;
1111
using System.Threading;
12-
using System.Runtime.Remoting.Messaging;
1312

1413
using MessageID = System.UInt16;
1514
using MessageLength = System.UInt16;
@@ -36,6 +35,7 @@ public class ConnectState
3635
public string connectIP = "";
3736
public int connectPort = 0;
3837
public ConnectCallback connectCB = null;
38+
public AsyncConnectMethod caller = null;
3939
public object userData = null;
4040
public Socket socket = null;
4141
public NetworkInterface networkInterface = null;
@@ -166,13 +166,11 @@ private void _asyncConnect(ConnectState state)
166166
private void _asyncConnectCB(IAsyncResult ar)
167167
{
168168
ConnectState state = (ConnectState)ar.AsyncState;
169-
AsyncResult result = (AsyncResult)ar;
170-
AsyncConnectMethod caller = (AsyncConnectMethod)result.AsyncDelegate;
171-
169+
172170
Dbg.DEBUG_MSG(string.Format("NetWorkInterface::_asyncConnectCB(), connect to '{0}:{1}' finish. error = '{2}'", state.connectIP, state.connectPort, state.error));
173171

174172
// Call EndInvoke to retrieve the results.
175-
caller.EndInvoke(ar);
173+
state.caller.EndInvoke(ar);
176174
Event.fireIn("_onConnectionState", new object[] { state });
177175
}
178176

@@ -194,22 +192,24 @@ public void connectTo(string ip, int port, ConnectCallback callback, object user
194192
_socket.NoDelay = true;
195193
//_socket.Blocking = false;
196194

195+
AsyncConnectMethod asyncConnectMethod = new AsyncConnectMethod(this._asyncConnect);
196+
197197
ConnectState state = new ConnectState();
198198
state.connectIP = ip;
199199
state.connectPort = port;
200200
state.connectCB = callback;
201201
state.userData = userData;
202202
state.socket = _socket;
203203
state.networkInterface = this;
204+
state.caller = asyncConnectMethod;
204205

205206
Dbg.DEBUG_MSG("connect to " + ip + ":" + port + " ...");
206207
connected = false;
207208

208209
// 先注册一个事件回调,该事件在当前线程触发
209210
Event.registerIn("_onConnectionState", this, "_onConnectionState");
210211

211-
var v = new AsyncConnectMethod(this._asyncConnect);
212-
v.BeginInvoke(state, new AsyncCallback(this._asyncConnectCB), state);
212+
asyncConnectMethod.BeginInvoke(state, new AsyncCallback(this._asyncConnectCB), state);
213213
}
214214

215215
public bool send(MemoryStream stream)

PacketReceiver.cs

+3-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using System.Text;
99
using System.Text.RegularExpressions;
1010
using System.Threading;
11-
using System.Runtime.Remoting.Messaging;
1211

1312
using MessageID = System.UInt16;
1413
using MessageLength = System.UInt16;
@@ -100,9 +99,8 @@ int _free()
10099

101100
public void startRecv()
102101
{
103-
104-
var v = new AsyncReceiveMethod(this._asyncReceive);
105-
v.BeginInvoke(new AsyncCallback(_onRecv), null);
102+
AsyncReceiveMethod asyncReceiveMethod = new AsyncReceiveMethod(this._asyncReceive);
103+
asyncReceiveMethod.BeginInvoke(new AsyncCallback(_onRecv), asyncReceiveMethod);
106104
}
107105

108106
private void _asyncReceive()
@@ -168,8 +166,7 @@ private void _asyncReceive()
168166

169167
private void _onRecv(IAsyncResult ar)
170168
{
171-
AsyncResult result = (AsyncResult)ar;
172-
AsyncReceiveMethod caller = (AsyncReceiveMethod)result.AsyncDelegate;
169+
AsyncReceiveMethod caller = (AsyncReceiveMethod)ar.AsyncState;
173170
caller.EndInvoke(ar);
174171
}
175172
}

PacketSender.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ void _startSend()
128128
{
129129
// 由于socket用的是非阻塞式,因此在这里不能直接使用socket.send()方法
130130
// 必须放到另一个线程中去做
131-
_asyncSendMethod.BeginInvoke(_asyncCallback, null);
131+
_asyncSendMethod.BeginInvoke(_asyncCallback, _asyncSendMethod);
132132
}
133133

134134
void _asyncSend()
@@ -183,8 +183,7 @@ void _asyncSend()
183183

184184
private static void _onSent(IAsyncResult ar)
185185
{
186-
AsyncResult result = (AsyncResult)ar;
187-
AsyncSendMethod caller = (AsyncSendMethod)result.AsyncDelegate;
186+
AsyncSendMethod caller = (AsyncSendMethod)ar.AsyncState;
188187
caller.EndInvoke(ar);
189188
}
190189
}

0 commit comments

Comments
 (0)