예제 #1
0
 public void Proccess(INetworkPacket packet)
 {
     if (_PacketMap.TryGetValue(packet.GetType(), out Action <INetworkPacket> func))
     {
         func(packet);
     }
     else
     {
         Debug.LogError(packet.GetType() + " 메시지가 도착함 해당 패킷에 대한 기능이 패킷 맵에 등록되어 있지 않아요!");
     }
 }
예제 #2
0
 public void Proccess(NetworkClient client, INetworkPacket packet)
 {
     if (_PacketMap.TryGetValue(packet.GetType(), out Action <NetworkClient, INetworkPacket> func))
     {
         func(client, packet);
     }
 }
        public void OnReceivePacket(NetworkClient client, INetworkPacket packet)
        {
            if (packet.GetType() == typeof(PtkClientConnect))
            {
                lock (_ConnectWaitingClientLocker)
                {
                    if (_ConnectWaitingClient.Contains(client))
                    {
                        _ConnectWaitingClient.Remove(client);
                    }
                }

                lock (ConnectedClientsLocker)
                {
                    if (ConnectedClients.ContainsKey(packet.ID))
                    {
                        ConnectedClients[packet.ID] = client;
                    }
                    else
                    {
                        ConnectedClients.Add(packet.ID, client);
                    }
                }
            }

            _TcpReceivePacketStreamer.RegisterPacket(packet.ID, packet);
        }
 private static ArchitectureAttribute GetArchitectureFromPacket(INetworkPacket packet)
 {
     return(packet
            .GetType()
            .GetCustomAttributes(typeof(ArchitectureAttribute), false)
            .OfType <ArchitectureAttribute>()
            .FirstOrDefault());
 }
예제 #5
0
        public int Serialize(INetworkPacket packet, BinaryWriter writer)
        {
            if (packet == null)
            {
                throw new ArgumentNullException(nameof(packet));
            }

            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            var length = 0;

            try
            {
                switch (packet)
                {
                case PushAckPacket pushAckPacket:
                {
                    writer.Write((byte)pushAckPacket.ProtocolVersion);

                    var token = BitConverter.GetBytes(pushAckPacket.Token);
                    writer.Write(token, 0, 2);

                    var messageType = BitConverter.GetBytes((ushort)pushAckPacket.MessageType);
                    writer.Write(messageType, 0, 1);

                    length = (int)writer.BaseStream.Length;
                }
                break;

                case PullAckPacket pullAckPacket:
                {
                    writer.Write((byte)pullAckPacket.ProtocolVersion);

                    var token = BitConverter.GetBytes(pullAckPacket.Token);
                    writer.Write(token, 0, 2);

                    var messageType = BitConverter.GetBytes((ushort)pullAckPacket.MessageType);
                    writer.Write(messageType, 0, 1);

                    length = (int)writer.BaseStream.Length;
                }
                break;

                case PullRespPacket pullRespPacket:
                {
                    writer.Write((byte)pullRespPacket.ProtocolVersion);

                    writer.Write(new byte[2], 0, 2);

                    var messageType = BitConverter.GetBytes((ushort)pullRespPacket.MessageType);
                    writer.Write(messageType, 0, 1);

                    if (pullRespPacket.Payload == null)
                    {
                        throw new NullReferenceException(nameof(pullRespPacket.Payload));
                    }

                    var data = _payloadSerialiser.Serialize(pullRespPacket.Payload);
                    writer.Write(data, 0, data.Length);

                    length = (int)writer.BaseStream.Length;
                }
                break;

                case PushDataPacket pushDataPacket:
                {
                    writer.Write((byte)pushDataPacket.ProtocolVersion);

                    var token = BitConverter.GetBytes(pushDataPacket.Token);
                    writer.Write(token, 0, 2);

                    var messageType = BitConverter.GetBytes((ushort)pushDataPacket.MessageType);
                    writer.Write(messageType, 0, 1);

                    var eui = pushDataPacket.Eui.HexToBytes();
                    writer.Write(eui, 0, 8);

                    if (pushDataPacket.Payload == null)
                    {
                        throw new NullReferenceException(nameof(pushDataPacket.Payload));
                    }

                    var data = _payloadSerialiser.Serialize(pushDataPacket.Payload);
                    writer.Write(data, 0, data.Length);

                    length = (int)writer.BaseStream.Length;
                }
                break;

                case PullDataPacket pullDataPacket:
                {
                    writer.Write((byte)pullDataPacket.ProtocolVersion);

                    var token = BitConverter.GetBytes(pullDataPacket.Token);
                    writer.Write(token, 0, 2);

                    var messageType = BitConverter.GetBytes((ushort)pullDataPacket.MessageType);
                    writer.Write(messageType, 0, 1);

                    var eui = pullDataPacket.Eui.HexToBytes();
                    writer.Write(eui, 0, 8);

                    length = (int)writer.BaseStream.Length;
                }
                break;

                case TxAckPacket txAckPacket:
                {
                    writer.Write((byte)txAckPacket.ProtocolVersion);

                    var token = BitConverter.GetBytes(txAckPacket.Token);
                    writer.Write(token, 0, 2);

                    var messageType = BitConverter.GetBytes((ushort)txAckPacket.MessageType);
                    writer.Write(messageType, 0, 1);

                    var eui = txAckPacket.Eui.HexToBytes();
                    writer.Write(eui, 0, 8);

                    length = (int)writer.BaseStream.Length;
                }
                break;

                default: throw new NotImplementedException($"Packet type '{packet.GetType().Name}' is not yet implemented.");
                }

                return(length);
            }
            catch (Exception ex)
            {
                throw new PicocellSerializeException($"Could not serialize message.", ex);
            }
        }
 public void Send(INetworkPacket packet)
 {
     _SendPacketStreamer.RegisterPacket(packet);
     Debug.Log(packet.GetType() + " 패킷 전송완료");
 }