3 Star 3 Fork 1

Gitee 极速下载/kbengine-unity3d-plugins

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/kbengine/kbengine_unity3d_plugins
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
PacketSender.cs 4.19 KB
一键复制 编辑 原始数据 按行查看 历史
namespace KBEngine
{
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;
/*
包发送模块(与服务端网络部分的名称对应)
处理网络数据的发送
*/
public class PacketSender
{
public delegate void AsyncSendMethod();
private byte[] _buffer;
int _wpos = 0; // 写入的数据位置
int _spos = 0; // 发送完毕的数据位置
object _sendingObj = new object();
Boolean _sending = false;
private NetworkInterface _networkInterface = null;
AsyncCallback _asyncCallback = null;
AsyncSendMethod _asyncSendMethod;
public PacketSender(NetworkInterface networkInterface)
{
_init(networkInterface);
}
~PacketSender()
{
Dbg.DEBUG_MSG("PacketSender::~PacketSender(), destroyed!");
}
void _init(NetworkInterface networkInterface)
{
_networkInterface = networkInterface;
_buffer = new byte[KBEngineApp.app.getInitArgs().SEND_BUFFER_MAX];
_asyncSendMethod = new AsyncSendMethod(this._asyncSend);
_asyncCallback = new AsyncCallback(_onSent);
_wpos = 0;
_spos = 0;
_sending = false;
}
public NetworkInterface networkInterface()
{
return _networkInterface;
}
public bool send(MemoryStream stream)
{
int dataLength = (int)stream.length();
if (dataLength <= 0)
return true;
Monitor.Enter(_sendingObj);
if (!_sending)
{
if (_wpos == _spos)
{
_wpos = 0;
_spos = 0;
}
}
int t_spos = _spos;
int space = 0;
int tt_wpos = _wpos % _buffer.Length;
int tt_spos = t_spos % _buffer.Length;
if(tt_wpos >= tt_spos)
space = _buffer.Length - tt_wpos + tt_spos - 1;
else
space = tt_spos - tt_wpos - 1;
if (dataLength > space)
{
Dbg.ERROR_MSG("PacketSender::send(): no space, Please adjust 'SEND_BUFFER_MAX'! data(" + dataLength
+ ") > space(" + space + "), wpos=" + _wpos + ", spos=" + t_spos);
return false;
}
int expect_total = tt_wpos + dataLength;
if(expect_total <= _buffer.Length)
{
Array.Copy(stream.data(), stream.rpos, _buffer, tt_wpos, dataLength);
}
else
{
int remain = _buffer.Length - tt_wpos;
Array.Copy(stream.data(), stream.rpos, _buffer, tt_wpos, remain);
Array.Copy(stream.data(), stream.rpos + remain, _buffer, 0, expect_total - _buffer.Length);
}
_wpos += dataLength;
if (!_sending)
{
_sending = true;
Monitor.Exit(_sendingObj);
_startSend();
}
else
{
Monitor.Exit(_sendingObj);
}
return true;
}
void _startSend()
{
// 由于socket用的是非阻塞式,因此在这里不能直接使用socket.send()方法
// 必须放到另一个线程中去做
_asyncSendMethod.BeginInvoke(_asyncCallback, _asyncSendMethod);
}
void _asyncSend()
{
if (_networkInterface == null || !_networkInterface.valid())
{
Dbg.WARNING_MSG("PacketSender::_asyncSend(): network interface invalid!");
return;
}
var socket = _networkInterface.sock();
while (true)
{
Monitor.Enter(_sendingObj);
int sendSize = _wpos - _spos;
int t_spos = _spos % _buffer.Length;
if (t_spos == 0)
t_spos = sendSize;
if (sendSize > _buffer.Length - t_spos)
sendSize = _buffer.Length - t_spos;
int bytesSent = 0;
try
{
bytesSent = socket.Send(_buffer, _spos % _buffer.Length, sendSize, 0);
}
catch (SocketException se)
{
Dbg.ERROR_MSG(string.Format("PacketSender::_asyncSend(): send data error, disconnect from '{0}'! error = '{1}'", socket.RemoteEndPoint, se));
Event.fireIn("_closeNetwork", new object[] { _networkInterface });
Monitor.Exit(_sendingObj);
return;
}
_spos += bytesSent;
// 所有数据发送完毕了
if (_spos == _wpos)
{
_sending = false;
Monitor.Exit(_sendingObj);
return;
}
Monitor.Exit(_sendingObj);
}
}
private static void _onSent(IAsyncResult ar)
{
AsyncSendMethod caller = (AsyncSendMethod)ar.AsyncState;
caller.EndInvoke(ar);
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/mirrors/kbengine-unity3d-plugins.git
git@gitee.com:mirrors/kbengine-unity3d-plugins.git
mirrors
kbengine-unity3d-plugins
kbengine-unity3d-plugins
master

搜索帮助