9
9
using System . Text ;
10
10
using System . Text . RegularExpressions ;
11
11
using System . Threading ;
12
- using System . Runtime . Remoting . Messaging ;
13
12
14
13
using MessageID = System . UInt16 ;
15
14
using MessageLength = System . UInt16 ;
@@ -36,6 +35,7 @@ public class ConnectState
36
35
public string connectIP = "" ;
37
36
public int connectPort = 0 ;
38
37
public ConnectCallback connectCB = null ;
38
+ public AsyncConnectMethod caller = null ;
39
39
public object userData = null ;
40
40
public Socket socket = null ;
41
41
public NetworkInterface networkInterface = null ;
@@ -166,13 +166,11 @@ private void _asyncConnect(ConnectState state)
166
166
private void _asyncConnectCB ( IAsyncResult ar )
167
167
{
168
168
ConnectState state = ( ConnectState ) ar . AsyncState ;
169
- AsyncResult result = ( AsyncResult ) ar ;
170
- AsyncConnectMethod caller = ( AsyncConnectMethod ) result . AsyncDelegate ;
171
-
169
+
172
170
Dbg . DEBUG_MSG ( string . Format ( "NetWorkInterface::_asyncConnectCB(), connect to '{0}:{1}' finish. error = '{2}'" , state . connectIP , state . connectPort , state . error ) ) ;
173
171
174
172
// Call EndInvoke to retrieve the results.
175
- caller . EndInvoke ( ar ) ;
173
+ state . caller . EndInvoke ( ar ) ;
176
174
Event . fireIn ( "_onConnectionState" , new object [ ] { state } ) ;
177
175
}
178
176
@@ -194,22 +192,24 @@ public void connectTo(string ip, int port, ConnectCallback callback, object user
194
192
_socket . NoDelay = true ;
195
193
//_socket.Blocking = false;
196
194
195
+ AsyncConnectMethod asyncConnectMethod = new AsyncConnectMethod ( this . _asyncConnect ) ;
196
+
197
197
ConnectState state = new ConnectState ( ) ;
198
198
state . connectIP = ip ;
199
199
state . connectPort = port ;
200
200
state . connectCB = callback ;
201
201
state . userData = userData ;
202
202
state . socket = _socket ;
203
203
state . networkInterface = this ;
204
+ state . caller = asyncConnectMethod ;
204
205
205
206
Dbg . DEBUG_MSG ( "connect to " + ip + ":" + port + " ..." ) ;
206
207
connected = false ;
207
208
208
209
// 先注册一个事件回调,该事件在当前线程触发
209
210
Event . registerIn ( "_onConnectionState" , this , "_onConnectionState" ) ;
210
211
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 ) ;
213
213
}
214
214
215
215
public bool send ( MemoryStream stream )
0 commit comments