private void HandleRecvQueue() { recvQueue.Switch(); while (!recvQueue.Empty()) { var recvBufferRaw = recvQueue.Pop(); int ret = kcpObject.Input(recvBufferRaw, recvBufferRaw.Length); //收到的不是一个正确的KCP包 if (ret < 0) { string str = System.Text.Encoding.UTF8.GetString(recvBufferRaw, 0, recvBufferRaw.Length); DLog.LogFormat("收到了错误的kcp包: {0}", str); return; } needKcpUpdateFlag = true; for (int size = kcpObject.PeekSize(); size > 0; size = kcpObject.PeekSize()) { var recvBuffer = new byte[size]; int treclen = kcpObject.Recv(recvBuffer, recvBuffer.Length); if (treclen > 0) { PushRecData(recvBuffer, treclen); } } } }
private void DoReceiveInMain() { m_RecvBufQueue.Switch(); while (!m_RecvBufQueue.Empty()) { var recvBufferRaw = m_RecvBufQueue.Pop(); int ret = m_Kcp.Input(recvBufferRaw, recvBufferRaw.Length); //收到的不是一个正确的KCP包 if (ret < 0) { Debuger.LogError("收到不正确的KCP包!Ret:{0}", ret); return; } m_NeedKcpUpdateFlag = true; for (int size = m_Kcp.PeekSize(); size > 0; size = m_Kcp.PeekSize()) { var recvBuffer = new byte[size]; if (m_Kcp.Recv(recvBuffer) > 0) { NetMessage msg = new NetMessage(); msg.Deserialize(recvBuffer, size); m_listener.OnReceive(this, msg); } } } }
//void process_connect_packet() //{ // mRecvQueue.Switch(); // if (!mRecvQueue.Empty()) // { // var buf = mRecvQueue.Pop(); // //UInt32 conv = 1; // //KCP.ikcp_decode32u(buf, 0, ref conv); // //if (conv <= 0) // // throw new Exception("inlvaid connect back packet"); // //init_kcp(conv); // //mInConnectStage = false; // //mConnectSucceed = true; // //m_connectStatusCallback(NetworkState.Connected); // //evHandler(cliEvent.Connected, null, null); // } //} void process_recv_queue(UInt32 current) { mRecvQueue.Switch(); while (!mRecvQueue.Empty()) { var buf = mRecvQueue.Pop(); int input= mKcp.Input(buf); mNeedUpdateFlag = true; //mKcp.Update(current); //Debug.Log("process_recv_queue :" + buf.Length + " input:"+ input+" PeekSize:" + mKcp.PeekSize()); for (var size = mKcp.PeekSize(); size > 0; size = mKcp.PeekSize()) { var buffer = new byte[size]; if (mKcp.Recv(buffer) > 0) { int offset = 0; //Debug.Log("m_byteCallBack ---------"); m_byteCallBack(buffer, ref offset, buffer.Length); } } } }
private void DoReceiveInMain() { recvBufQueue.Switch(); while (!recvBufQueue.Empty()) { var recvBufferRaw = recvBufQueue.Pop(); int ret = kcp.Input(recvBufferRaw, recvBufferRaw.Length); if (ret < 0) { Debuger.LogError("收到不正确的KCP包!Ret:{0}", ret); return; } needKcpUpdateFlag = true; for (int size = kcp.PeekSize(); size > 0; size = kcp.PeekSize()) { var recvBuffer = new byte[size]; if (kcp.Recv(recvBuffer) > 0) { listener.OnReceive(this, recvBuffer, size); } } } }
void HandleRecvQueue() { if (_kcpQueue.Count <= 0) { return; } List <byte[]> kcpData = new List <byte[]>(); lock (_kcpQueue) { while (_kcpQueue.Count > 0) { kcpData.Add(_kcpQueue.Dequeue()); } } foreach (byte[] data in kcpData) { int ret = _kcp.Input(data); // invalid kcp package if (ret < 0) { continue; } for (int size = _kcp.PeekSize(); size > 0; size = _kcp.PeekSize()) { var recvBuffer = new byte[size]; if (_kcp.Recv(recvBuffer) > 0) { _queue.Enqueue(recvBuffer); } } } }
private void Update() { while (true) { if (_receiveMeesages.Count > 0) { var buf = _receiveMeesages.Dequeue(); _kcp.Input(buf); // mNeedUpdateFlag = true; for (var size = _kcp.PeekSize(); size > 0; size = _kcp.PeekSize()) { var buffer = new byte[size]; if (_kcp.Recv(buffer) > 0) { _reveiveHandler(buffer); } } } _kcp.Update(iclock()); Thread.Sleep(10); } }
private void DoReceiveInMain() { recvBufQueue.Switch(); while (!recvBufQueue.Empty()) { var recvBufferRaw = recvBufQueue.Pop(); int ret = kcp.Input(recvBufferRaw, recvBufferRaw.Length); if (ret < 0) { Debuger.LogError("收到不正确的KCP包!Ret:{0}", ret); return; } needKcpUpdateFlag = true; for (int size = kcp.PeekSize(); size > 0; size = kcp.PeekSize()) { var recvBuffer = new byte[size]; if (kcp.Recv(recvBuffer) > 0) { if (listener != null) { FSPDataCToS data = ProtoBuffUtility.Deserialize <FSPDataCToS>(recvBuffer); listener(data); } else { Debuger.LogError("找不到接收者!"); } } } } }
public int ReceiveData(byte[] ar) { if (needSwitchRecvQueue) { needSwitchRecvQueue = false; mRecvQueue.Switch(); while (!mRecvQueue.Empty()) { var buf = mRecvQueue.Pop(); mKcp.Input(buf); mNeedUpdateFlag = true; } } for (var size = mKcp.PeekSize(); size > 0; size = mKcp.PeekSize()) { int recvSize = mKcp.Recv(ar); if (recvSize > 0) { return(recvSize); } } needSwitchRecvQueue = true; return(0); }
private void DoReceiveInMain() { recvBuffQueue.Switch(); while (!recvBuffQueue.Empty()) { var recvBufferRaw = recvBuffQueue.Pop(); int ret = kcp.Input(recvBufferRaw, recvBufferRaw.Length); if (ret < 0) { Debuger.LogError("收到不正确的KCP包, Ret:{0}", ret); return; } needKcpUpdateFlag = true; for (int size = kcp.PeekSize(); size > 0; size = kcp.PeekSize()) { var recvBuffer = new byte[size]; if (kcp.Recv(recvBuffer) > 0) { lastRecvTimestamp = (uint)TimeUtility.GetTotalMillisecondsSince1970(); var data = ProtoBuffUtility.Deserialize <FSPDataSToC>(recvBuffer); recvListener?.Invoke(data.frame); } } } }
private void UpdateKcpInput() { while (udpClient.Available > 0) { byte[] buff = udpClient.Receive(ref remoteIpEndpoint); kcp.Input(buff); } }
void process_recv_queue() { m_RecvQueue.Switch(); while (!m_RecvQueue.Empty()) { if (m_Kcp == null) {//退出 return; } var buf = m_RecvQueue.Pop(); if (this.Status == ClientSessionStatus.InConnect) { //服务端将返回和握手请求相同的响应数据 uint _index = 0, _key = 0; if (HandshakeUtility.IsHandshakeDataRight(buf, 0, buf.Length, out _index, out _key)) { if (_index == m_NetIndex && _key == m_Key) { #if DEBUG Console.WriteLine("连接握手成功"); #endif this.Status = ClientSessionStatus.Connected; var e = Event; if (e != null) { m_LastSendTimestamp = UnityEngine.Time.time; m_LastRecvTimestamp = UnityEngine.Time.time; e(UdpClientEvents.Connected, null); } continue; } } } m_Kcp.Input(buf); m_NeedUpdateFlag = true; for (var size = m_Kcp.PeekSize(); size > 0; size = m_Kcp.PeekSize()) { var buffer = new byte[size]; if (m_Kcp.Recv(buffer) > 0) { m_LastRecvTimestamp = UnityEngine.Time.time; var e = Event; if (e != null) { e(UdpClientEvents.Recv, buffer); } } } } }
private void DoReceiveInMain() { //主线程 m_RecvBufQueue.Switch(); while (!m_RecvBufQueue.Empty()) { var recvBufferRaw = m_RecvBufQueue.Pop(); int ret = m_Kcp.Input(recvBufferRaw, recvBufferRaw.Length); //收到的不是一个正确的KCP包 if (ret < 0) { Debuger.LogError("收到不正确的KCP包!Ret:{0}", ret); return; } m_NeedKcpUpdateFlag = true; for (int size = m_Kcp.PeekSize(); size > 0; size = m_Kcp.PeekSize()) { var recvBuffer = new byte[size]; if (m_Kcp.Recv(recvBuffer) > 0) { NetMessage msg = new NetMessage(); msg.Deserialize(recvBuffer, size); if (msg.head.sid == 0) { var errmsg = PBSerializer.NDeserialize <NetErrorMessage>(msg.content); this.LogWarning("服务器返回错误:{0},{1}", errmsg.code, errmsg.info); HandleServerError(errmsg.code); onServerError.InvokeSafe(this, errmsg.code, errmsg.info); } else { //更新SessionId if (Id != msg.head.sid) { Id = msg.head.sid; LOG_TAG = "KcpConnection[" + Id + "," + m_localPort + "]"; this.LogWarning("SessionId发生变化:{0}", Id); } onReceive.InvokeSafe(msg); } } } } }
public void Input(byte[] rcvBuf) { _kcp.Input(rcvBuf); for (var size = _kcp.PeekSize(); size > 0; size = _kcp.PeekSize()) { byte[] buf = new byte[size]; if (_kcp.Recv(buf) > 0) { //Log4U.LogDebug("ASynServerKcp:Input Message receive data=", Encoding.ASCII.GetString(buf)); _recHandler(buf, this); } } }
/// <summary> /// 和Update同一个线程调用 /// </summary> internal void process_recv_queue(byte[] datas) { #if DEV IRQLog.AppLog.Log(this.m_netIndex.ToString() + ",接收1"); #endif if (m_Kcp != null && datas != null) { m_Kcp.Input(datas); m_NeedUpdateFlag = true; for (var size = m_Kcp.PeekSize(); size > 0; size = m_Kcp.PeekSize()) { byte[] buffer; buffer = (UdpLibConfig.UseBytePool ? m_KCPServer.BytePool.Rent(size) : new byte[size]); try { if (m_Kcp.Recv(buffer) > 0) { m_LastRecvTimestamp = m_KCPServer.m_watch.Elapsed; uint key = 0; KCP.ikcp_decode32u(buffer, 0, ref key); if (m_KCPServer.IsClientKeyCorrect(this.m_netIndex, (int)key) == false) { #if DEBUG Console.WriteLine("index:{0} key 不对", this.m_netIndex); #endif m_KCPServer.BytePool.Return(buffer, true); DisposeReason = ClientSessionDisposeReason.IndexKeyError; //key不对 Dispose(); return; } #if DEV IRQLog.AppLog.Log(this.m_netIndex.ToString() + ",接收2"); #endif m_KCPServer.OnRecvData(this, buffer, 0, size); } } finally { if (UdpLibConfig.UseBytePool) { m_KCPServer.BytePool.Return(buffer, true); } } } } }
public void OnUpdate(object obj) { while (true) { Thread.Sleep(1); if (KcpHelper.currentMS() >= this.n_time) { while (this.recv_queue.Count > 0) { this.recv_queue.TryDequeue(out var item); // Console.WriteLine("xxxxxxxxxxxx>>"+item.Length); var seg = item.GetBinaryArray(); kcp.Input(seg.Array, 0, seg.Count, true, false); var length = this.kcp.PeekSize(); if (length < 0) { continue; } // Console.WriteLine(">>>>>>"+length); var bytesLength = this.kcp.Recv(buffer, 0, length); Memory <byte> memory = new Memory <byte>(buffer, 0, bytesLength); if (this.onRcvEvent != null) { this.onRcvEvent(memory); } } while (this.send_queue.Count > 0) { if (this.kcp.WaitSnd > this.kcp.SndWnd * 2) { break; } this.send_queue.TryDequeue(out var item); this.kcp.Send(item); } this.kcp.Update(); this.n_time = this.kcp.Check(); } } }
private void HandleRecvQueue() { recvQueue.Switch(); while (!recvQueue.Empty()) { var recvobject = recvQueue.Pop(); int ret = kcpObject.Input(recvobject.bytes, recvobject.length); cacheBytesQue.Enqueue(recvobject); if (ret < 0) { //收到的不是一个正确的KCP包 if (IsShowDebugLog) { DLog.Log("Error kcp package."); } return; } needKcpUpdateFlag = true; for (int size = kcpObject.PeekSize(); size > 0; size = kcpObject.PeekSize()) { if (size > 1048576) { DLog.LogErrorFormat("The size is too long.size = {0}", size); } if (kcpRecvBuffer.Length < size) { int tnewlen = size + kcpRecvBuffer.Length; kcpRecvBuffer = new byte[tnewlen]; } else { kcpRecvBuffer.Initialize(); } int treclen = kcpObject.Recv(kcpRecvBuffer, size); if (treclen > 0) { PopRecData(kcpRecvBuffer, treclen); } } } }
private static void ReceiveCallback(IAsyncResult ar) { try { var data = Client.EndReceive(ar, ref Remote); if (data != null) { Kcp.Input(data); } Receive(); } catch (SocketException) { WillDisconnect = true; } }
public void processRecvQueue(SocketAsyncEventArgs e) { byte[] realData = new byte[e.BytesTransferred]; Buffer.BlockCopy(e.Buffer, 0, realData, 0, e.BytesTransferred); mKcp.Input(realData); mNeedUpdateFlag = true; for (var size = mKcp.PeekSize(); size > 0; size = mKcp.PeekSize()) { byte[] buffer = new byte[size]; if (mKcp.Recv(buffer) > 0) { mLastRecvTime = Helper.iclock(); RecvDataHandler(this, buffer); } } }
/// <summary> /// 将数据Input进Kcp待处理队列 /// </summary> /// <param name="recvBuf"> 接收到的数据</param> public void Input(byte[] recvBuf) { //刷新超时时间 timer = SystemTime.Clock(); //将收到的数据打入kcp中处理 kcp.Input(recvBuf); for (var size = kcp.PeekSize(); size > 0; size = kcp.PeekSize()) { var buffer = new byte[size]; if (kcp.Recv(buffer) > 0) { //从kcp缓存池中取出排列处理后的数据 OnRecive.Invoke(conv, buffer); } } }
/// <summary> /// 执行接收队列 /// </summary> /// <returns>是否需要更新</returns> private bool ProcessReceiveQueue() { receiveQueue.Switch(); var needUpdate = false; while (!receiveQueue.Empty()) { kcp.Input(receiveQueue.Pop()); needUpdate = true; for (var size = kcp.PeekSize(); size > 0; size = kcp.PeekSize()) { var buffer = new byte[size]; if (kcp.Recv(buffer) > 0) { Trigger(SocketEvents.Message, buffer); } } } return(needUpdate); }
void process_recv_queue() { mRecvQueue.Switch(); while (!mRecvQueue.Empty()) { var buf = mRecvQueue.Pop(); mKcp.Input(buf); mNeedUpdateFlag = true; for (var size = mKcp.PeekSize(); size > 0; size = mKcp.PeekSize()) { var buffer = new byte[size]; if (mKcp.Recv(buffer) > 0) { evHandler(buffer); } } } }
/// <summary> /// 执行接收队列 /// </summary> /// <returns>是否需要更新</returns> private bool ProcessReceiveQueue() { receiveQueue.Switch(); var needUpdate = false; while (!receiveQueue.Empty()) { kcp.Input(receiveQueue.Pop()); needUpdate = true; for (var size = kcp.PeekSize(); size > 0; size = kcp.PeekSize()) { var buffer = new byte[size]; if (kcp.Recv(buffer) > 0) { callback.Invoke(buffer); kcp.Send(buffer); } } } return(needUpdate); }
void process_recv_queue() { mRecvQueue.Switch(); while (!mRecvQueue.Empty()) { var buf = mRecvQueue.Pop(); mKcp.Input(buf); mNeedUpdateFlag = true; for (var size = mKcp.PeekSize(); size > 0; size = mKcp.PeekSize()) { var buffer = new byte[size]; if (mKcp.Recv(buffer) > 0) { int offset = 0; m_byteCallBack(buffer, ref offset, buffer.Length); } } } }
protected override void OnConnectionPacketReceived(ByteBuffer recv_buffer) { _kcp.Input(recv_buffer.data, recv_buffer.readableBytes); var length = _kcp.PeekSize(); if (length > 0) { var buffer = ByteBufferPooledAllocator.Default.Alloc(length); try { var size = _kcp.Recv(buffer.data); //接受数据 buffer._SetPosition(size); // 不想改kcp的实现细节,所以这里简单地强制改buffer长度标记 base.OnConnectionPacketReceived(buffer); } finally { buffer.Release(); } } }
private void ReceiveAsyn(IAsyncResult arg) { //byte[] rcvBuf = _receiveEP == null ? _socket.Receive(ref _receiveEP) : _socket.EndReceive(arg, ref _receiveEP); byte[] rcvBuf = _socket.EndReceive(arg, ref _listenEP); if (rcvBuf != null) { //Log4U.LogDebug("ASynKcpUdpClientSocket:ReceiveAsyn Message receive from ", _listenEP.ToString()); _kcp.Input(rcvBuf); for (var size = _kcp.PeekSize(); size > 0; size = _kcp.PeekSize()) { byte[] buf = new byte[size]; if (_kcp.Recv(buf) > 0) { //Log4U.LogDebug("ASynKcpUdpClientSocket:ReceiveAsyn Message receive data=", Encoding.ASCII.GetString(buf)); _recHandler(buf); } } } if (_socket != null) { _socket.BeginReceive(ReceiveAsyn, this); } }
private void ProcessRecvQueue() { while (!recvQueue.IsEmpty) { byte[] data = null; bool isSuccess = recvQueue.TryDequeue(out data); if (!isSuccess || data == null) { continue; } mKcp.Input(data); mNeedUpdateFlag = true; for (var size = mKcp.PeekSize(); size > 0; size = mKcp.PeekSize()) { var packet = new byte[size]; if (mKcp.Recv(packet) > 0) { RecvDataHandler(packet); } } } }
private void DoReceiveInMain() { m_RecvBufQueue.Switch(); while (!m_RecvBufQueue.Empty()) { var recvBufferRaw = m_RecvBufQueue.Pop(); int ret = m_Kcp.Input(recvBufferRaw, recvBufferRaw.Length); //收到的不是一个正确的KCP包 if (ret < 0) { Debuger.LogError("收到不正确的KCP包, Ret:{0}", ret); return; } m_NeedKcpUpdateFlag = true; for (int size = m_Kcp.PeekSize(); size > 0; size = m_Kcp.PeekSize()) { var recvBuffer = new byte[size]; if (m_Kcp.Recv(recvBuffer) > 0) { m_lastRecvTimestamp = (uint)TimeUtils.GetTotalMillisecondsSince1970(); var data = PBSerializer.NDeserialize <FSPDataS2C>(recvBuffer); if (m_RecvListener != null) { for (int i = 0; i < data.frames.Count; i++) { m_RecvListener(data.frames[i]); } } } } } }
void process_recv_queue() { m_RecvQueue.Switch(); while (!m_RecvQueue.Empty()) { if (m_Kcp == null) {//退出 return; } var buf = m_RecvQueue.Pop(); m_Kcp.Input(buf); m_NeedUpdateFlag = true; int size = m_Kcp.PeekSize(); //UnityEngine.Main.Debug("size:" + size); for (; size > 0; size = m_Kcp.PeekSize()) { var buffer = new byte[size]; if (m_Kcp.Recv(buffer) > 0) { Recv(buffer); } } } }
private void DoReceiveInMain() { m_RecvBufQueue.Switch(); while (!m_RecvBufQueue.Empty()) { var recvBufferRaw = m_RecvBufQueue.Pop(); int ret = m_Kcp.Input(recvBufferRaw, recvBufferRaw.Length); //收到的不是一个正确的KCP包 if (ret < 0) { Debuger.LogError("收到不正确的KCP包!Ret:{0}", ret); return; } m_NeedKcpUpdateFlag = true; for (int size = m_Kcp.PeekSize(); size > 0; size = m_Kcp.PeekSize()) { var recvBuffer = new byte[size]; if (m_Kcp.Recv(recvBuffer) > 0) { if (m_listener != null) { FSPDataC2S data = PBSerializer.NDeserialize <FSPDataC2S>(recvBuffer); m_listener(data); } else { Debuger.LogError("找不到接收者!"); } } } } }
void process_recv_queue() { mRecvQueue.Switch(); while (!mRecvQueue.Empty()) { var buf = mRecvQueue.Pop(); mKcp.Input(buf); mNeedUpdateFlag = true; for (var size = mKcp.PeekSize(); size > 0; size = mKcp.PeekSize()) { var buffer = new byte[size]; if (mKcp.Recv(buffer) > 0) { //evHandler(buffer); //这里开始解包 try { //解析并派发 ByteArray ba = new ByteArray(); ba.WriteALLBytes(buffer); ProtocolRequestBase msg = Analysis(ba); ProtocolAnalysisService.AnalysisAndDispatchMessage(this, msg); } catch (Exception e) { Debug.LogError("AnalysisAndDispatchMessage :\nException: " + e.ToString()); } } } } }