Example #1
0
            bool _SendEnd()
            {
                ++_SendPacketSeq;

                byte[] Body = _StreamSnd.GetStreamAt(_StreamSndTail + c_HeaderSize);
                if (!_StreamSnd.SetAt(_StreamSndTail, Body.Length))
                {
                    return(false);
                }

                if (!_StreamSnd.SetAt(_StreamSndTail + sizeof(TSize) + sizeof(TCheckSum), _SendPacketSeq))
                {
                    return(false);
                }

                if (Body.Length > 0)
                {
                    var CheckSum = CBase.GetCheckSum(Body);
                    if (!_StreamSnd.SetAt(_StreamSndTail + sizeof(TSize), CheckSum))
                    {
                        return(false);
                    }

                    _Crypto.Encode(Body, (0x1f3a49b72c8d5ef6 ^ (UInt64)Body.Length ^ CheckSum ^ _SendPacketSeq));
                    for (int i = 0; i < Body.Count(); ++i)
                    {
                        if (!_StreamSnd.SetAt(_StreamSndTail + c_HeaderSize + i, Body[i]))
                        {
                            return(false);
                        }
                    }
                }

                if (!_Sending)
                {
                    _SendCore();
                }

                return(true);
            }
Example #2
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;
                }
            }
Example #3
0
 public UInt64 CheckSum()
 {
     return(CBase.GetCheckSum(_Bytes.Data, 0, _Bytes.Size));
 }