private void AcknowledgeConnect(EndPoint endPoint, IReadStream readStream, PacketHead packetHead)
        {
            if (_connections.ContainsKey(endPoint))
            {
                Logger.Warn($"{endPoint}已经连接,无需重复连接!");
                return;
            }

            var service = PoolAllocator <SocketService> .GetObject();

            service.Connection.RemoteAddress = endPoint;
            service.Connection.LocalAddress  = _readSocket.LocalEndPoint as IPEndPoint;

            if (_connections.TryAdd(endPoint, service))
            {
                CreateAck(service);
                HandleConnect?.Invoke(service, readStream);

                SocketStatistics.LogUnreliableReceive();
                SocketStatistics.LogAcknowledgementReceive();
                SocketStatistics.LogDataBytesReceived(packetHead.TotalBytes);
                SocketStatistics.LogTotalBytesReceived(packetHead.Length);
                Logger.Info($"{endPoint}连接成功!");
            }
            else
            {
                Logger.Error($"添加连接{endPoint}失败!");
            }
        }
        private void Connected(IPeer peer, IReadStream readStream)
        {
            if (peer == null)
            {
                Logger.Info($"连接失败!");
                Callback?.ConnectEvent?.Invoke(false, readStream);
                return;
            }

            Logger.Info("连接到: " + peer.Connection.RemoteAddress);
            var ids = readStream.ShiftRight <string>();

            Logger.Info(ids.Length);
            if (ids != null && ids.Length != 0)
            {
                var rpcMethodIds = ids.Split(';');

                foreach (var item in rpcMethodIds)
                {
                    Ssui.Connections[item] = peer;
                }

                Logger.Info(ids);
            }

            Callback?.ConnectEvent?.Invoke(true, readStream);
        }
Example #3
0
 public void Handle(ulong remoteMessageId, IPeer player, IReadStream readStream)
 {
     if (player == null)
     {
         Logger.Error($"{nameof(player)} == null");
     }
 }
Example #4
0
            public void MergeFrom(IReadStream input)
            {
                uint tag;

                while ((tag = input.ReadTag()) != 0)
                {
                    switch (tag)
                    {
                    case 13:
                    {
                        key = input.ReadSFixed32();
                    }
                    break;

                    case 18:
                    {
                        if (value == null)
                        {
                            value = new Point3();
                        }
                        input.ReadMessage(value);
                    }
                    break;

                    default:
                        input.SkipLastField(tag);
                        break;
                    }
                }
            }
Example #5
0
        public void DecryptAES(IReadStream readStream, string sharedSecret)
        {
            if (!IsEncrypt)
            {
                return;
            }
            using (var aesAlg = new RijndaelManaged()) {
                var key = new Rfc2898DeriveBytes(sharedSecret, _salt);
                aesAlg.Padding = PaddingMode.None;

                aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);

                var iv    = readStream.ShiftRight(readStream.ShiftRight <ushort>());
                var bytes = new byte[iv.Count];
                Buffer.BlockCopy(iv.Buffer, iv.Offset, bytes, 0, iv.Count);
                aesAlg.IV = bytes;

                var length       = readStream.ShiftRight <ushort>();
                var byteFragment = readStream.ToByteFragment();

                using (var msDecrypt = new MemoryStream(byteFragment.Buffer, byteFragment.Offset, byteFragment.Count)) {
                    var decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
                    using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) {
                        csDecrypt.Read(byteFragment.Buffer, byteFragment.Offset, length);
                    }
                }
            }
        }
Example #6
0
        public void MergeFrom(IReadStream input)
        {
            uint tag;

            while ((tag = input.ReadTag()) != 0)
            {
                switch (tag)
                {
                case 13:
                {
                    x = input.ReadFloat();
                }
                break;

                case 21:
                {
                    y = input.ReadFloat();
                }
                break;

                case 29:
                {
                    z = input.ReadFloat();
                }
                break;

                default:
                    input.SkipLastField(tag);
                    break;
                }
            }
        }
Example #7
0
        private void AcknowledgeConnect(IPEndPoint endPoint, IReadStream readStream,
                                        PacketHead packetHead)
        {
            if (_connections.ContainsKey(endPoint))
            {
                Logger.Warn($"{endPoint}已经连接,无需重复连接!");
                return;
            }

            var service = new SocketService();

            service.Connection.RemoteAddress = endPoint;
            service.Connection.LocalAddress  = _writer.Client.LocalEndPoint as IPEndPoint;

            if (_connections.TryAdd(endPoint, service))
            {
                _autoResetEvent.Set();
                UdpKeepAlive.AddConnection(service,
                                           () => HandleReconnect?.Invoke(service, true),
                                           () => {
                    HandleReconnect?.Invoke(service, false);
                    OnDisconnect(service);
                });

                HandleConnect?.Invoke(service, readStream);

                Logger.Info($"{endPoint}连接成功!");
            }
            else
            {
                Logger.Error($"添加连接{endPoint}失败!");
            }
        }
Example #8
0
        public void MergeFrom(IReadStream input)
        {
            uint tag;

            while ((tag = input.ReadTag()) != 0)
            {
                switch (tag)
                {
                case 10:
                {
                    if (point3map == null)
                    {
                        point3map = new Dictionary <int, Point3>();
                    }
                    var entry = new Point3mapEntry();
                    input.ReadMessage(entry);
                    point3map[entry.key] = entry.value;
                }
                break;

                default:
                    input.SkipLastField(tag);
                    break;
                }
            }
        }
Example #9
0
        public static PacketHead Unpacking(IReadStream readStream)
        {
            var packetHead = new PacketHead();

            packetHead.FromBytes(readStream);
            return(packetHead);
        }
        private void ResponseConnect(EndPoint endPoint, IReadStream readStream, PacketHead packetHead)
        {
            if (_connections.TryGetValue(endPoint, out var socketService))
            {
                Logger.Warn($"已经存在连接{endPoint}!无需重复连接");
                Acknowledge(socketService, SendOption.Acknowledge | SendOption.Connect, packetHead);
                return;
            }

            var service = PoolAllocator <SocketService> .GetObject();

            service.Connection.RemoteAddress = endPoint;

            if (_connections.TryAdd(endPoint, service))
            {
                using (var writeStream = PoolAllocator <IWriteStream> .GetObject()) {
                    HandleAccept?.Invoke(service, readStream, writeStream);
                    var byteSegment = writeStream.ToByteFragment();
                    Acknowledge(service, SendOption.Acknowledge | SendOption.Connect, packetHead, writeStream);
                    SocketStatistics.LogAcknowledgementSend();
                    SocketStatistics.LogUnreliableReceive();
                    SocketStatistics.LogDataBytesReceived(byteSegment.Count);
                    SocketStatistics.LogTotalBytesReceived(writeStream.ToByteFragment().Count);
                }

                Logger.Info($"创建{endPoint}连接心跳包!");
            }
            else
            {
                Logger.Error($"添加连接{endPoint}失败!");
            }
        }
Example #11
0
        private void AcceptConnected(IUser peer, IReadStream readStream,
                                     IWriteStream writeStream)
        {
            var publicKey  = readStream.ShiftRight <string>();
            var key        = ID + "_" + SystemUtil.CreateRandomString(Ssfi.CryptoConfig.CryptonKeyLength);
            var encryptKey = Ssfi.Security.EncryptAesKey(key, publicKey);

            var securityComponent = SecurityComponent.GetObject();

            securityComponent.AesKey     = key;
            securityComponent.Encryptkey = encryptKey;

            peer.AddComponent <ISecurityComponent>(securityComponent);
            Logger.Debug($"添加{nameof(ISecurityComponent)}组件!");

            writeStream.ShiftRight((ushort)encryptKey.Length);
            Logger.Debug("EncryptKey Length:" + encryptKey.Length);
            writeStream.ShiftRight(encryptKey);
            writeStream.ShiftRight(string.Join(";", RpcMethodIds));

            var tmp = readStream.Clone();

            _serverSocket.AddEvent(() => {
                try {
                    _acceptAction.Invoke(peer, readStream);
                } catch (Exception e) {
                    Logger.Error(e);
                }
                tmp.Dispose();
            });
        }
Example #12
0
 public void Handle(ulong messageId, IPeer peer, IReadStream readStream)
 {
     if (peer == null)
     {
         Logger.Error($"{nameof(peer)} 为 null");
         return;
     }
 }
Example #13
0
 public void FromBytes(IReadStream readStream)
 {
     Length         = readStream.ShiftRight <ushort>();
     PacketId       = readStream.ShiftRight <ulong>();
     SendOption     = (SendOption)readStream.ShiftRight <byte>();
     TotalBytes     = readStream.ShiftRight <ushort>();
     FragmentId     = readStream.ShiftRight <ushort>();
     TotalFragments = readStream.ShiftRight <ushort>();
 }
        private void OnRead(EndPoint endPoint, IReadStream readStream)
        {
            var packetHead = Packet.Unpacking(readStream);

            if ((packetHead.SendOption & SendOption.Acknowledge) > 0)
            {
                if ((packetHead.SendOption & SendOption.Connect) > 0)
                {
                    AcknowledgeConnect(endPoint, readStream, packetHead);
                    return;
                }

                if ((packetHead.SendOption & SendOption.Disconnect) > 0)
                {
                    AcknowledgeDisonnect(endPoint, readStream, packetHead);
                    return;
                }

                if ((packetHead.SendOption & SendOption.Fragment) > 0)
                {
                    AcknowledgeFragment(endPoint, readStream, packetHead);
                    return;
                }

                if ((packetHead.SendOption & SendOption.Ack) > 0)
                {
                    AcknowledgeAck(endPoint, readStream, packetHead);
                    return;
                }
            }

            if ((packetHead.SendOption & SendOption.Ack) > 0)
            {
                ResponseAck(endPoint, readStream, packetHead);
                return;
            }

            if ((packetHead.SendOption & SendOption.Fragment) > 0)
            {
                ResponseFragment(endPoint, readStream, packetHead);
                return;
            }

            if ((packetHead.SendOption & SendOption.Connect) > 0)
            {
                ResponseConnect(endPoint, readStream, packetHead);
                return;
            }

            if ((packetHead.SendOption & SendOption.Disconnect) > 0)
            {
                ResponseDisconnect(endPoint, readStream, packetHead);
            }
        }
 public void OnAcknowledge(bool success, IReadStream readStream)
 {
     if (HandleAcknowledge == null)
     {
         Logger.Debug($"{nameof(HandleAcknowledge)}没添加事件!");
     }
     else
     {
         HandleAcknowledge.Invoke(success, readStream);
     }
 }
        private void PeerConnected(SocketService socketService, IReadStream readStream)
        {
            var peer = AddListener(socketService);

            var tmp = readStream.Clone();

            _messageQueue.Enqueue(() => {
                Connected?.Invoke(peer, readStream);
                tmp.Dispose();
            });
        }
 public IReadStream <T> Create(IReadStream <string> stringStream)
 {
     try
     {
         IAdapter <IReadOnlyDictionary <string, string>, T> objectAdapter = null;
         return(new HeaderBasedAdapterStream <string, T>(stringStream, XsvAdapterFactory.CreateXsvToObjectFactory(split, objectAdapter)));
     } catch (Exception)
     {
         stringStream.Dispose();
         throw;
     }
 }
Example #18
0
        private void AcknowledgeDisconnect(IPEndPoint endPoint, IReadStream readStream,
                                           PacketHead packetHead)
        {
            if (!_connections.TryGetValue(endPoint, out var socketService))
            {
                Logger.Error($"{endPoint}连接没有添加!");
                return;
            }

            OnDisconnect(socketService);

            Logger.Info($"应答{endPoint}主动断开连接!");
        }
Example #19
0
        public void MergeFrom(IReadStream input)
        {
            uint tag;

            while ((tag = input.ReadTag()) != 0)
            {
                switch (tag)
                {
                default:
                    input.SkipLastField(tag);
                    break;
                }
            }
        }
Example #20
0
        private void ResponseDisconnect(IPEndPoint endPoint, IReadStream readStream,
                                        PacketHead packetHead)
        {
            if (!_connections.TryGetValue(endPoint, out var socketService))
            {
                Logger.Warn("断开没有记录的连接?");
                return;
            }

            Logger.Info($"{endPoint}主动断开!");
            Acknowledge(socketService, SendOption.Acknowledge | SendOption.Disconnect, packetHead);

            OnDisconnect(socketService);
        }
Example #21
0
        public void Handle(ulong remoteMessageId, IPeer peer, IReadStream readStream)
        {
            if (peer == null)
            {
                Logger.Error($"{nameof(peer)} 为 null");
                return;
            }

            try {
                peer.Response(MessageId, this, new Deserializable(readStream));
            } catch (Exception e) {
                Logger.Error(e.ToString());
            }
        }
Example #22
0
        private void OnRead(NetworkConfig networkConfig, IPEndPoint endPoint, IReadStream readStream)
        {
            var packetHead = Packet.Unpacking(readStream);

            Logger.Debug("SendOption:" + packetHead.SendOption);

            if ((packetHead.SendOption & SendOption.Acknowledge) > 0)
            {
                if ((packetHead.SendOption & SendOption.Connect) > 0)
                {
                    AcknowledgeConnect(endPoint, readStream, packetHead);
                    return;
                }

                if ((packetHead.SendOption & SendOption.Disconnect) > 0)
                {
                    AcknowledgeDisconnect(endPoint, readStream, packetHead);
                    return;
                }

                if ((packetHead.SendOption & SendOption.Fragment) > 0)
                {
                    AcknowledgeFragment(endPoint, readStream, packetHead);
                    return;
                }

                if ((packetHead.SendOption & SendOption.Ack) > 0)
                {
                    return;
                }
            }

            if ((packetHead.SendOption & SendOption.Ack) > 0)
            {
                ResponseAck(endPoint, readStream, packetHead);
                return;
            }

            if ((packetHead.SendOption & SendOption.Fragment) > 0)
            {
                ResponseFragment(endPoint, readStream, packetHead);
                return;
            }

            if ((packetHead.SendOption & SendOption.Disconnect) > 0)
            {
                ResponseDisconnect(endPoint, readStream, packetHead);
            }
        }
 public void OnRead(IReadStream readStream)
 {
     if (HandleRead == null)
     {
         Logger.Debug($"{nameof(HandleRead)}没添加事件!");
     }
     else
     {
         try {
             HandleRead.Invoke(readStream);
         } catch (Exception e) {
             Logger.Error(e);
         }
     }
 }
Example #24
0
        public static void Invoke(ulong remoteMessageId, IReadStream readStream, IPeer peer)
        {
            var index = (MessageType)readStream.ShiftRight <short>();

            if (!_messages.TryGetValue(index, out var func))
            {
                Logger.Warn($"没有添加消息:{index}");
                return;
            }

            var message = func();

            message.DeserializeFields(readStream);
            message.Handle(remoteMessageId, peer, readStream);
        }
        private void ResponseAck(EndPoint endPoint, IReadStream readStream, PacketHead packetHead)
        {
            if (!_connections.TryGetValue(endPoint, out var socketService))
            {
                Logger.Error($"{endPoint}连接没有添加!");
                return;
            }
            Logger.Info($"接收到{endPoint}心跳包!");

            Acknowledge(socketService, SendOption.Acknowledge | SendOption.Ack, packetHead);
            SocketStatistics.LogAcknowledgementSend();
            SocketStatistics.LogUnreliableReceive();
            SocketStatistics.LogAcknowledgementSend();
            SocketStatistics.LogAckReceive();
            SocketStatistics.LogTotalBytesReceived(PacketHead.GetSize());
        }
        private void AcknowledgeDisonnect(EndPoint endPoint, IReadStream readStream, PacketHead packetHead)
        {
            if (!_connections.TryGetValue(endPoint, out var socketService))
            {
                Logger.Error($"{endPoint}连接没有添加!");
                return;
            }

            OnDisconnect(socketService);

            SocketStatistics.LogUnreliableReceive();
            SocketStatistics.LogAcknowledgementReceive();
            SocketStatistics.LogDataBytesReceived(packetHead.TotalBytes);
            SocketStatistics.LogTotalBytesReceived(packetHead.Length);
            Logger.Info($"应答{endPoint}主动断开连接!");
        }
        private void ResponseDisconnect(EndPoint endPoint, IReadStream readStream, PacketHead packetHead)
        {
            if (!_connections.TryGetValue(endPoint, out var socketService))
            {
                Logger.Warn("断开没有记录的连接?");
                return;
            }

            Logger.Info($"{endPoint}主动断开!");
            Acknowledge(socketService, SendOption.Acknowledge | SendOption.Disconnect, packetHead);

            SocketStatistics.LogAcknowledgementSend();
            SocketStatistics.LogUnreliableReceive();
            SocketStatistics.LogTotalBytesReceived(PacketHead.GetSize());

            OnDisconnect(socketService);
        }
        private bool MoveNext()
        {
            bool moved = streamIterator.MoveNext();

            if (moved)
            {
                currentStream = streamIterator.Current.Create();
                if (currentStream != null)
                {
                    createdStreams.Add(currentStream);
                }
            }
            else
            {
                currentStream = null;
            }
            return(moved);
        }
Example #29
0
        private void AcknowledgeFragment(IPEndPoint endPoint, IReadStream readStream,
                                         PacketHead packetHead)
        {
            if (!_connections.TryGetValue(endPoint, out var socketService))
            {
                Logger.Error($"{endPoint}连接没有添加!");
                return;
            }
            var id = BuildID(packetHead);

            if (!(_fragmentsTimer.Remove(id) && socketService.PacketIds.Remove(id)))
            {
                return;
            }

            socketService.OnAcknowledge(true, readStream);
            Logger.Info($"{endPoint}:{packetHead.PacketId}消息发送完成!");
        }
Example #30
0
        public void MergeFrom(IReadStream input)
        {
            uint tag;

            while ((tag = input.ReadTag()) != 0)
            {
                switch (tag)
                {
                case 9:
                {
                    value = input.ReadSFixed64();
                }
                break;

                default:
                    input.SkipLastField(tag);
                    break;
                }
            }
        }