1 Star 0 Fork 0

子弹大仙/kbengine_unity3d_plugins

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
NetworkInterface.cs 4.89 KB
一键复制 编辑 原始数据 按行查看 历史
liquidx 提交于 2015-02-27 14:49 . up
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.fireAll("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);
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();
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/BulletHermit/kbengine_unity3d_plugins.git
[email protected]:BulletHermit/kbengine_unity3d_plugins.git
BulletHermit
kbengine_unity3d_plugins
kbengine_unity3d_plugins
master

搜索帮助