Size(2 bytes), PacketId(2 bytes)로 구성된 4바이트 헤더를 갖는 기본 패킷 구성입니다.
Inheritance: Aegis.IO.StreamBuffer
Ejemplo n.º 1
0
        private void Echo_Req(ReceiveData data)
        {
            _counter.Add(1);

            Packet resPacket = new Packet(Protocol.Echo_Res);
            Send(data.Sender as EndPoint, resPacket);
        }
Ejemplo n.º 2
0
        private void Echo_Res(Packet data)
        {
            _counter.Add(1);

            Packet reqPacket = new Packet(Protocol.Echo_Req);
            Send(reqPacket);
        }
Ejemplo n.º 3
0
        protected override void OnAccept()
        {
            Logger.Write(LogType.Info, 2, "[{0}] Accepted", SessionId);

            //  Hello packet을 클라이언트에 전달
            Packet packet = new Packet(0x01);
            SendPacket(packet);
        }
Ejemplo n.º 4
0
        private void OnReceived(IOEventResult result)
        {
            IntervalCounter.Counters["ReceiveCount"].Add(1);
            IntervalCounter.Counters["ReceiveBytes"].Add(result.Buffer.Length);

            Packet packet = new Packet(result.Buffer);
            Logger.Err("Invalid packet received(PacketId={0:X})", packet.PacketId);
        }
Ejemplo n.º 5
0
        private void OnAcceptd(IOEventResult result)
        {
            Logger.Info("[{0}] Accepted", SessionId);

            //  Hello packet을 클라이언트에 전달
            Packet packet = new Packet(Protocol.Hello_Ntf);
            SendPacket(packet);
        }
Ejemplo n.º 6
0
 public void Send(EndPoint ep, Packet packet)
 {
     try
     {
         _udp.Send(ep, packet.Buffer);
     }
     catch (Exception e)
     {
         Logger.Err(e.ToString());
     }
 }
Ejemplo n.º 7
0
 protected override void OnReceive(StreamBuffer buffer)
 {
     Packet packet = new Packet(buffer);
     AegisTask.Run(() =>
     {
         packet.SkipHeader();
         switch (packet.PID)
         {
             case 0x01: OnHello(packet); break;
             case 0x03: OnEcho_Res(packet); break;
         }
     });
 }
Ejemplo n.º 8
0
        public TestSession()
        {
            base.EventConnect += OnConnected;
            base.EventClose += OnClosed;
            base.EventReceive += OnReceived;
            base.PacketValidator += Packet.IsValidPacket;

            CreatePacketDispatcher(this, (ref StreamBuffer source, out string key) =>
            {
                source = new Packet(source as StreamBuffer);
                key = (source as Packet).PacketId.ToString();
                (source as Packet).SkipHeader();
            });
        }
Ejemplo n.º 9
0
        private void Echo_Res(Packet packet)
        {
            Packet reqPacket = new Packet(Protocol.Echo_Req);
            reqPacket.Write(_tempBuffer, 0, FormMain.BufferSize);

            SendPacket(reqPacket,
                        (buffer) => { return Packet.GetPacketId(buffer.Buffer) == Protocol.Echo_Res; },
                        (result) =>
                        {
                            packet.SkipHeader();
                            Echo_Res(new Packet(result.Buffer));
                        }
                );
        }
Ejemplo n.º 10
0
        protected override void OnReceive(StreamBuffer buffer)
        {
            Counter_ReceiveCount.Add(1);
            Counter_ReceiveBytes.Add(buffer.WrittenBytes);

            Packet packet = new Packet(buffer);
            AegisTask.Run(() =>
            {
                packet.SkipHeader();
                switch (packet.PID)
                {
                    case 0x02: OnEcho_Req(packet); break;
                }
            });
        }
Ejemplo n.º 11
0
 private void NetworkEvent_Receive(IOEventResult result)
 {
     try
     {
         int packetSize;
         if (Packet.IsValidPacket(result.Buffer, 0, result.Buffer.Length, out packetSize) == true)
         {
             Packet packet = new Packet(result.Buffer);
             if (_packetDispatcher.Dispatch(new ReceiveData(result.Sender as EndPoint, packet)) == false)
                 Logger.Err("[GameProcess] Invalid packet received(pid=0x{0:X}).", packet.PacketId);
         }
     }
     catch (Exception e)
     {
         Logger.Err(e.ToString());
     }
 }
Ejemplo n.º 12
0
        public ClientSession()
        {
            base.EventAccept += OnAcceptd;
            base.EventClose += OnClosed;
            base.EventReceive += OnReceived;
            base.PacketValidator += Packet.IsValidPacket;

            CreatePacketDispatcher(this, (ref StreamBuffer source, out string key) =>
            {
                source = new Packet(source as StreamBuffer);
                key = (source as Packet).PacketId.ToString();
                (source as Packet).SkipHeader();

                IntervalCounter.Counters["ReceiveCount"].Add(1);
                IntervalCounter.Counters["ReceiveBytes"].Add((source as Packet).Buffer.Length);
            });
        }
Ejemplo n.º 13
0
        public Boolean ProcessResponseWaitPacket(Packet packet)
        {
            lock (_listTCS)
            {
                foreach (TCSData data in _listTCS)
                {
                    if (data.packetId == packet.PacketId
                        && (data.predicate == null || data.predicate(packet) == true))
                    {
                        data.tcs.SetResult(new Packet(packet));
                        _listTCS.Remove(data);

                        return true;
                    }
                }
            }

            return false;
        }
Ejemplo n.º 14
0
 private void OnReceived(IOEventResult result)
 {
     Packet packet = new Packet(result.Buffer);
     Logger.Err("Invalid packet received(PacketId={0:X})", packet.PacketId);
 }
Ejemplo n.º 15
0
 private void OnEcho_Req(Packet packet)
 {
     Packet resPacket = new Packet(0x03);
     SendPacket(resPacket);
 }
Ejemplo n.º 16
0
 private void Hello_Ntf(Packet packet)
 {
     Packet reqPacket = new Packet(Protocol.Echo_Req);
     reqPacket.Write(_tempBuffer, 0, FormMain.BufferSize);
     SendPacket(reqPacket);
 }
Ejemplo n.º 17
0
 private void OnHello(Packet packet)
 {
     Packet reqPacket = new Packet(0x02);
     reqPacket.Write(_tempBuffer, 0, FormMain.BufferSize);
     SendPacket(reqPacket);
 }
Ejemplo n.º 18
0
        private void Start()
        {
            _packetDispatcher = new MethodSelector<Packet>(this, (ref Packet source, out string key) =>
            {
                key = source.PacketId.ToString();
                source.SkipHeader();
            });

            _udp = new UDPClient();
            _udp.EventRead += NetworkEvent_Receive;
            _udp.EventClose += NetworkEvent_Close;
            _udp.Connect("127.0.0.1", 10201);

            _counter = new IntervalCounter(1000);
            _counter.Start();
            (new IntervalTimer(1000, () =>
            {
                DateTime now = DateTime.Now;
                Console.WriteLine(string.Format("[{0}/{1} {2}:{3}:{4}] recv: {5}",
                                        DateTime.Now.Month, DateTime.Now.Day,
                                        DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second,
                                        _counter.Value));
            })).Start();

            {
                Packet reqPacket = new Packet(Protocol.Echo_Req);
                Send(reqPacket);
            }
        }
Ejemplo n.º 19
0
        /// <summary>
        /// 이 SecurePacket 객체와 동일한 내용의 새로운 객체를 생성합니다.
        /// </summary>
        /// <returns>현재 SecurePacket의 데이터가 복제된 객체</returns>
        public override StreamBuffer Clone()
        {
            Packet packet = new Packet(this);
            packet.Read(ReadBytes);

            return packet;
        }
Ejemplo n.º 20
0
 public ReceiveData(EndPoint sender, Packet packet)
 {
     Sender = sender;
     Packet = packet;
 }
Ejemplo n.º 21
0
        public virtual async Task<Packet> SendAndWaitResponse(Packet packet, UInt16 responsePacketId, Func<Packet, Boolean> predicate, Int32 timeout)
        {
            TaskCompletionSource<Packet> tcs = new TaskCompletionSource<Packet>();
            CancellationTokenSource cancel = new CancellationTokenSource();
            TCSData data = new TCSData() { packetId = responsePacketId, tcs = tcs, predicate = predicate };
            Packet response = null;


            lock (_listTCS)
            {
                _listTCS.Add(data);
            }


            //  Task.Result의 Timeout 처리
            Task.Run(async () =>
            {
                try
                {
                    await Task.Delay(timeout, cancel.Token);
                    tcs.SetCanceled();
                }
                catch (Exception)
                {
                }
            });

            //  Packet Send & Response 작업
            await Task.Run(() =>
            {
                try
                {
                    _session.SendPacket(packet);
                    response = tcs.Task.Result;
                    cancel.Cancel();
                }
                catch (Exception)
                {
                    //  Task가 Cancel된 경우 추가된 작업(data)을 삭제한다.
                    _listTCS.Remove(data);
                }
            });

            cancel.Dispose();


            if (response == null)
                throw new WaitResponseTimeoutException("The waiting time of ResponsePacketId(0x{0:X}) has expired.", responsePacketId);

            return response;
        }
Ejemplo n.º 22
0
 private void Echo_Req(Packet packet)
 {
     Packet resPacket = new Packet(Protocol.Echo_Res);
     SendPacket(resPacket);
 }
Ejemplo n.º 23
0
        public virtual async Task<Packet> SendAndWaitResponse(Packet packet, UInt16 responsePacketId)
        {
            TaskCompletionSource<Packet> tcs = new TaskCompletionSource<Packet>();
            TCSData data = new TCSData() { packetId = responsePacketId, tcs = tcs, predicate = null };
            Packet response = null;


            lock (_listTCS)
            {
                _listTCS.Add(data);
            }


            await Task.Run(() =>
            {
                try
                {
                    _session.SendPacket(packet);
                    response = tcs.Task.Result;
                }
                catch (Exception)
                {
                    //  Nothing to do.
                }
            });

            return response;
        }