Example #1
0
        static void Main(String[] args)
        {
            var a = new CCrypto();
            var b = new CCrypto();

            byte[] data = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };

            for (int i = 0; i < 10; ++i)
            {
                a.Encode(data, 0, data.Length, 0x1f3a49b72c8d5ef6);
                foreach (byte d in data)
                {
                    Console.Write(String.Format("{0,4}", d));
                }
                Console.WriteLine();

                b.Decode(data, 0, data.Length, 0x1f3a49b72c8d5ef6);
                foreach (byte d in data)
                {
                    Console.Write(String.Format("{0,4}", d));
                }
                Console.WriteLine();
            }
        }
Example #2
0
            public ERecvState RecvedBegin()
            {
                // For Header /////////////////////
                if (_RecvHeader == null)
                {
                    if (StreamRcv.Size < _HeaderSize)
                    {
                        return(ERecvState.NoData);
                    }

                    _RecvHeader = new SHeader();
                    _RecvHeader.Push(StreamRcv);
                }

                // For Body /////////////////////
                if (StreamRcv.Size < _RecvHeader.Size)
                {
                    return(ERecvState.NoData);
                }

                if (_RecvHeader.SendPacketSeq != (_RecvPacketSeq + 1))
                {
                    throw new Exception("PacketSeq Error " + _RecvHeader.SendPacketSeq.ToString() + ", " + (_RecvPacketSeq + 1).ToString());
                }

                _RecvPacketSeq = _RecvHeader.SendPacketSeq;
                _Crypto.Decode(StreamRcv.Data, StreamRcv.Head, _RecvHeader.Size, (0x1f3a49b72c8d5ef6 ^ (UInt64)_RecvHeader.Size ^ _RecvHeader.CheckSum ^ _RecvHeader.SendPacketSeq));

                if (_RecvHeader.CheckSum != core.CBase.GetCheckSum(StreamRcv.Data, StreamRcv.Head, _RecvHeader.Size))
                {
                    throw new Exception("CheckSum Error");
                }

                var OldStreamSize = StreamRcv.Size;
                var Header2       = new SHeader2();

                Header2.Push(StreamRcv);
                _RecvHeader.Size -= (OldStreamSize - StreamRcv.Size);
                StreamRcv.SaveState();

                switch (Header2.PacketType)
                {
                case EPacketType.Ping:
                    if (!_SendPong())
                    {
                        throw new Exception("_SendPong Fail");
                    }

                    return(ERecvState.PingPong);

                case EPacketType.Pong:
                    if (Header2.RecvPacketSeq == _PingPacketSeq)
                    {
                        _Latency  = DateTime.Now - _PingTime.Value;
                        _PingTime = null;
                    }
                    return(ERecvState.PingPong);

                case EPacketType.User:
                    break;

                default:
                    throw new Exception("Invalid Packet Type");
                }

                return(ERecvState.UserData);
            }
Example #3
0
            void _Recved() // 수신 처리
            {
                if (_RecvEvent.SocketError != SocketError.Success)
                {
                    _UnLink(ENetState.Normal);
                    return;
                }

                if (_RecvEvent.BytesTransferred == 0)
                {
                    _UnLink(ENetState.Normal);
                    return;
                }

                _HBRcvPeriod.NextLoose();

                if (!_StreamRcv.Push(_aRecvBuf.SubArray(0, _RecvEvent.BytesTransferred)))
                {
                    _UnLink(ENetState.Normal);
                    return;
                }

                while (true)
                {
                    // For Header /////////////////////
                    if (_RecvHeader == null)
                    {
                        if (_StreamRcv.Size < c_HeaderSize)
                        {
                            break;
                        }

                        try
                        {
                            _RecvHeader = new SHeader();
                        }
                        catch
                        {
                            _UnLink(ENetState.Normal);
                            return;
                        }

                        if (!_StreamRcv.Pop(_RecvHeader))
                        {
                            _UnLink(ENetState.Normal);
                            return;
                        }
                    }

                    // For Body /////////////////////
                    if (_StreamRcv.Size < _RecvHeader.Size)
                    {
                        break;
                    }

                    if (_RecvHeader.Size > 0)
                    {
                        if (_RecvHeader.PacketSeq <= _RecvPacketSeq &&
                            (_RecvPacketSeq - _RecvHeader.PacketSeq) < (TPacketSeq.MaxValue << 1))
                        {
                            _UnLink(ENetState.Normal);
                            return;
                        }
                        _RecvPacketSeq = _RecvHeader.PacketSeq;

                        var Body = _StreamRcv.GetStream(_RecvHeader.Size);
                        _Crypto.Decode(Body, (0x1f3a49b72c8d5ef6 ^ (UInt64)_RecvHeader.Size ^ _RecvHeader.CheckSum ^ _RecvHeader.PacketSeq));
                        for (int i = 0; i < Body.Count(); ++i)
                        {
                            _StreamRcv.SetAt(_StreamRcv.Head + i, Body[i]);
                        }

                        if (_RecvHeader.CheckSum != CBase.GetCheckSum(Body))
                        {
                            _UnLink(ENetState.Normal);
                            return;
                        }

                        var OldSize = _StreamRcv.Size;
                        var OldHead = _StreamRcv.Head;
                        var OldTail = _StreamRcv.Tail;
                        _StreamRcv.Tail = _StreamRcv.Head + _RecvHeader.Size;   // For Notifying Received Size to RecvFunc

                        _RecvFunc(_Key.PeerNum, _StreamRcv);

                        if (_Key == null)
                        {
                            return;
                        }

                        // 외부에서 모두 Pop하지 않았거나 Stream에 대해서 Clear 하지 않아도 상관 없으므로 에러 체크하지 않음.
                        //if (_StreamRcv.Size != OldSize - _RecvHeader.Size)
                        //    throw new Exception("Did Not Receive Whole Protocol");

                        if (OldSize > _RecvHeader.Size)
                        {
                            _StreamRcv.Head = OldHead + _RecvHeader.Size;
                            _StreamRcv.Tail = OldTail;
                        }
                        else
                        {
                            _StreamRcv.Clear();
                        }
                    }

                    _RecvHeader = null;
                }

                if (_Socket == null)
                {
                    return;
                }

                if (!_Recv())
                {
                    _UnLink(ENetState.Normal);
                    return;
                }
            }