1 Star 0 Fork 0

starlove/kbengine_unity3d_plugins

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
NetworkInterface.cs 6.15 KB
一键复制 编辑 原始数据 按行查看 历史
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 System.Runtime.Remoting.Messaging;
using MessageID = System.UInt16;
using MessageLength = System.UInt16;
/// <summary>
/// 网络模块
/// 处理连接、收发数据
/// </summary>
public class NetworkInterface
{
public delegate void AsyncConnectMethod(ConnectState state);
public const int TCP_PACKET_MAX = 1460;
public delegate void ConnectCallback(string ip, int port, bool success, object userData);
protected 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();
}
~NetworkInterface()
{
Dbg.DEBUG_MSG("NetworkInterface::~NetworkInterface(), destructed!!!");
reset();
}
public virtual Socket sock()
{
return _socket;
}
public void reset()
{
if(valid())
{
Dbg.DEBUG_MSG(string.Format("NetworkInterface::reset(), close socket from '{0}'", _socket.RemoteEndPoint.ToString()));
_socket.Close(0);
}
_socket = null;
_packetReceiver = null;
_packetSender = null;
}
public void close()
{
if(_socket != null)
{
_socket.Close(0);
_socket = null;
Event.fireAll("onDisconnected", new object[]{});
}
_socket = null;
}
public virtual PacketReceiver packetReceiver()
{
return _packetReceiver;
}
public virtual bool valid()
{
return ((_socket != null) && (_socket.Connected == true));
}
public void _onConnectionState(ConnectState state)
{
KBEngine.Event.deregisterIn(this);
bool success = (state.error == "" && valid());
if (success)
{
Dbg.DEBUG_MSG(string.Format("NetworkInterface::_onConnectionState(), connect to {0} is success!", state.socket.RemoteEndPoint.ToString()));
_packetReceiver = new PacketReceiver(this);
_packetReceiver.startRecv();
}
else
{
Dbg.ERROR_MSG(string.Format("NetworkInterface::_onConnectionState(), connect is error! ip: {0}:{1}, err: {2}", state.connectIP, state.connectPort, state.error));
}
Event.fireAll("onConnectionState", 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("_onConnectionState", new object[] { state });
}
catch (Exception e)
{
state.error = e.ToString();
Event.fireIn("_onConnectionState", new object[] { state });
}
}
/// <summary>
/// 在非主线程执行:连接服务器
/// </summary>
private void _asyncConnect(ConnectState state)
{
Dbg.DEBUG_MSG(string.Format("NetWorkInterface::_asyncConnect(), will connect to '{0}:{1}' ...", state.connectIP, state.connectPort));
try
{
state.socket.Connect(state.connectIP, state.connectPort);
}
catch (Exception e)
{
Dbg.ERROR_MSG(string.Format("NetWorkInterface::_asyncConnect(), connect to '{0}:{1}' fault! error = '{2}'", state.connectIP, state.connectPort, e));
state.error = e.ToString();
}
}
/// <summary>
/// 在非主线程执行:连接服务器结果回调
/// </summary>
private void _asyncConnectCB(IAsyncResult ar)
{
ConnectState state = (ConnectState)ar.AsyncState;
AsyncResult result = (AsyncResult)ar;
AsyncConnectMethod caller = (AsyncConnectMethod)result.AsyncDelegate;
Dbg.DEBUG_MSG(string.Format("NetWorkInterface::_asyncConnectCB(), connect to '{0}:{1}' finish. error = '{2}'", state.connectIP, state.connectPort, state.error));
// Call EndInvoke to retrieve the results.
caller.EndInvoke(ar);
Event.fireIn("_onConnectionState", 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;
//_socket.Blocking = false;
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("_onConnectionState", this, "_onConnectionState");
var v = new AsyncConnectMethod(this._asyncConnect);
v.BeginInvoke(state, new AsyncCallback(this._asyncConnectCB), state);
}
public bool send(MemoryStream stream)
{
if (!valid())
{
throw new ArgumentException("invalid socket!");
}
if (_packetSender == null)
_packetSender = new PacketSender(this);
return _packetSender.send(stream);
}
public void process()
{
if (!valid())
return;
if (_packetReceiver != null)
_packetReceiver.process();
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/starlove/kbengine_unity3d_plugins.git
git@gitee.com:starlove/kbengine_unity3d_plugins.git
starlove
kbengine_unity3d_plugins
kbengine_unity3d_plugins
master

搜索帮助