From f7d52d59396378389d3c040156ee4471065bb96b Mon Sep 17 00:00:00 2001 From: liuxq Date: Mon, 19 Dec 2016 23:09:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=87=E7=BA=A7=E5=88=B00.9.7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/kbengine_unity3d_plugins-0.8.10.meta | 9 - .../PacketReceiver.cs | 177 -------------- .../PacketSender.cs | 173 -------------- ...ta => kbengine_unity3d_plugins-0.9.7.meta} | 4 +- .../Bundle.cs | 2 +- .../Bundle.cs.meta | 4 +- .../DataTypes.cs | 0 .../DataTypes.cs.meta | 4 +- .../Dbg.cs | 0 .../Dbg.cs.meta | 4 +- .../Entity.cs | 13 +- .../Entity.cs.meta | 4 +- .../EntityDef.cs | 0 .../EntityDef.cs.meta | 4 +- .../Event.cs | 0 .../Event.cs.meta | 4 +- .../KBEMain.cs | 0 .../KBEMain.cs.meta | 4 +- .../KBEngine.cs | 156 ++++++++---- .../KBEngine.cs.meta | 4 +- .../KBEngineArgs.cs | 0 .../KBEngineArgs.cs.meta | 4 +- .../Mailbox.cs | 0 .../Mailbox.cs.meta | 4 +- .../Math.cs | 0 .../Math.cs.meta | 4 +- .../MemoryStream.cs | 0 .../MemoryStream.cs.meta | 4 +- .../Message.cs | 0 .../Message.cs.meta | 4 +- .../MessageReader.cs | 16 ++ .../MessageReader.cs.meta | 4 +- .../Method.cs | 0 .../Method.cs.meta | 4 +- .../NetworkInterface.cs | 222 ++++++++++-------- .../NetworkInterface.cs.meta | 4 +- .../ObjectPool.cs | 0 .../ObjectPool.cs.meta | 4 +- .../PacketReceiver.cs | 176 ++++++++++++++ .../PacketReceiver.cs.meta | 4 +- .../PacketSender.cs | 174 ++++++++++++++ .../PacketSender.cs.meta | 4 +- .../PersistentInfos.cs} | 6 +- .../PersistentInfos.cs.meta} | 4 +- .../Profile.cs | 0 .../Profile.cs.meta | 4 +- .../Property.cs | 12 + .../Property.cs.meta | 4 +- .../README.md | 0 .../README.md.meta | 4 +- .../ScriptModule.cs | 0 .../ScriptModule.cs.meta | 4 +- .../script/script_kbe/interface/GameObject.cs | Bin 8532 -> 8552 bytes 53 files changed, 675 insertions(+), 561 deletions(-) delete mode 100644 Assets/kbengine_unity3d_plugins-0.8.10.meta delete mode 100644 Assets/kbengine_unity3d_plugins-0.8.10/PacketReceiver.cs delete mode 100644 Assets/kbengine_unity3d_plugins-0.8.10/PacketSender.cs rename Assets/{IGSoft_Resources/Projects/[Resources]/[Sprite]/UserSprite.meta => kbengine_unity3d_plugins-0.9.7.meta} (67%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/Bundle.cs (98%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/Bundle.cs.meta (76%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/DataTypes.cs (100%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/DataTypes.cs.meta (76%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/Dbg.cs (100%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/Dbg.cs.meta (76%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/Entity.cs (97%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/Entity.cs.meta (76%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/EntityDef.cs (100%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/EntityDef.cs.meta (76%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/Event.cs (100%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/Event.cs.meta (76%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/KBEMain.cs (100%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/KBEMain.cs.meta (76%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/KBEngine.cs (94%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/KBEngine.cs.meta (76%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/KBEngineArgs.cs (100%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/KBEngineArgs.cs.meta (76%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/Mailbox.cs (100%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/Mailbox.cs.meta (76%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/Math.cs (100%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/Math.cs.meta (76%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/MemoryStream.cs (100%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/MemoryStream.cs.meta (76%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/Message.cs (100%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/Message.cs.meta (76%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/MessageReader.cs (89%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/MessageReader.cs.meta (76%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/Method.cs (100%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/Method.cs.meta (76%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/NetworkInterface.cs (54%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/NetworkInterface.cs.meta (76%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/ObjectPool.cs (100%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/ObjectPool.cs.meta (76%) create mode 100644 Assets/kbengine_unity3d_plugins-0.9.7/PacketReceiver.cs rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/PacketReceiver.cs.meta (76%) create mode 100644 Assets/kbengine_unity3d_plugins-0.9.7/PacketSender.cs rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/PacketSender.cs.meta (76%) rename Assets/{kbengine_unity3d_plugins-0.8.10/PersistentInofs.cs => kbengine_unity3d_plugins-0.9.7/PersistentInfos.cs} (97%) rename Assets/{kbengine_unity3d_plugins-0.8.10/PersistentInofs.cs.meta => kbengine_unity3d_plugins-0.9.7/PersistentInfos.cs.meta} (76%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/Profile.cs (100%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/Profile.cs.meta (76%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/Property.cs (79%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/Property.cs.meta (76%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/README.md (100%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/README.md.meta (64%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/ScriptModule.cs (100%) rename Assets/{kbengine_unity3d_plugins-0.8.10 => kbengine_unity3d_plugins-0.9.7}/ScriptModule.cs.meta (76%) diff --git a/Assets/kbengine_unity3d_plugins-0.8.10.meta b/Assets/kbengine_unity3d_plugins-0.8.10.meta deleted file mode 100644 index 2a1252a..0000000 --- a/Assets/kbengine_unity3d_plugins-0.8.10.meta +++ /dev/null @@ -1,9 +0,0 @@ -fileFormatVersion: 2 -guid: ec8772ad87c0d4948bd0a369aef3d97d -folderAsset: yes -timeCreated: 1467109502 -licenseType: Free -DefaultImporter: - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/PacketReceiver.cs b/Assets/kbengine_unity3d_plugins-0.8.10/PacketReceiver.cs deleted file mode 100644 index 0c7228e..0000000 --- a/Assets/kbengine_unity3d_plugins-0.8.10/PacketReceiver.cs +++ /dev/null @@ -1,177 +0,0 @@ -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 MessageID = System.UInt16; - using MessageLength = System.UInt16; - - /* - 包接收模块(与服务端网络部分的名称对应) - 处理网络数据的接收 - */ - public class PacketReceiver - { - private MessageReader messageReader = null; - private NetworkInterface _networkInterface = null; - AsyncCallback _asyncCallback = null; - - private byte[] _buffer; - - // socket向缓冲区写的起始位置 - int _wpos = 0; - - // 主线程读取数据的起始位置 - int _rpos = 0; - - public PacketReceiver(NetworkInterface networkInterface) - { - _init(networkInterface); - } - - ~PacketReceiver() - { - Dbg.DEBUG_MSG("PacketReceiver::~PacketReceiver(), destroyed!"); - } - - void _init(NetworkInterface networkInterface) - { - _networkInterface = networkInterface; - _buffer = new byte[KBEngineApp.app.getInitArgs().RECV_BUFFER_MAX]; - _asyncCallback = new AsyncCallback(_onRecv); - - messageReader = new MessageReader(); - } - - public NetworkInterface networkInterface() - { - return _networkInterface; - } - - public void process() - { - int t_wpos = Interlocked.Add(ref _wpos, 0); - - if(_rpos < t_wpos) - { - messageReader.process(_buffer, (UInt32)_rpos, (UInt32)(t_wpos - _rpos)); - Interlocked.Exchange(ref _rpos, t_wpos); - } - else if(t_wpos < _rpos) - { - messageReader.process(_buffer, (UInt32)_rpos, (UInt32)(_buffer.Length - _rpos)); - messageReader.process(_buffer, (UInt32)0, (UInt32)t_wpos); - Interlocked.Exchange(ref _rpos, t_wpos); - } - else - { - // 没有可读数据 - } - } - - int _free() - { - int t_rpos = Interlocked.Add(ref _rpos, 0); - - if(_wpos == _buffer.Length) - { - if(t_rpos == 0) - { - return 0; - } - - Interlocked.Exchange(ref _wpos, 0); - } - - if(t_rpos <= _wpos) - { - return _buffer.Length - _wpos; - } - - return t_rpos - _wpos - 1; - } - - public void startRecv() - { - // 必须有空间可写,否则我们阻塞在线程中直到有空间为止 - int first = 0; - int space = _free(); - - while(space == 0) - { - if(first > 0) - { - if(first > 1000) - throw new Exception("PacketReceiver::startRecv(): no space!"); - - Dbg.WARNING_MSG("PacketReceiver::startRecv(): waiting for space, Please adjust 'RECV_BUFFER_MAX'! retries=" + first); - System.Threading.Thread.Sleep(5); - } - - first += 1; - space = _free(); - } - - try - { - _networkInterface.sock().BeginReceive(_buffer, _wpos, space, 0, - _asyncCallback, this); - } - catch (Exception e) - { - Dbg.ERROR_MSG("PacketReceiver::startRecv(): call ReceiveAsync() is err: " + e.ToString()); - Event.fireIn("_closeNetwork", new object[]{_networkInterface}); - } - } - - private static void _onRecv(IAsyncResult ar) - { - // Retrieve the socket from the state object. - PacketReceiver state = (PacketReceiver) ar.AsyncState; - - try - { - // 由于多线程问题,networkInterface可能已被丢弃了 - // 例如:在连接loginapp之后自动开始连接到baseapp之前会先关闭并丢弃networkInterface - if(!state.networkInterface().valid()) - return; - - Socket client = state.networkInterface().sock(); - - // Read data from the remote device. - int bytesRead = client.EndReceive(ar); - - if (bytesRead > 0) - { - // 更新写位置 - Interlocked.Add(ref state._wpos, bytesRead); - state.startRecv(); - } - else - { - if (bytesRead == 0) - { - Dbg.WARNING_MSG(string.Format("PacketReceiver::_processRecved(): disconnect!")); - Event.fireIn("_closeNetwork", new object[]{state.networkInterface()}); - return; - } - else - { - state.startRecv(); - } - } - } - catch (Exception e) - { - Dbg.ERROR_MSG(string.Format("PacketReceiver::_processRecved(): is error({0})!", e.ToString())); - Event.fireIn("_closeNetwork", new object[]{state.networkInterface()}); - } - } - } -} diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/PacketSender.cs b/Assets/kbengine_unity3d_plugins-0.8.10/PacketSender.cs deleted file mode 100644 index 9a6b59d..0000000 --- a/Assets/kbengine_unity3d_plugins-0.8.10/PacketSender.cs +++ /dev/null @@ -1,173 +0,0 @@ -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 MessageID = System.UInt16; - using MessageLength = System.UInt16; - - /* - 包发送模块(与服务端网络部分的名称对应) - 处理网络数据的发送 - */ - public class PacketSender - { - private byte[] _buffer; - - int _wpos = 0; // 写入的数据位置 - int _spos = 0; // 发送完毕的数据位置 - int _sending = 0; - - private NetworkInterface _networkInterface = null; - AsyncCallback _asyncCallback = null; - - 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]; - _asyncCallback = new AsyncCallback(_onSent); - - _wpos = 0; - _spos = 0; - _sending = 0; - } - - public NetworkInterface networkInterface() - { - return _networkInterface; - } - - public bool send(byte[] datas) - { - if(datas.Length <= 0) - return true; - - bool startSend = false; - if(Interlocked.CompareExchange(ref _sending, 1, 0) == 0) - { - startSend = true; - if(_wpos == _spos) - { - _wpos = 0; - _spos = 0; - } - } - - int t_spos = Interlocked.Add(ref _spos, 0); - 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 (datas.Length > space) - { - Dbg.ERROR_MSG("PacketSender::send(): no space, Please adjust 'SEND_BUFFER_MAX'! data(" + datas.Length - + ") > space(" + space + "), wpos=" + _wpos + ", spos=" + t_spos); - - return false; - } - - int expect_total = tt_wpos + datas.Length; - if(expect_total <= _buffer.Length) - { - Array.Copy(datas, 0, _buffer, tt_wpos, datas.Length); - } - else - { - int remain = _buffer.Length - tt_wpos; - Array.Copy(datas, 0, _buffer, tt_wpos, remain); - Array.Copy(datas, remain, _buffer, 0, expect_total - _buffer.Length); - } - - Interlocked.Add(ref _wpos, datas.Length); - - if(startSend) - { - _startSend(); - } - - return true; - } - - void _startSend() - { - int sendSize = Interlocked.Add(ref _wpos, 0) - _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; - - try - { - _networkInterface.sock().BeginSend(_buffer, _spos % _buffer.Length, sendSize, 0, - _asyncCallback, this); - } - catch (Exception e) - { - Dbg.ERROR_MSG("PacketSender::startSend(): is err: " + e.ToString()); - Event.fireIn("_closeNetwork", new object[]{_networkInterface}); - } - } - - private static void _onSent(IAsyncResult ar) - { - // Retrieve the socket from the state object. - PacketSender state = (PacketSender) ar.AsyncState; - - try - { - // 由于多线程问题,networkInterface可能已被丢弃了 - // 例如:在连接loginapp之后自动开始连接到baseapp之前会先关闭并丢弃networkInterface - if(!state.networkInterface().valid()) - return; - - Socket client = state.networkInterface().sock(); - - // Complete sending the data to the remote device. - int bytesSent = client.EndSend(ar); - - int spos = Interlocked.Add(ref state._spos, bytesSent); - - // 如果数据没有发送完毕需要继续投递发送 - if(spos != Interlocked.Add(ref state._wpos, 0)) - { - state._startSend(); - } - else - { - // 所有数据发送完毕了 - Interlocked.Exchange(ref state._sending, 0); - } - } - catch (Exception e) - { - Dbg.ERROR_MSG(string.Format("PacketSender::_processSent(): is error({0})!", e.ToString())); - Event.fireIn("_closeNetwork", new object[]{state.networkInterface()}); - Interlocked.Exchange(ref state._sending, 0); - } - } - } -} diff --git a/Assets/IGSoft_Resources/Projects/[Resources]/[Sprite]/UserSprite.meta b/Assets/kbengine_unity3d_plugins-0.9.7.meta similarity index 67% rename from Assets/IGSoft_Resources/Projects/[Resources]/[Sprite]/UserSprite.meta rename to Assets/kbengine_unity3d_plugins-0.9.7.meta index 6d1dab8..5c8a2d9 100644 --- a/Assets/IGSoft_Resources/Projects/[Resources]/[Sprite]/UserSprite.meta +++ b/Assets/kbengine_unity3d_plugins-0.9.7.meta @@ -1,7 +1,7 @@ fileFormatVersion: 2 -guid: ec1bd382e472abe4eb30b835e33f2da6 +guid: 28e124bd2581d3b4381312a56eea7dd6 folderAsset: yes -timeCreated: 1456727631 +timeCreated: 1482072541 licenseType: Free DefaultImporter: userData: diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/Bundle.cs b/Assets/kbengine_unity3d_plugins-0.9.7/Bundle.cs similarity index 98% rename from Assets/kbengine_unity3d_plugins-0.8.10/Bundle.cs rename to Assets/kbengine_unity3d_plugins-0.9.7/Bundle.cs index af9cac8..122d4ae 100644 --- a/Assets/kbengine_unity3d_plugins-0.8.10/Bundle.cs +++ b/Assets/kbengine_unity3d_plugins-0.9.7/Bundle.cs @@ -83,7 +83,7 @@ public void send(NetworkInterface networkInterface) for(int i=0; i " + v); diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/Entity.cs.meta b/Assets/kbengine_unity3d_plugins-0.9.7/Entity.cs.meta similarity index 76% rename from Assets/kbengine_unity3d_plugins-0.8.10/Entity.cs.meta rename to Assets/kbengine_unity3d_plugins-0.9.7/Entity.cs.meta index acfb915..3596044 100644 --- a/Assets/kbengine_unity3d_plugins-0.8.10/Entity.cs.meta +++ b/Assets/kbengine_unity3d_plugins-0.9.7/Entity.cs.meta @@ -1,6 +1,6 @@ fileFormatVersion: 2 -guid: 3982b44b53653174eb98da7657707198 -timeCreated: 1467109502 +guid: ce55fac3d3f04ae4d92ddd998c4efeca +timeCreated: 1482072542 licenseType: Free MonoImporter: serializedVersion: 2 diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/EntityDef.cs b/Assets/kbengine_unity3d_plugins-0.9.7/EntityDef.cs similarity index 100% rename from Assets/kbengine_unity3d_plugins-0.8.10/EntityDef.cs rename to Assets/kbengine_unity3d_plugins-0.9.7/EntityDef.cs diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/EntityDef.cs.meta b/Assets/kbengine_unity3d_plugins-0.9.7/EntityDef.cs.meta similarity index 76% rename from Assets/kbengine_unity3d_plugins-0.8.10/EntityDef.cs.meta rename to Assets/kbengine_unity3d_plugins-0.9.7/EntityDef.cs.meta index 5245dde..7de5ff3 100644 --- a/Assets/kbengine_unity3d_plugins-0.8.10/EntityDef.cs.meta +++ b/Assets/kbengine_unity3d_plugins-0.9.7/EntityDef.cs.meta @@ -1,6 +1,6 @@ fileFormatVersion: 2 -guid: 302f32438869b694fa6f13b51fc7e20b -timeCreated: 1467109502 +guid: 45dd16d48a50d9c43b10531822b17ac1 +timeCreated: 1482072541 licenseType: Free MonoImporter: serializedVersion: 2 diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/Event.cs b/Assets/kbengine_unity3d_plugins-0.9.7/Event.cs similarity index 100% rename from Assets/kbengine_unity3d_plugins-0.8.10/Event.cs rename to Assets/kbengine_unity3d_plugins-0.9.7/Event.cs diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/Event.cs.meta b/Assets/kbengine_unity3d_plugins-0.9.7/Event.cs.meta similarity index 76% rename from Assets/kbengine_unity3d_plugins-0.8.10/Event.cs.meta rename to Assets/kbengine_unity3d_plugins-0.9.7/Event.cs.meta index 41344fc..aec9db8 100644 --- a/Assets/kbengine_unity3d_plugins-0.8.10/Event.cs.meta +++ b/Assets/kbengine_unity3d_plugins-0.9.7/Event.cs.meta @@ -1,6 +1,6 @@ fileFormatVersion: 2 -guid: 7479add2f6ea5ee4daba91acebdda71a -timeCreated: 1467109502 +guid: a99b5ce2e1453aa478e3c42676790220 +timeCreated: 1482072541 licenseType: Free MonoImporter: serializedVersion: 2 diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/KBEMain.cs b/Assets/kbengine_unity3d_plugins-0.9.7/KBEMain.cs similarity index 100% rename from Assets/kbengine_unity3d_plugins-0.8.10/KBEMain.cs rename to Assets/kbengine_unity3d_plugins-0.9.7/KBEMain.cs diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/KBEMain.cs.meta b/Assets/kbengine_unity3d_plugins-0.9.7/KBEMain.cs.meta similarity index 76% rename from Assets/kbengine_unity3d_plugins-0.8.10/KBEMain.cs.meta rename to Assets/kbengine_unity3d_plugins-0.9.7/KBEMain.cs.meta index f888795..15e5451 100644 --- a/Assets/kbengine_unity3d_plugins-0.8.10/KBEMain.cs.meta +++ b/Assets/kbengine_unity3d_plugins-0.9.7/KBEMain.cs.meta @@ -1,6 +1,6 @@ fileFormatVersion: 2 -guid: ec4739357af29a74aba0ed4e97d54757 -timeCreated: 1467109503 +guid: f67568d2a217fef45bfda0d2cbb08bb5 +timeCreated: 1482072542 licenseType: Free MonoImporter: serializedVersion: 2 diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/KBEngine.cs b/Assets/kbengine_unity3d_plugins-0.9.7/KBEngine.cs similarity index 94% rename from Assets/kbengine_unity3d_plugins-0.8.10/KBEngine.cs rename to Assets/kbengine_unity3d_plugins-0.9.7/KBEngine.cs index b12b604..405976b 100644 --- a/Assets/kbengine_unity3d_plugins-0.8.10/KBEngine.cs +++ b/Assets/kbengine_unity3d_plugins-0.9.7/KBEngine.cs @@ -86,7 +86,7 @@ public enum CLIENT_TYPE // 服务端与客户端的版本号以及协议MD5 public string serverVersion = ""; - public string clientVersion = "0.8.10"; + public string clientVersion = "0.9.0"; public string serverScriptVersion = ""; public string clientScriptVersion = "0.1.0"; public string serverProtocolMD5 = ""; @@ -94,7 +94,7 @@ public enum CLIENT_TYPE // 持久化插件信息, 例如:从服务端导入的协议可以持久化到本地,下次登录版本不发生改变 // 可以直接从本地加载来提供登录速度 - private PersistentInofs _persistentInofs = null; + private PersistentInfos _persistentInfos = null; // 当前玩家的实体id与实体类别 public UInt64 entity_uuid = 0; @@ -129,7 +129,8 @@ public struct ServerErr // 所有服务端错误码对应的错误描述 public static Dictionary serverErrs = new Dictionary(); - private System.DateTime _lastticktime = System.DateTime.Now; + private System.DateTime _lastTickTime = System.DateTime.Now; + private System.DateTime _lastTickCBTime = System.DateTime.Now; private System.DateTime _lastUpdateToServerTime = System.DateTime.Now; // 玩家当前所在空间的id, 以及空间对应的资源 @@ -164,7 +165,7 @@ public virtual bool initialize(KBEngineArgs args) // 允许持久化KBE(例如:协议,entitydef等) if(args.persistentDataPath != "") - _persistentInofs = new PersistentInofs(args.persistentDataPath); + _persistentInfos = new PersistentInfos(args.persistentDataPath); return true; } @@ -252,7 +253,8 @@ public virtual void reset() _entityIDAliasIDList.Clear(); _bufferedCreateEntityMessage.Clear(); - _lastticktime = System.DateTime.Now; + _lastTickTime = System.DateTime.Now; + _lastTickCBTime = System.DateTime.Now; _lastUpdateToServerTime = System.DateTime.Now; spaceID = 0; @@ -317,13 +319,24 @@ public void sendTick() if(!loginappMessageImported_ && !baseappMessageImported_) return; - TimeSpan span = DateTime.Now - _lastticktime; + TimeSpan span = DateTime.Now - _lastTickTime; // 更新玩家的位置与朝向到服务端 updatePlayerToServer(); if(span.Seconds > 15) { + span = _lastTickCBTime - _lastTickTime; + + // 如果心跳回调接收时间小于心跳发送时间,说明没有收到回调 + // 此时应该通知客户端掉线了 + if(span.Seconds < 0) + { + Dbg.ERROR_MSG("sendTick: Receive appTick timeout!"); + _networkInterface.close(); + return; + } + Message Loginapp_onClientActiveTickMsg = null; Message Baseapp_onClientActiveTickMsg = null; @@ -349,10 +362,18 @@ public void sendTick() } } - _lastticktime = System.DateTime.Now; + _lastTickTime = System.DateTime.Now; } } - + + /* + 服务器心跳回调 + */ + public void Client_onAppActiveTickCB() + { + _lastTickCBTime = System.DateTime.Now; + } + /* 与服务端握手,与任何一个进程连接之后应该第一时间进行握手 */ @@ -407,8 +428,8 @@ public void Client_onVersionNotMatch(MemoryStream stream) Dbg.ERROR_MSG("Client_onVersionNotMatch: verInfo=" + clientVersion + "(server: " + serverVersion + ")"); Event.fireAll("onVersionNotMatch", new object[]{clientVersion, serverVersion}); - if(_persistentInofs != null) - _persistentInofs.onVersionNotMatch(clientVersion, serverVersion); + if(_persistentInfos != null) + _persistentInfos.onVersionNotMatch(clientVersion, serverVersion); } /* @@ -421,8 +442,8 @@ public void Client_onScriptVersionNotMatch(MemoryStream stream) Dbg.ERROR_MSG("Client_onScriptVersionNotMatch: verInfo=" + clientScriptVersion + "(server: " + serverScriptVersion + ")"); Event.fireAll("onScriptVersionNotMatch", new object[]{clientScriptVersion, serverScriptVersion}); - if(_persistentInofs != null) - _persistentInofs.onScriptVersionNotMatch(clientScriptVersion, serverScriptVersion); + if(_persistentInfos != null) + _persistentInfos.onScriptVersionNotMatch(clientScriptVersion, serverScriptVersion); } /* @@ -444,8 +465,8 @@ public void Client_onImportServerErrorsDescr(MemoryStream stream) onImportServerErrorsDescr (stream); - if(_persistentInofs != null) - _persistentInofs.onImportServerErrorsDescr(datas); + if(_persistentInfos != null) + _persistentInfos.onImportServerErrorsDescr(datas); } /* @@ -506,6 +527,8 @@ public void login_loginapp(bool noconnect) private void onConnectTo_loginapp_callback(string ip, int port, bool success, object userData) { + _lastTickCBTime = System.DateTime.Now; + if(!success) { Dbg.ERROR_MSG(string.Format("KBEngine::login_loginapp(): connect {0}:{1} is error!", ip, port)); @@ -522,6 +545,8 @@ private void onConnectTo_loginapp_callback(string ip, int port, bool success, ob private void onLogin_loginapp() { + _lastTickCBTime = System.DateTime.Now; + if(!loginappMessageImported_) { var bundle = Bundle.createObject(); @@ -561,6 +586,8 @@ public void login_baseapp(bool noconnect) private void onConnectTo_baseapp_callback(string ip, int port, bool success, object userData) { + _lastTickCBTime = System.DateTime.Now; + if(!success) { Dbg.ERROR_MSG(string.Format("KBEngine::login_baseapp(): connect {0}:{1} is error!", ip, port)); @@ -577,6 +604,8 @@ private void onConnectTo_baseapp_callback(string ip, int port, bool success, obj private void onLogin_baseapp() { + _lastTickCBTime = System.DateTime.Now; + if(!baseappMessageImported_) { var bundle = Bundle.createObject(); @@ -619,6 +648,8 @@ private void onReConnectTo_baseapp_callback(string ip, int port, bool success, o bundle.writeUint64(entity_uuid); bundle.writeInt32(entity_id); bundle.send(_networkInterface); + + _lastTickCBTime = System.DateTime.Now; } /* @@ -806,8 +837,8 @@ public void Client_onImportClientEntityDef(MemoryStream stream) onImportClientEntityDef (stream); - if(_persistentInofs != null) - _persistentInofs.onImportClientEntityDef(datas); + if(_persistentInfos != null) + _persistentInfos.onImportClientEntityDef(datas); } public void onImportClientEntityDef(MemoryStream stream) @@ -817,19 +848,19 @@ public void onImportClientEntityDef(MemoryStream stream) while(stream.length() > 0) { - string scriptmethod_name = stream.readString(); + string scriptmodule_name = stream.readString(); UInt16 scriptUtype = stream.readUint16(); UInt16 propertysize = stream.readUint16(); UInt16 methodsize = stream.readUint16(); UInt16 base_methodsize = stream.readUint16(); UInt16 cell_methodsize = stream.readUint16(); - Dbg.DEBUG_MSG("KBEngine::Client_onImportClientEntityDef: import(" + scriptmethod_name + "), propertys(" + propertysize + "), " + + Dbg.DEBUG_MSG("KBEngine::Client_onImportClientEntityDef: import(" + scriptmodule_name + "), propertys(" + propertysize + "), " + "clientMethods(" + methodsize + "), baseMethods(" + base_methodsize + "), cellMethods(" + cell_methodsize + ")!"); - ScriptModule module = new ScriptModule(scriptmethod_name); - EntityDef.moduledefs[scriptmethod_name] = module; + ScriptModule module = new ScriptModule(scriptmodule_name); + EntityDef.moduledefs[scriptmodule_name] = module; EntityDef.idmoduledefs[scriptUtype] = module; Type Class = module.script; @@ -855,7 +886,7 @@ public void onImportClientEntityDef(MemoryStream stream) catch (Exception e) { string err = "KBEngine::Client_onImportClientEntityDef: " + - scriptmethod_name + ".set_" + name + ", error=" + e.ToString(); + scriptmodule_name + ".set_" + name + ", error=" + e.ToString(); throw new Exception(err); } @@ -884,7 +915,7 @@ public void onImportClientEntityDef(MemoryStream stream) module.idpropertys[properUtype] = savedata; } - //Dbg.DEBUG_MSG("KBEngine::Client_onImportClientEntityDef: add(" + scriptmethod_name + "), property(" + name + "/" + properUtype + ")."); + //Dbg.DEBUG_MSG("KBEngine::Client_onImportClientEntityDef: add(" + scriptmodule_name + "), property(" + name + "/" + properUtype + ")."); }; while(methodsize > 0) @@ -916,7 +947,7 @@ public void onImportClientEntityDef(MemoryStream stream) } catch (Exception e) { - string err = "KBEngine::Client_onImportClientEntityDef: " + scriptmethod_name + "." + name + ", error=" + e.ToString(); + string err = "KBEngine::Client_onImportClientEntityDef: " + scriptmodule_name + "." + name + ", error=" + e.ToString(); throw new Exception(err); } } @@ -934,7 +965,7 @@ public void onImportClientEntityDef(MemoryStream stream) module.idmethods[methodUtype] = savedata; } - //Dbg.DEBUG_MSG("KBEngine::Client_onImportClientEntityDef: add(" + scriptmethod_name + "), method(" + name + ")."); + //Dbg.DEBUG_MSG("KBEngine::Client_onImportClientEntityDef: add(" + scriptmodule_name + "), method(" + name + ")."); }; while(base_methodsize > 0) @@ -962,7 +993,7 @@ public void onImportClientEntityDef(MemoryStream stream) module.base_methods[name] = savedata; module.idbase_methods[methodUtype] = savedata; - //Dbg.DEBUG_MSG("KBEngine::Client_onImportClientEntityDef: add(" + scriptmethod_name + "), base_method(" + name + ")."); + //Dbg.DEBUG_MSG("KBEngine::Client_onImportClientEntityDef: add(" + scriptmodule_name + "), base_method(" + name + ")."); }; while(cell_methodsize > 0) @@ -989,12 +1020,12 @@ public void onImportClientEntityDef(MemoryStream stream) module.cell_methods[name] = savedata; module.idcell_methods[methodUtype] = savedata; - //Dbg.DEBUG_MSG("KBEngine::Client_onImportClientEntityDef: add(" + scriptmethod_name + "), cell_method(" + name + ")."); + //Dbg.DEBUG_MSG("KBEngine::Client_onImportClientEntityDef: add(" + scriptmodule_name + "), cell_method(" + name + ")."); }; if(module.script == null) { - Dbg.ERROR_MSG("KBEngine::Client_onImportClientEntityDef: module(" + scriptmethod_name + ") not found!"); + Dbg.ERROR_MSG("KBEngine::Client_onImportClientEntityDef: module(" + scriptmodule_name + ") not found!"); } foreach(string name in module.methods.Keys) @@ -1003,7 +1034,7 @@ public void onImportClientEntityDef(MemoryStream stream) if(module.script != null && module.script.GetMethod(name) == null) { - Dbg.WARNING_MSG(scriptmethod_name + "(" + module.script + "):: method(" + name + ") no implement!"); + Dbg.WARNING_MSG(scriptmodule_name + "(" + module.script + "):: method(" + name + ") no implement!"); } }; } @@ -1045,8 +1076,8 @@ public void Client_onImportClientMessages(MemoryStream stream) onImportClientMessages (stream); - if(_persistentInofs != null) - _persistentInofs.onImportClientMessages(currserver, datas); + if(_persistentInfos != null) + _persistentInfos.onImportClientMessages(currserver, datas); } public void onImportClientMessages(MemoryStream stream) @@ -1061,7 +1092,7 @@ public void onImportClientMessages(MemoryStream stream) MessageID msgid = stream.readUint16(); Int16 msglen = stream.readInt16(); - + string msgname = stream.readString(); sbyte argstype = stream.readInt8(); Byte argsize = stream.readUint8(); @@ -1135,7 +1166,8 @@ public void onOpenLoginapp_resetpassword() Dbg.DEBUG_MSG("KBEngine::onOpenLoginapp_resetpassword: successfully!"); currserver = "loginapp"; currstate = "resetpassword"; - + _lastTickCBTime = System.DateTime.Now; + if(!loginappMessageImported_) { Bundle bundle = Bundle.createObject(); @@ -1179,6 +1211,8 @@ public void resetpassword_loginapp(bool noconnect) private void onConnectTo_resetpassword_callback(string ip, int port, bool success, object userData) { + _lastTickCBTime = System.DateTime.Now; + if(!success) { Dbg.ERROR_MSG(string.Format("KBEngine::resetpassword_loginapp(): connect {0}:{1} is error!", ip, port)); @@ -1283,6 +1317,7 @@ public void onOpenLoginapp_createAccount() Dbg.DEBUG_MSG("KBEngine::onOpenLoginapp_createAccount: successfully!"); currserver = "loginapp"; currstate = "createAccount"; + _lastTickCBTime = System.DateTime.Now; if(!loginappMessageImported_) { @@ -1299,6 +1334,8 @@ public void onOpenLoginapp_createAccount() private void onConnectTo_createAccount_callback(string ip, int port, bool success, object userData) { + _lastTickCBTime = System.DateTime.Now; + if(!success) { Dbg.ERROR_MSG(string.Format("KBEngine::createAccount_loginapp(): connect {0}:{1} is error!", ip, port)); @@ -1314,8 +1351,8 @@ private void onConnectTo_createAccount_callback(string ip, int port, bool succes */ public void onServerDigest() { - if(_persistentInofs != null) - _persistentInofs.onServerDigest(currserver, serverProtocolMD5, serverEntitydefMD5); + if(_persistentInfos != null) + _persistentInfos.onServerDigest(currserver, serverProtocolMD5, serverEntitydefMD5); } /* @@ -1386,9 +1423,6 @@ public void Client_onCreatedProxies(UInt64 rndUUID, Int32 eid, string entityType // Dbg.WARNING_MSG("KBEngine::Client_onCreatedProxies: eid(" + eid + ") has exist!"); Client_onEntityDestroyed(eid); } - - MemoryStream entityMessage = null; - _bufferedCreateEntityMessage.TryGetValue(eid, out entityMessage); entity_uuid = rndUUID; entity_id = eid; @@ -1398,6 +1432,7 @@ public void Client_onCreatedProxies(UInt64 rndUUID, Int32 eid, string entityType if(!EntityDef.moduledefs.TryGetValue(entityType, out module)) { Dbg.ERROR_MSG("KBEngine::Client_onCreatedProxies: not found module(" + entityType + ")!"); + return; } Type runclass = module.script; @@ -1415,6 +1450,9 @@ public void Client_onCreatedProxies(UInt64 rndUUID, Int32 eid, string entityType entities[eid] = entity; + MemoryStream entityMessage = null; + _bufferedCreateEntityMessage.TryGetValue(eid, out entityMessage); + if(entityMessage != null) { Client_onUpdatePropertys(entityMessage); @@ -1898,10 +1936,25 @@ public void updatePlayerToServer() bundle.writeFloat(position.x); bundle.writeFloat(position.y); bundle.writeFloat(position.z); + + double x = ((double)direction.x / 360 * (System.Math.PI * 2)); + double y = ((double)direction.y / 360 * (System.Math.PI * 2)); + double z = ((double)direction.z / 360 * (System.Math.PI * 2)); + + // 根据弧度转角度公式会出现负数 + // unity会自动转化到0~360度之间,这里需要做一个还原 + if(x - System.Math.PI > 0.0) + x -= System.Math.PI * 2; - bundle.writeFloat((float)((double)direction.x / 360 * 6.283185307179586)); - bundle.writeFloat((float)((double)direction.y / 360 * 6.283185307179586)); - bundle.writeFloat((float)((double)direction.z / 360 * 6.283185307179586)); + if(y - System.Math.PI > 0.0) + y -= System.Math.PI * 2; + + if(z - System.Math.PI > 0.0) + z -= System.Math.PI * 2; + + bundle.writeFloat((float)x); + bundle.writeFloat((float)y); + bundle.writeFloat((float)z); bundle.writeUint8((Byte)(playerEntity.isOnGround == true ? 1 : 0)); bundle.writeUint32(spaceID); bundle.send(_networkInterface); @@ -1929,9 +1982,24 @@ public void updatePlayerToServer() bundle.writeFloat(position.y); bundle.writeFloat(position.z); - bundle.writeFloat((float)((double)direction.x / 360 * 6.283185307179586)); - bundle.writeFloat((float)((double)direction.y / 360 * 6.283185307179586)); - bundle.writeFloat((float)((double)direction.z / 360 * 6.283185307179586)); + double x = ((double)direction.x / 360 * (System.Math.PI * 2)); + double y = ((double)direction.y / 360 * (System.Math.PI * 2)); + double z = ((double)direction.z / 360 * (System.Math.PI * 2)); + + // 根据弧度转角度公式会出现负数 + // unity会自动转化到0~360度之间,这里需要做一个还原 + if(x - System.Math.PI > 0.0) + x -= System.Math.PI * 2; + + if(y - System.Math.PI > 0.0) + y -= System.Math.PI * 2; + + if(z - System.Math.PI > 0.0) + z -= System.Math.PI * 2; + + bundle.writeFloat((float)x); + bundle.writeFloat((float)y); + bundle.writeFloat((float)z); bundle.writeUint8((Byte)(entity.isOnGround == true ? 1 : 0)); bundle.writeUint32(spaceID); bundle.send(_networkInterface); @@ -2234,7 +2302,7 @@ public void Client_onUpdateData_y(MemoryStream stream) { Int32 eid = getAoiEntityIDFromStream(stream); - float y = stream.readInt8(); + SByte y = stream.readInt8(); _updateVolatileData(eid, 0.0f, 0.0f, 0.0f, y, KBEDATATYPE_BASE.KBE_FLT_MAX, KBEDATATYPE_BASE.KBE_FLT_MAX, -1); } diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/KBEngine.cs.meta b/Assets/kbengine_unity3d_plugins-0.9.7/KBEngine.cs.meta similarity index 76% rename from Assets/kbengine_unity3d_plugins-0.8.10/KBEngine.cs.meta rename to Assets/kbengine_unity3d_plugins-0.9.7/KBEngine.cs.meta index c2a842b..1d979ba 100644 --- a/Assets/kbengine_unity3d_plugins-0.8.10/KBEngine.cs.meta +++ b/Assets/kbengine_unity3d_plugins-0.9.7/KBEngine.cs.meta @@ -1,6 +1,6 @@ fileFormatVersion: 2 -guid: 648ca568278426b4da0e0e89d87f870b -timeCreated: 1467109502 +guid: e7c99df72430532408873e0e2d76c02f +timeCreated: 1482072542 licenseType: Free MonoImporter: serializedVersion: 2 diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/KBEngineArgs.cs b/Assets/kbengine_unity3d_plugins-0.9.7/KBEngineArgs.cs similarity index 100% rename from Assets/kbengine_unity3d_plugins-0.8.10/KBEngineArgs.cs rename to Assets/kbengine_unity3d_plugins-0.9.7/KBEngineArgs.cs diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/KBEngineArgs.cs.meta b/Assets/kbengine_unity3d_plugins-0.9.7/KBEngineArgs.cs.meta similarity index 76% rename from Assets/kbengine_unity3d_plugins-0.8.10/KBEngineArgs.cs.meta rename to Assets/kbengine_unity3d_plugins-0.9.7/KBEngineArgs.cs.meta index 3e3234b..06bd64e 100644 --- a/Assets/kbengine_unity3d_plugins-0.8.10/KBEngineArgs.cs.meta +++ b/Assets/kbengine_unity3d_plugins-0.9.7/KBEngineArgs.cs.meta @@ -1,6 +1,6 @@ fileFormatVersion: 2 -guid: b8e6da6ff48b58e47bd2900b88de0e73 -timeCreated: 1467109503 +guid: d0e933e78a807334ab09c2aebde60f79 +timeCreated: 1482072542 licenseType: Free MonoImporter: serializedVersion: 2 diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/Mailbox.cs b/Assets/kbengine_unity3d_plugins-0.9.7/Mailbox.cs similarity index 100% rename from Assets/kbengine_unity3d_plugins-0.8.10/Mailbox.cs rename to Assets/kbengine_unity3d_plugins-0.9.7/Mailbox.cs diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/Mailbox.cs.meta b/Assets/kbengine_unity3d_plugins-0.9.7/Mailbox.cs.meta similarity index 76% rename from Assets/kbengine_unity3d_plugins-0.8.10/Mailbox.cs.meta rename to Assets/kbengine_unity3d_plugins-0.9.7/Mailbox.cs.meta index f4ae64f..6e12ed2 100644 --- a/Assets/kbengine_unity3d_plugins-0.8.10/Mailbox.cs.meta +++ b/Assets/kbengine_unity3d_plugins-0.9.7/Mailbox.cs.meta @@ -1,6 +1,6 @@ fileFormatVersion: 2 -guid: 8fb989870d426f24096ce3a0bdf0de65 -timeCreated: 1467109502 +guid: 15d112e1e0e67264c9e0df3320fdce13 +timeCreated: 1482072541 licenseType: Free MonoImporter: serializedVersion: 2 diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/Math.cs b/Assets/kbengine_unity3d_plugins-0.9.7/Math.cs similarity index 100% rename from Assets/kbengine_unity3d_plugins-0.8.10/Math.cs rename to Assets/kbengine_unity3d_plugins-0.9.7/Math.cs diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/Math.cs.meta b/Assets/kbengine_unity3d_plugins-0.9.7/Math.cs.meta similarity index 76% rename from Assets/kbengine_unity3d_plugins-0.8.10/Math.cs.meta rename to Assets/kbengine_unity3d_plugins-0.9.7/Math.cs.meta index 5649e3c..e27bde0 100644 --- a/Assets/kbengine_unity3d_plugins-0.8.10/Math.cs.meta +++ b/Assets/kbengine_unity3d_plugins-0.9.7/Math.cs.meta @@ -1,6 +1,6 @@ fileFormatVersion: 2 -guid: 70193636bfe763044981fb2507286b5f -timeCreated: 1467109502 +guid: 4471a58347e00a243b3d1492be5601ab +timeCreated: 1482072541 licenseType: Free MonoImporter: serializedVersion: 2 diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/MemoryStream.cs b/Assets/kbengine_unity3d_plugins-0.9.7/MemoryStream.cs similarity index 100% rename from Assets/kbengine_unity3d_plugins-0.8.10/MemoryStream.cs rename to Assets/kbengine_unity3d_plugins-0.9.7/MemoryStream.cs diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/MemoryStream.cs.meta b/Assets/kbengine_unity3d_plugins-0.9.7/MemoryStream.cs.meta similarity index 76% rename from Assets/kbengine_unity3d_plugins-0.8.10/MemoryStream.cs.meta rename to Assets/kbengine_unity3d_plugins-0.9.7/MemoryStream.cs.meta index 108175b..b2a5631 100644 --- a/Assets/kbengine_unity3d_plugins-0.8.10/MemoryStream.cs.meta +++ b/Assets/kbengine_unity3d_plugins-0.9.7/MemoryStream.cs.meta @@ -1,6 +1,6 @@ fileFormatVersion: 2 -guid: fd16637e260aa1446be1c3d29f0a5c6d -timeCreated: 1467109510 +guid: 5b8ca0edb8f4ef945b57eeec83fa9ed9 +timeCreated: 1482072541 licenseType: Free MonoImporter: serializedVersion: 2 diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/Message.cs b/Assets/kbengine_unity3d_plugins-0.9.7/Message.cs similarity index 100% rename from Assets/kbengine_unity3d_plugins-0.8.10/Message.cs rename to Assets/kbengine_unity3d_plugins-0.9.7/Message.cs diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/Message.cs.meta b/Assets/kbengine_unity3d_plugins-0.9.7/Message.cs.meta similarity index 76% rename from Assets/kbengine_unity3d_plugins-0.8.10/Message.cs.meta rename to Assets/kbengine_unity3d_plugins-0.9.7/Message.cs.meta index 9b120a3..ce68359 100644 --- a/Assets/kbengine_unity3d_plugins-0.8.10/Message.cs.meta +++ b/Assets/kbengine_unity3d_plugins-0.9.7/Message.cs.meta @@ -1,6 +1,6 @@ fileFormatVersion: 2 -guid: 81373a92609fb1c4bb0b71fe96888190 -timeCreated: 1467109502 +guid: ede01cab1d5ba4c49a94c3a2d300d178 +timeCreated: 1482072542 licenseType: Free MonoImporter: serializedVersion: 2 diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/MessageReader.cs b/Assets/kbengine_unity3d_plugins-0.9.7/MessageReader.cs similarity index 89% rename from Assets/kbengine_unity3d_plugins-0.8.10/MessageReader.cs rename to Assets/kbengine_unity3d_plugins-0.9.7/MessageReader.cs index ee883f1..baa55e1 100644 --- a/Assets/kbengine_unity3d_plugins-0.8.10/MessageReader.cs +++ b/Assets/kbengine_unity3d_plugins-0.9.7/MessageReader.cs @@ -63,6 +63,22 @@ public void process(byte[] datas, MessageLengthEx offset, MessageLengthEx length state = READ_STATE.READ_STATE_MSGLEN; expectSize = 2; } + else if(msg.msglen == 0) + { + // 如果是0个参数的消息,那么没有后续内容可读了,处理本条消息并且直接跳到下一条消息 + #if UNITY_EDITOR + Dbg.profileStart(msg.name); + #endif + + msg.handleMessage(stream); + + #if UNITY_EDITOR + Dbg.profileEnd(msg.name); + #endif + + state = READ_STATE.READ_STATE_MSGID; + expectSize = 2; + } else { expectSize = (MessageLengthEx)msg.msglen; diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/MessageReader.cs.meta b/Assets/kbengine_unity3d_plugins-0.9.7/MessageReader.cs.meta similarity index 76% rename from Assets/kbengine_unity3d_plugins-0.8.10/MessageReader.cs.meta rename to Assets/kbengine_unity3d_plugins-0.9.7/MessageReader.cs.meta index a0ca5c7..ccbb480 100644 --- a/Assets/kbengine_unity3d_plugins-0.8.10/MessageReader.cs.meta +++ b/Assets/kbengine_unity3d_plugins-0.9.7/MessageReader.cs.meta @@ -1,6 +1,6 @@ fileFormatVersion: 2 -guid: 1d0f3b6fce19c884d9d0d25171830a49 -timeCreated: 1467109502 +guid: d134a5dcaf84eba4bbbfb7b4af84ddaa +timeCreated: 1482072542 licenseType: Free MonoImporter: serializedVersion: 2 diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/Method.cs b/Assets/kbengine_unity3d_plugins-0.9.7/Method.cs similarity index 100% rename from Assets/kbengine_unity3d_plugins-0.8.10/Method.cs rename to Assets/kbengine_unity3d_plugins-0.9.7/Method.cs diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/Method.cs.meta b/Assets/kbengine_unity3d_plugins-0.9.7/Method.cs.meta similarity index 76% rename from Assets/kbengine_unity3d_plugins-0.8.10/Method.cs.meta rename to Assets/kbengine_unity3d_plugins-0.9.7/Method.cs.meta index 53d0008..188e59c 100644 --- a/Assets/kbengine_unity3d_plugins-0.8.10/Method.cs.meta +++ b/Assets/kbengine_unity3d_plugins-0.9.7/Method.cs.meta @@ -1,6 +1,6 @@ fileFormatVersion: 2 -guid: 08c822ab46ec95f49964088e2c059f35 -timeCreated: 1467109502 +guid: be8790838d745224eb29910646a554eb +timeCreated: 1482072541 licenseType: Free MonoImporter: serializedVersion: 2 diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/NetworkInterface.cs b/Assets/kbengine_unity3d_plugins-0.9.7/NetworkInterface.cs similarity index 54% rename from Assets/kbengine_unity3d_plugins-0.8.10/NetworkInterface.cs rename to Assets/kbengine_unity3d_plugins-0.9.7/NetworkInterface.cs index 4b623d6..6ce97fa 100644 --- a/Assets/kbengine_unity3d_plugins-0.8.10/NetworkInterface.cs +++ b/Assets/kbengine_unity3d_plugins-0.9.7/NetworkInterface.cs @@ -1,31 +1,33 @@ namespace KBEngine { - using UnityEngine; - using System; - using System.Net.Sockets; - using System.Net; - using System.Collections; + 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; - - /* - 网络模块 - 处理连接、收发数据 - */ - public class NetworkInterface - { - public const int TCP_PACKET_MAX = 1460; - public delegate void ConnectCallback(string ip, int port, bool success, object userData); - - protected Socket _socket = null; + + /// + /// 网络模块 + /// 处理连接、收发数据 + /// + 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 @@ -38,10 +40,21 @@ public class ConnectState public string error = ""; } - public NetworkInterface() - { - reset(); - } + public NetworkInterface() + { + reset(); + } + + ~NetworkInterface() + { + Dbg.DEBUG_MSG("NetworkInterface::~NetworkInterface(), destructed!!!"); + reset(); + } + + public virtual Socket sock() + { + return _socket; + } public void reset() { @@ -55,17 +68,25 @@ public void reset() _packetSender = null; } - public Socket sock() - { - return _socket; - } - - public PacketReceiver packetReceiver() + + public void close() + { + if(_socket != null) + { + _socket.Close(0); + _socket = null; + Event.fireAll("onDisableConnect", new object[]{}); + } + + _socket = null; + } + + public virtual PacketReceiver packetReceiver() { return _packetReceiver; } - public bool valid() + public virtual bool valid() { return ((_socket != null) && (_socket.Connected == true)); } @@ -73,9 +94,9 @@ public bool valid() public void _onConnectStatus(ConnectState state) { KBEngine.Event.deregisterIn(this); - + bool success = (state.error == "" && valid()); - if(success) + if (success) { Dbg.DEBUG_MSG(string.Format("NetworkInterface::_onConnectStatus(), connect to {0} is success!", state.socket.RemoteEndPoint.ToString())); _packetReceiver = new PacketReceiver(this); @@ -85,13 +106,13 @@ public void _onConnectStatus(ConnectState state) { Dbg.ERROR_MSG(string.Format("NetworkInterface::_onConnectStatus(), connect is error! ip: {0}:{1}, err: {2}", state.connectIP, state.connectPort, state.error)); } - - Event.fireOut("onConnectStatus", new object[]{success}); - + + 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; @@ -104,32 +125,66 @@ private static void connectCB(IAsyncResult ar) // Complete the connection. state.socket.EndConnect(ar); - Event.fireIn("_onConnectStatus", new object[]{state}); + Event.fireIn("_onConnectStatus", new object[] { state }); } catch (Exception e) { state.error = e.ToString(); - Event.fireIn("_onConnectStatus", new object[]{state}); + Event.fireIn("_onConnectStatus", new object[] { state }); } } - - public void connectTo(string ip, int port, ConnectCallback callback, object userData) + + /// + /// 在非主线程执行:连接服务器 + /// + 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(); + } + } + + /// + /// 在非主线程执行:连接服务器结果回调 + /// + 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("_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)) + 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); + IPHostEntry ipHost = Dns.GetHostEntry(ip); ip = ipHost.AddressList[0].ToString(); } // Security.PrefetchSocketPolicy(ip, 843); - _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + _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; @@ -137,65 +192,36 @@ public void connectTo(string ip, int port, ConnectCallback callback, object user 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; - } + var v = new AsyncConnectMethod(this._asyncConnect); + v.BeginInvoke(state, new AsyncCallback(this._asyncConnectCB), state); + } - public bool send(byte[] datas) - { - if(!valid()) + public bool send(MemoryStream stream) + { + if (!valid()) { - throw new ArgumentException ("invalid socket!"); + throw new ArgumentException("invalid socket!"); } - - if(_packetSender == null) + + 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(); - } + + return _packetSender.send(stream); + } + + public void process() + { + if (!valid()) + return; + + if (_packetReceiver != null) + _packetReceiver.process(); + } } -} +} diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/NetworkInterface.cs.meta b/Assets/kbengine_unity3d_plugins-0.9.7/NetworkInterface.cs.meta similarity index 76% rename from Assets/kbengine_unity3d_plugins-0.8.10/NetworkInterface.cs.meta rename to Assets/kbengine_unity3d_plugins-0.9.7/NetworkInterface.cs.meta index fbde111..a70aef4 100644 --- a/Assets/kbengine_unity3d_plugins-0.8.10/NetworkInterface.cs.meta +++ b/Assets/kbengine_unity3d_plugins-0.9.7/NetworkInterface.cs.meta @@ -1,6 +1,6 @@ fileFormatVersion: 2 -guid: 4e0ececa87075d6489b767e8422ca7ae -timeCreated: 1467109502 +guid: 885f1cee1fb721f438e170c5b1c51c5f +timeCreated: 1482072541 licenseType: Free MonoImporter: serializedVersion: 2 diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/ObjectPool.cs b/Assets/kbengine_unity3d_plugins-0.9.7/ObjectPool.cs similarity index 100% rename from Assets/kbengine_unity3d_plugins-0.8.10/ObjectPool.cs rename to Assets/kbengine_unity3d_plugins-0.9.7/ObjectPool.cs diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/ObjectPool.cs.meta b/Assets/kbengine_unity3d_plugins-0.9.7/ObjectPool.cs.meta similarity index 76% rename from Assets/kbengine_unity3d_plugins-0.8.10/ObjectPool.cs.meta rename to Assets/kbengine_unity3d_plugins-0.9.7/ObjectPool.cs.meta index 56e6399..833f4be 100644 --- a/Assets/kbengine_unity3d_plugins-0.8.10/ObjectPool.cs.meta +++ b/Assets/kbengine_unity3d_plugins-0.9.7/ObjectPool.cs.meta @@ -1,6 +1,6 @@ fileFormatVersion: 2 -guid: 0b564dc91d03172458c396be263838f1 -timeCreated: 1467109502 +guid: ef2afa54175ad5549ba76e7c57e666d8 +timeCreated: 1482072542 licenseType: Free MonoImporter: serializedVersion: 2 diff --git a/Assets/kbengine_unity3d_plugins-0.9.7/PacketReceiver.cs b/Assets/kbengine_unity3d_plugins-0.9.7/PacketReceiver.cs new file mode 100644 index 0000000..e84df57 --- /dev/null +++ b/Assets/kbengine_unity3d_plugins-0.9.7/PacketReceiver.cs @@ -0,0 +1,176 @@ +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 PacketReceiver + { + public delegate void AsyncReceiveMethod(); + + private MessageReader messageReader = null; + private NetworkInterface _networkInterface = null; + + private byte[] _buffer; + + // socket向缓冲区写的起始位置 + int _wpos = 0; + + // 主线程读取数据的起始位置 + int _rpos = 0; + + public PacketReceiver(NetworkInterface networkInterface) + { + _init(networkInterface); + } + + ~PacketReceiver() + { + Dbg.DEBUG_MSG("PacketReceiver::~PacketReceiver(), destroyed!"); + } + + void _init(NetworkInterface networkInterface) + { + _networkInterface = networkInterface; + _buffer = new byte[KBEngineApp.app.getInitArgs().RECV_BUFFER_MAX]; + + messageReader = new MessageReader(); + } + + public NetworkInterface networkInterface() + { + return _networkInterface; + } + + public void process() + { + int t_wpos = Interlocked.Add(ref _wpos, 0); + + if (_rpos < t_wpos) + { + messageReader.process(_buffer, (UInt32)_rpos, (UInt32)(t_wpos - _rpos)); + Interlocked.Exchange(ref _rpos, t_wpos); + } + else if (t_wpos < _rpos) + { + messageReader.process(_buffer, (UInt32)_rpos, (UInt32)(_buffer.Length - _rpos)); + messageReader.process(_buffer, (UInt32)0, (UInt32)t_wpos); + Interlocked.Exchange(ref _rpos, t_wpos); + } + else + { + // 没有可读数据 + } + } + + int _free() + { + int t_rpos = Interlocked.Add(ref _rpos, 0); + + if (_wpos == _buffer.Length) + { + if (t_rpos == 0) + { + return 0; + } + + Interlocked.Exchange(ref _wpos, 0); + } + + if (t_rpos <= _wpos) + { + return _buffer.Length - _wpos; + } + + return t_rpos - _wpos - 1; + } + + public void startRecv() + { + + var v = new AsyncReceiveMethod(this._asyncReceive); + v.BeginInvoke(new AsyncCallback(_onRecv), null); + } + + private void _asyncReceive() + { + if (_networkInterface == null || !_networkInterface.valid()) + { + Dbg.WARNING_MSG("PacketReceiver::_asyncReceive(): network interface invalid!"); + return; + } + + var socket = _networkInterface.sock(); + + while (true) + { + // 必须有空间可写,否则我们阻塞在线程中直到有空间为止 + int first = 0; + int space = _free(); + + while (space == 0) + { + if (first > 0) + { + if (first > 1000) + { + Dbg.ERROR_MSG("PacketReceiver::_asyncReceive(): no space!"); + Event.fireIn("_closeNetwork", new object[] { _networkInterface }); + return; + } + + Dbg.WARNING_MSG("PacketReceiver::_asyncReceive(): waiting for space, Please adjust 'RECV_BUFFER_MAX'! retries=" + first); + System.Threading.Thread.Sleep(5); + } + + first += 1; + space = _free(); + } + + int bytesRead = 0; + try + { + bytesRead = socket.Receive(_buffer, _wpos, space, 0); + } + catch (SocketException se) + { + Dbg.ERROR_MSG(string.Format("PacketReceiver::_asyncReceive(): receive error, disconnect from '{0}'! error = '{1}'", socket.RemoteEndPoint, se)); + Event.fireIn("_closeNetwork", new object[] { _networkInterface }); + return; + } + + if (bytesRead > 0) + { + // 更新写位置 + Interlocked.Add(ref _wpos, bytesRead); + } + else + { + Dbg.WARNING_MSG(string.Format("PacketReceiver::_asyncReceive(): receive 0 bytes, disconnect from '{0}'!", socket.RemoteEndPoint)); + Event.fireIn("_closeNetwork", new object[] { _networkInterface }); + return; + } + } + } + + private void _onRecv(IAsyncResult ar) + { + AsyncResult result = (AsyncResult)ar; + AsyncReceiveMethod caller = (AsyncReceiveMethod)result.AsyncDelegate; + caller.EndInvoke(ar); + } + } +} diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/PacketReceiver.cs.meta b/Assets/kbengine_unity3d_plugins-0.9.7/PacketReceiver.cs.meta similarity index 76% rename from Assets/kbengine_unity3d_plugins-0.8.10/PacketReceiver.cs.meta rename to Assets/kbengine_unity3d_plugins-0.9.7/PacketReceiver.cs.meta index 0c0e732..f5c2479 100644 --- a/Assets/kbengine_unity3d_plugins-0.8.10/PacketReceiver.cs.meta +++ b/Assets/kbengine_unity3d_plugins-0.9.7/PacketReceiver.cs.meta @@ -1,6 +1,6 @@ fileFormatVersion: 2 -guid: 9a4206ba076e04040a841a518bf8cebd -timeCreated: 1467109502 +guid: da5480ff74efdc248bdf115cbfe03380 +timeCreated: 1482072542 licenseType: Free MonoImporter: serializedVersion: 2 diff --git a/Assets/kbengine_unity3d_plugins-0.9.7/PacketSender.cs b/Assets/kbengine_unity3d_plugins-0.9.7/PacketSender.cs new file mode 100644 index 0000000..455467e --- /dev/null +++ b/Assets/kbengine_unity3d_plugins-0.9.7/PacketSender.cs @@ -0,0 +1,174 @@ +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; // 发送完毕的数据位置 + int _sending = 0; + + 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 = 0; + } + + public NetworkInterface networkInterface() + { + return _networkInterface; + } + + public bool send(MemoryStream stream) + { + int dataLength = (int)stream.length(); + if (dataLength <= 0) + return true; + + if (0 == Interlocked.Add(ref _sending, 0)) + { + if (_wpos == _spos) + { + _wpos = 0; + _spos = 0; + } + } + + int t_spos = Interlocked.Add(ref _spos, 0); + 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); + } + + Interlocked.Add(ref _wpos, dataLength); + + if (Interlocked.CompareExchange(ref _sending, 1, 0) == 0) + { + _startSend(); + } + + return true; + } + + void _startSend() + { + // 由于socket用的是非阻塞式,因此在这里不能直接使用socket.send()方法 + // 必须放到另一个线程中去做 + _asyncSendMethod.BeginInvoke(_asyncCallback, null); + } + + void _asyncSend() + { + if (_networkInterface == null || !_networkInterface.valid()) + { + Dbg.WARNING_MSG("PacketSender::_asyncSend(): network interface invalid!"); + return; + } + + var socket = _networkInterface.sock(); + + while (true) + { + int sendSize = Interlocked.Add(ref _wpos, 0) - _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 }); + return; + } + + int spos = Interlocked.Add(ref _spos, bytesSent); + + // 所有数据发送完毕了 + if (spos == Interlocked.Add(ref _wpos, 0)) + { + Interlocked.Exchange(ref _sending, 0); + return; + } + } + } + + private static void _onSent(IAsyncResult ar) + { + AsyncResult result = (AsyncResult)ar; + AsyncSendMethod caller = (AsyncSendMethod)result.AsyncDelegate; + caller.EndInvoke(ar); + } + } +} diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/PacketSender.cs.meta b/Assets/kbengine_unity3d_plugins-0.9.7/PacketSender.cs.meta similarity index 76% rename from Assets/kbengine_unity3d_plugins-0.8.10/PacketSender.cs.meta rename to Assets/kbengine_unity3d_plugins-0.9.7/PacketSender.cs.meta index d29b4a9..9adfd76 100644 --- a/Assets/kbengine_unity3d_plugins-0.8.10/PacketSender.cs.meta +++ b/Assets/kbengine_unity3d_plugins-0.9.7/PacketSender.cs.meta @@ -1,6 +1,6 @@ fileFormatVersion: 2 -guid: cd740bfc75138b44787a4577d32421b0 -timeCreated: 1467109503 +guid: c54a650c29475ba47895d96570872903 +timeCreated: 1482072542 licenseType: Free MonoImporter: serializedVersion: 2 diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/PersistentInofs.cs b/Assets/kbengine_unity3d_plugins-0.9.7/PersistentInfos.cs similarity index 97% rename from Assets/kbengine_unity3d_plugins-0.8.10/PersistentInofs.cs rename to Assets/kbengine_unity3d_plugins-0.9.7/PersistentInfos.cs index 65aa05f..71b14f5 100644 --- a/Assets/kbengine_unity3d_plugins-0.8.10/PersistentInofs.cs +++ b/Assets/kbengine_unity3d_plugins-0.9.7/PersistentInfos.cs @@ -10,13 +10,13 @@ namespace KBEngine /* 持久化引擎协议,在检测到协议版本发生改变时会清理协议 */ - public class PersistentInofs + public class PersistentInfos { string _persistentDataPath = ""; bool _isGood = false; string _digest = ""; - public PersistentInofs(string path) + public PersistentInfos(string path) { _persistentDataPath = path; installEvents(); @@ -75,7 +75,7 @@ public bool loadAll() } catch(Exception e) { - Dbg.ERROR_MSG("PersistentInofs::loadAll(): is error(" + e.ToString() + ")!"); + Dbg.ERROR_MSG("PersistentInfos::loadAll(): is error(" + e.ToString() + ")!"); clearMessageFiles(); return false; } diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/PersistentInofs.cs.meta b/Assets/kbengine_unity3d_plugins-0.9.7/PersistentInfos.cs.meta similarity index 76% rename from Assets/kbengine_unity3d_plugins-0.8.10/PersistentInofs.cs.meta rename to Assets/kbengine_unity3d_plugins-0.9.7/PersistentInfos.cs.meta index 37f8c60..1d33424 100644 --- a/Assets/kbengine_unity3d_plugins-0.8.10/PersistentInofs.cs.meta +++ b/Assets/kbengine_unity3d_plugins-0.9.7/PersistentInfos.cs.meta @@ -1,6 +1,6 @@ fileFormatVersion: 2 -guid: edbd7443209ecc64caa5100f6da0df66 -timeCreated: 1467109503 +guid: 047d27d0b15ce7c4482251bae01082a6 +timeCreated: 1482072541 licenseType: Free MonoImporter: serializedVersion: 2 diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/Profile.cs b/Assets/kbengine_unity3d_plugins-0.9.7/Profile.cs similarity index 100% rename from Assets/kbengine_unity3d_plugins-0.8.10/Profile.cs rename to Assets/kbengine_unity3d_plugins-0.9.7/Profile.cs diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/Profile.cs.meta b/Assets/kbengine_unity3d_plugins-0.9.7/Profile.cs.meta similarity index 76% rename from Assets/kbengine_unity3d_plugins-0.8.10/Profile.cs.meta rename to Assets/kbengine_unity3d_plugins-0.9.7/Profile.cs.meta index 4062849..86de51b 100644 --- a/Assets/kbengine_unity3d_plugins-0.8.10/Profile.cs.meta +++ b/Assets/kbengine_unity3d_plugins-0.9.7/Profile.cs.meta @@ -1,6 +1,6 @@ fileFormatVersion: 2 -guid: 0e45016c06d7c4547b26fc5d39d41f90 -timeCreated: 1467109502 +guid: 0fcc267569bdbd741b50b5fb2bc35dc2 +timeCreated: 1482072541 licenseType: Free MonoImporter: serializedVersion: 2 diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/Property.cs b/Assets/kbengine_unity3d_plugins-0.9.7/Property.cs similarity index 79% rename from Assets/kbengine_unity3d_plugins-0.8.10/Property.cs rename to Assets/kbengine_unity3d_plugins-0.9.7/Property.cs index d42eaae..b79d669 100644 --- a/Assets/kbengine_unity3d_plugins-0.8.10/Property.cs +++ b/Assets/kbengine_unity3d_plugins-0.9.7/Property.cs @@ -43,6 +43,18 @@ public bool isBase() return properFlags == (UInt32)EntityDataFlags.ED_FLAG_BASE_AND_CLIENT || properFlags == (UInt32)EntityDataFlags.ED_FLAG_BASE; } + + public bool isOwnerOnly() + { + return properFlags == (UInt32)EntityDataFlags.ED_FLAG_CELL_PUBLIC_AND_OWN || + properFlags == (UInt32)EntityDataFlags.ED_FLAG_OWN_CLIENT; + } + + public bool isOtherOnly() + { + return properFlags == (UInt32)EntityDataFlags.ED_FLAG_OTHER_CLIENTS || + properFlags == (UInt32)EntityDataFlags.ED_FLAG_OTHER_CLIENTS; + } } } diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/Property.cs.meta b/Assets/kbengine_unity3d_plugins-0.9.7/Property.cs.meta similarity index 76% rename from Assets/kbengine_unity3d_plugins-0.8.10/Property.cs.meta rename to Assets/kbengine_unity3d_plugins-0.9.7/Property.cs.meta index 2eefc3d..b535923 100644 --- a/Assets/kbengine_unity3d_plugins-0.8.10/Property.cs.meta +++ b/Assets/kbengine_unity3d_plugins-0.9.7/Property.cs.meta @@ -1,6 +1,6 @@ fileFormatVersion: 2 -guid: 76717f16453da554586b3662d50072d7 -timeCreated: 1467109502 +guid: 3296f2ed24cf9fd4b81903f8cb23e3f7 +timeCreated: 1482072541 licenseType: Free MonoImporter: serializedVersion: 2 diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/README.md b/Assets/kbengine_unity3d_plugins-0.9.7/README.md similarity index 100% rename from Assets/kbengine_unity3d_plugins-0.8.10/README.md rename to Assets/kbengine_unity3d_plugins-0.9.7/README.md diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/README.md.meta b/Assets/kbengine_unity3d_plugins-0.9.7/README.md.meta similarity index 64% rename from Assets/kbengine_unity3d_plugins-0.8.10/README.md.meta rename to Assets/kbengine_unity3d_plugins-0.9.7/README.md.meta index c234c13..0cb5a1f 100644 --- a/Assets/kbengine_unity3d_plugins-0.8.10/README.md.meta +++ b/Assets/kbengine_unity3d_plugins-0.9.7/README.md.meta @@ -1,6 +1,6 @@ fileFormatVersion: 2 -guid: 355c8919d6c94f0479fe55d974dcdf0a -timeCreated: 1467109502 +guid: 3764e9cff8c5eb3458126fbe8e6be0a9 +timeCreated: 1482072541 licenseType: Free DefaultImporter: userData: diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/ScriptModule.cs b/Assets/kbengine_unity3d_plugins-0.9.7/ScriptModule.cs similarity index 100% rename from Assets/kbengine_unity3d_plugins-0.8.10/ScriptModule.cs rename to Assets/kbengine_unity3d_plugins-0.9.7/ScriptModule.cs diff --git a/Assets/kbengine_unity3d_plugins-0.8.10/ScriptModule.cs.meta b/Assets/kbengine_unity3d_plugins-0.9.7/ScriptModule.cs.meta similarity index 76% rename from Assets/kbengine_unity3d_plugins-0.8.10/ScriptModule.cs.meta rename to Assets/kbengine_unity3d_plugins-0.9.7/ScriptModule.cs.meta index 2ddcdb7..7875e6c 100644 --- a/Assets/kbengine_unity3d_plugins-0.8.10/ScriptModule.cs.meta +++ b/Assets/kbengine_unity3d_plugins-0.9.7/ScriptModule.cs.meta @@ -1,6 +1,6 @@ fileFormatVersion: 2 -guid: 67f0df24eb2e2334eb4f752f9a57da10 -timeCreated: 1467109502 +guid: 3b45215f090257c468c824107ef6b174 +timeCreated: 1482072541 licenseType: Free MonoImporter: serializedVersion: 2 diff --git a/Assets/script/script_kbe/interface/GameObject.cs b/Assets/script/script_kbe/interface/GameObject.cs index af40a0a949a9bf34c0f27eed3cbd28ffb0701389..54c944dd0158b8da29b79eca85e3e9f06c458e4a 100644 GIT binary patch delta 84 zcmccO^ulR_9w)OtgZ|_|0rkxxoJGuV&KsTy+%V4M2~sSR{lxh;Z;{x>2ou~qL23t_ N160AaxkBa+3jhvY7+?SZ delta 67 zcmaFibj4|d9_M5QA+^mBoJGta#^hDpJezo1xWViNQY@3LB=|NTkl4luR<%HC2beK= MffU!~9+^8V0QOrJC;$Ke