Beispiel #1
0
        /// <summary>
        /// 服务器端收包处理
        /// </summary>
        /// <param name="data"></param>
        /// <param name="OnHeart"></param>
        /// <param name="OnUnPackage"></param>
        /// <param name="onFile"></param>
        public void Pack(byte[] data, Action <DateTime> onHeart, Action <ISocketProtocal> onUnPackage, Action <byte[]> onFile)
        {
            lock (_locker)
            {
                _buffer.AddRange(data);

                while (_buffer.Count >= P_Head)
                {
                    var buffer = _buffer.ToArray();

                    var bodyLen = GetLength(buffer);

                    var type = GetType(buffer);

                    if (bodyLen == 0 && type == HolePunchingType.Heart) //空包认为是心跳包
                    {
                        var sm = new PSocketMsg()
                        {
                            BodyLength = bodyLen, Type = (byte)type
                        };
                        _buffer.Clear();
                        onHeart?.Invoke(DateTimeHelper.Now);
                    }
                    else if (buffer.Length >= P_Head + bodyLen)
                    {
                        var sm = new PSocketMsg()
                        {
                            BodyLength = bodyLen, Type = (byte)type, Content = GetContent(buffer, P_Head, (int)bodyLen)
                        };
                        _buffer.RemoveRange(0, (int)(P_Head + bodyLen));
                        bodyLen = 0;
                        onUnPackage?.Invoke(sm);
                    }
                    else
                    {
                        return;
                    }
                }
            }
        }
Beispiel #2
0
        public static PSocketMsg Parse(byte[] data, TcpP2pType type)
        {
            var msg = new PSocketMsg();

            if (data != null)
            {
                msg.BodyLength = data.Length;
            }
            else
            {
                msg.BodyLength = 0;
            }

            msg.Type = (byte)type;

            if (msg.BodyLength > 0)
            {
                msg.Content = data;
            }

            return(msg);
        }
Beispiel #3
0
 private void ReplyP2PResponse(IUserToken userToken)
 {
     ReplyBase(userToken, PSocketMsg.Parse(null, TcpP2pType.P2PResponse));
 }
Beispiel #4
0
 private void ReplyHeart(IUserToken userToken)
 {
     ReplyBase(userToken, PSocketMsg.Parse(null, TcpP2pType.Heart));
 }
Beispiel #5
0
 private void ReplyBase(IUserToken userToken, PSocketMsg msg)
 {
     base.SendAsync(userToken, msg.ToBytes());
 }
Beispiel #6
0
 public void SendMessage(string ipPort, byte[] msg)
 {
     base.SendAsync(ipPort, PSocketMsg.Parse(msg, TcpP2pType.Message).ToBytes());
 }
Beispiel #7
0
 private void ReplyP2PResponse(IUserToken userToken)
 {
     ReplyBase(userToken, PSocketMsg.Parse(null, HolePunchingType.P2PResponse));
 }
Beispiel #8
0
 private void ReplyHeart(IUserToken userToken)
 {
     ReplyBase(userToken, PSocketMsg.Parse(null, HolePunchingType.Heart));
 }
Beispiel #9
0
        private void SendBase(HolePunchingType type, byte[] content = null)
        {
            var qm = PSocketMsg.Parse(content, type);

            SendAsync(qm.ToBytes());
        }
Beispiel #10
0
        public void SendMessage(string msg)
        {
            var qm = PSocketMsg.Parse(Encoding.UTF8.GetBytes(msg), HolePunchingType.Message);

            SendTo(new System.Net.IPEndPoint(IPAddress.Parse(_peerBAddress.Item1), _peerBAddress.Item2), qm.ToBytes());
        }
Beispiel #11
0
        private void SendLogout()
        {
            var qm = PSocketMsg.Parse(null, HolePunchingType.P2PRequest);

            Send(qm.ToBytes());
        }
Beispiel #12
0
        private void SendLogin()
        {
            var qm = PSocketMsg.Parse(null, HolePunchingType.Login);

            Send(qm.ToBytes());
        }