Skip to content

Commit

Permalink
Merge pull request #31 from jay602/master
Browse files Browse the repository at this point in the history
unity sdk .net4.x支持 #1148
  • Loading branch information
kbengine authored May 8, 2019
2 parents 8494c76 + 03522b1 commit 4f190a3
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 46 deletions.
53 changes: 27 additions & 26 deletions Assets/Plugins/kbengine_unity3d_plugins/NetworkInterfaceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Runtime.Remoting.Messaging;

using MessageID = System.UInt16;
using MessageLength = System.UInt16;
Expand All @@ -31,7 +30,7 @@ public abstract class NetworkInterfaceBase
protected Socket _socket = null;
protected PacketReceiverBase _packetReceiver = null;
protected PacketSenderBase _packetSender = null;
protected EncryptionFilter _filter = null;
protected EncryptionFilter _filter = null;

public bool connected = false;

Expand All @@ -41,6 +40,7 @@ public class ConnectState
public string connectIP = "";
public int connectPort = 0;
public ConnectCallback connectCB = null;
public AsyncConnectMethod caller = null;
public object userData = null;
public Socket socket = null;
public NetworkInterfaceBase networkInterface = null;
Expand Down Expand Up @@ -88,18 +88,18 @@ public virtual void reset()
}


public virtual void close()
{
if(_socket != null)
public virtual void close()
{
if(_socket != null)
{
_socket.Close(0);
_socket = null;
Event.fireAll(EventOutTypes.onDisconnected);
}
}

_socket = null;
connected = false;
}
_socket = null;
connected = false;
}

protected abstract PacketReceiverBase createPacketReceiver();
protected abstract PacketSenderBase createPacketSender();
Expand Down Expand Up @@ -186,14 +186,13 @@ protected virtual void onAsyncConnectCB(ConnectState state)
private void _asyncConnectCB(IAsyncResult ar)
{
ConnectState state = (ConnectState)ar.AsyncState;
AsyncResult result = (AsyncResult)ar;
AsyncConnectMethod caller = (AsyncConnectMethod)result.AsyncDelegate;

onAsyncConnectCB(state);

Dbg.DEBUG_MSG(string.Format("NetworkInterfaceBase::_asyncConnectCB(), connect to '{0}:{1}' finish. error = '{2}'", state.connectIP, state.connectPort, state.error));

// Call EndInvoke to retrieve the results.
caller.EndInvoke(ar);
state.caller.EndInvoke(ar);
Event.fireIn("_onConnectionState", new object[] { state });
}

Expand All @@ -210,22 +209,24 @@ public void connectTo(string ip, int port, ConnectCallback callback, object user

_socket = createSocket();

AsyncConnectMethod asyncConnectMethod = new AsyncConnectMethod(this._asyncConnect);

ConnectState state = new ConnectState();
state.connectIP = ip;
state.connectPort = port;
state.connectCB = callback;
state.userData = userData;
state.socket = _socket;
state.networkInterface = this;
state.caller = asyncConnectMethod;

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

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

var v = new AsyncConnectMethod(this._asyncConnect);
v.BeginInvoke(state, new AsyncCallback(this._asyncConnectCB), state);
asyncConnectMethod.BeginInvoke(state, new AsyncCallback(this._asyncConnectCB), state);
}

public virtual bool send(MemoryStream stream)
Expand All @@ -238,10 +239,10 @@ public virtual bool send(MemoryStream stream)
if (_packetSender == null)
_packetSender = createPacketSender();

if (_filter != null)
return _filter.send(_packetSender, stream);
if (_filter != null)
return _filter.send(_packetSender, stream);

return _packetSender.send(stream);
return _packetSender.send(stream);
}

public virtual void process()
Expand All @@ -254,14 +255,14 @@ public virtual void process()
}


public EncryptionFilter fileter()
{
return _filter;
}
public EncryptionFilter fileter()
{
return _filter;
}

public void setFilter(EncryptionFilter filter)
{
_filter = filter;
}
}
public void setFilter(EncryptionFilter filter)
{
_filter = filter;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Runtime.Remoting.Messaging;

using MessageID = System.UInt16;
using MessageLength = System.UInt16;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Runtime.Remoting.Messaging;

using MessageID = System.UInt16;
using MessageLength = System.UInt16;
Expand Down
10 changes: 4 additions & 6 deletions Assets/Plugins/kbengine_unity3d_plugins/PacketReceiverBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Runtime.Remoting.Messaging;

using MessageID = System.UInt16;
using MessageLength = System.UInt16;
Expand All @@ -19,7 +18,7 @@
*/
public abstract class PacketReceiverBase
{
protected delegate void AsyncReceiveMethod();
public delegate void AsyncReceiveMethod();
protected MessageReaderBase _messageReader = null;
protected NetworkInterfaceBase _networkInterface = null;

Expand All @@ -41,16 +40,15 @@ public NetworkInterfaceBase networkInterface()

public virtual void startRecv()
{
var v = new AsyncReceiveMethod(this._asyncReceive);
v.BeginInvoke(new AsyncCallback(_onRecv), null);
AsyncReceiveMethod asyncReceiveMethod = new AsyncReceiveMethod(this._asyncReceive);
asyncReceiveMethod.BeginInvoke(new AsyncCallback(_onRecv), asyncReceiveMethod);
}

protected abstract void _asyncReceive();

private void _onRecv(IAsyncResult ar)
{
AsyncResult result = (AsyncResult)ar;
AsyncReceiveMethod caller = (AsyncReceiveMethod)result.AsyncDelegate;
AsyncReceiveMethod caller = (AsyncReceiveMethod)ar.AsyncState; ;
caller.EndInvoke(ar);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Runtime.Remoting.Messaging;

using MessageID = System.UInt16;
using MessageLength = System.UInt16;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Runtime.Remoting.Messaging;

using MessageID = System.UInt16;
using MessageLength = System.UInt16;
Expand Down
15 changes: 7 additions & 8 deletions Assets/Plugins/kbengine_unity3d_plugins/PacketSenderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Runtime.Remoting.Messaging;

using MessageID = System.UInt16;
using MessageLength = System.UInt16;
Expand All @@ -25,12 +24,12 @@ public abstract class PacketSenderBase
AsyncCallback _asyncCallback = null;
AsyncSendMethod _asyncSendMethod;

public PacketSenderBase(NetworkInterfaceBase networkInterface)
{
_networkInterface = networkInterface;
public PacketSenderBase(NetworkInterfaceBase networkInterface)
{
_networkInterface = networkInterface;
_asyncSendMethod = new AsyncSendMethod(this._asyncSend);
_asyncCallback = new AsyncCallback(_onSent);
}
}

~PacketSenderBase()
{
Expand All @@ -47,15 +46,15 @@ protected void _startSend()
{
// 由于socket用的是非阻塞式,因此在这里不能直接使用socket.send()方法
// 必须放到另一个线程中去做
_asyncSendMethod.BeginInvoke(_asyncCallback, null);

_asyncSendMethod.BeginInvoke(_asyncCallback, _asyncSendMethod);
}

protected abstract void _asyncSend();

protected static void _onSent(IAsyncResult ar)
{
AsyncResult result = (AsyncResult)ar;
AsyncSendMethod caller = (AsyncSendMethod)result.AsyncDelegate;
AsyncSendMethod caller = (AsyncSendMethod)ar.AsyncState;
caller.EndInvoke(ar);
}
}
Expand Down
1 change: 0 additions & 1 deletion Assets/Plugins/kbengine_unity3d_plugins/PacketSenderKCP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Runtime.Remoting.Messaging;

using MessageID = System.UInt16;
using MessageLength = System.UInt16;
Expand Down
1 change: 0 additions & 1 deletion Assets/Plugins/kbengine_unity3d_plugins/PacketSenderTCP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Runtime.Remoting.Messaging;

using MessageID = System.UInt16;
using MessageLength = System.UInt16;
Expand Down

0 comments on commit 4f190a3

Please sign in to comment.