/// <summary> /// 发送消息基本方法 /// </summary> /// <param name="_protocalType"></param> /// <param name="_data"></param> internal void SendMsgBase(byte[] _data) { if (clientSocket == null || !clientSocket.Connected) { SGNFDebug.Log("InfoServer didn't connected at all!"); return; } byte[] data = new byte[4 + _data.Length]; SocketUtil.IntToBytes(_data.Length).CopyTo(data, 0); _data.CopyTo(data, 4); SGNFDebug.HEXLog("send", data, data.Length); clientSocket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(_onSendMsg), clientSocket); }
/// <summary> /// 添加缓存数据 /// </summary> /// <param name="_data"></param> /// <param name="_dataLen"></param> public void AddBuffer(byte[] _data, int _dataLen) { if (_dataLen > _buff.Length - _curBuffPosition)//超过当前缓存 { byte[] _tmpBuff = new byte[_curBuffPosition + _dataLen]; Array.Copy(_buff, 0, _tmpBuff, 0, _curBuffPosition); Array.Copy(_data, 0, _tmpBuff, _curBuffPosition, _dataLen); _buff = _tmpBuff; _tmpBuff = null; } else { Array.Copy(_data, 0, _buff, _curBuffPosition, _dataLen); } _curBuffPosition += _dataLen;//修改当前数据标记 SGNFDebug.HEXLog("current buffer", _buff, _buff.Length); }
/// <summary> /// 接受网络数据,将接受到的放入消息队列 /// </summary> private void _onReceiveSocket() { while (true) { if (!clientSocket.Connected) { _isConnected = false; break; } try { int receiveLength = clientSocket.Receive(_tmpReceiveBuff); if (receiveLength > 0) { //将收到的数据添加到缓存器中 _databuffer.AddBuffer(_tmpReceiveBuff, receiveLength); SGNFDebug.HEXLog("rcv", _tmpReceiveBuff, receiveLength); //取出一条完整数据 while (_databuffer.GetData(out _socketData)) { ISSocketModel DeData = SocketUtil.ISDeSerial(_socketData); //如果数据属于内部协议 if (Enum.IsDefined(typeof(SocketUtil.InternalCommand), DeData.Command)) { if (DeData.Command == (int)SocketUtil.InternalCommand.NULL) { SGNFDebug.Log("Got NULL from server"); } if (DeData.Command == (int)SocketUtil.InternalCommand.PING) { //Client.RcvPingStr = DeData.Message[0]; } if (DeData.Command == (int)SocketUtil.InternalCommand.SSINFO) { int num = Convert.ToInt32(DeData.Message[0]); if (num > 0) { NetManager.Instance.allSSInfo.Clear(); for (int i = 0; i < num; i++) { NetManager.Instance.allSSInfo.Add(new SocketUtil.SSInfo() { Tag = DeData.Message[i * 3 + 1], IP = DeData.Message[i * 3 + 2], Port = Convert.ToInt32(DeData.Message[i * 3 + 3]), }); } } SGNFDebug.ListLog("Got " + num + " row of SS from server", NetManager.Instance.allSSInfo); } } else { //锁死消息中心消息队列,并添加数据 lock (MessageCenter.Instance.ISMessageDataQueue) { MessageCenter.Instance.ISMessageDataQueue.Enqueue(DeData); } } } } } catch (Exception e) { clientSocket.Disconnect(true); clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); SGNFDebug.ExceptionCaught(e); break; } } }
/// <summary> /// 接受网络数据,将接受到的放入消息队列 /// </summary> private void _onReceiveSocket() { while (true) { if (!clientSocket.Connected) { _isConnected = false; break; } try { int receiveLength = clientSocket.Receive(_tmpReceiveBuff); if (receiveLength > 0) { //将收到的数据添加到缓存器中 _databuffer.AddBuffer(_tmpReceiveBuff, receiveLength); SGNFDebug.HEXLog("rcv", _tmpReceiveBuff, receiveLength); //取出一条完整数据 while (_databuffer.GetData(out _socketData)) { SSSocketModel DeData = SocketUtil.SSDeSerial(_socketData); //如果数据属于内部协议 if (Enum.IsDefined(typeof(SocketUtil.InternalCommand), DeData.Command)) { if (DeData.Command == (int)SocketUtil.InternalCommand.NULL) { SGNFDebug.Log("Got NULL from server"); } if (DeData.Command == (int)SocketUtil.InternalCommand.PING) { //Client.RcvPingStr = DeData.Message[0]; } if (DeData.Command == (int)SocketUtil.InternalCommand.TICK) { if (DeData.Message.Count >= 1) { NetManager.Instance.tick = DeData.Message[0]; SGNFDebug.Log("Got Tick!Which is " + DeData.Message[0]); } else { SGNFDebug.ExceptionCaught(new Exception("Recieved nothing from TICK package!"));; } } } else { lock (SSRecorder.Instance.RecieveDataBuffer) { SSRecorder.Instance.RecieveDataBuffer.Add(DeData.CurrentTick, DeData); } //锁死消息中心消息队列,并添加数据 lock (MessageCenter.Instance.SSMessageDataQueue) { MessageCenter.Instance.SSMessageDataQueue.Enqueue(DeData); } } } } } catch (Exception e) { clientSocket.Disconnect(true); clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); SGNFDebug.ExceptionCaught(e); break; } } }