예제 #1
0
        private void DataFromClient(IAsyncResult iAr)
        {
            try
            {
                if (_clientS == null)
                {
                    return;
                }
                int length = _clientS.EndReceive(iAr);
                if (length < 1)
                {
                    Disconnect(); return;
                }

                byte[] data = ByteUtils.CopyBlock(_clientB, 0, length);
                #region Official Socket Check
                if (!_hasOfficialSocket)
                {
                    bool isModern = Modern.DecypherShort(data, 4) == 4000;
                    if (_hasOfficialSocket = (isModern || Ancient.DecypherShort(data, 3) == 206))
                    {
                        ResetHost();

                        _htcpExt.Stop();
                        _htcpExt = null;

                        Protocol = isModern ? HProtocol.Modern : HProtocol.Ancient;
                        OnConnected(EventArgs.Empty);
                    }
                    else
                    {
                        SendToServer(data);
                        return;
                    }
                }
                #endregion
                #region Decrypt/Split
                if (ClientDecrypt != null)
                {
                    ClientDecrypt.Parse(data);
                }

                if (_toServerS == 3 && Protocol == HProtocol.Modern)
                {
                    int dLength = data.Length >= 6 ? Modern.DecypherInt(data) : 0;
                    RequestEncrypted = (dLength != data.Length - 4);
                }

                byte[][] chunks = RequestEncrypted ? new[] { data } : ByteUtils.Split(ref _clientC, data, HDestination.Server, Protocol);
                #endregion

                foreach (byte[] chunk in chunks)
                {
                    ProcessOutgoing(chunk);
                }

                ReadClientData();
            }
            catch { Disconnect(); }
        }
예제 #2
0
        private void DataFromClient(IAsyncResult iAr)
        {
            try
            {
                if (ClientS == null)
                {
                    return;
                }
                int Length = ClientS.EndReceive(iAr);

                if (Length > 0)
                {
                    byte[] Data = ByteUtils.CopyBlock(ClientB, 0, Length);
                    #region Official Socket Check
                    if (!HasOfficialSocket)
                    {
                        bool IsModern = false;
                        if (HasOfficialSocket = (Ancient.DecypherShort(Data, 3) == 206 || (IsModern = BigEndian.DecypherShort(Data, 4) == 4000)))
                        {
                            TCP.Stop(); TCP = null;
                            ResetHost();
                            Protocol = IsModern ? HProtocols.Modern : HProtocols.Ancient;
                            if (OnConnected != null)
                            {
                                OnConnected(this, EventArgs.Empty);
                            }
                        }
                        else
                        {
                            SendToServer(Data);
                            return;
                        }
                    }
                    #endregion

                    try
                    {
                        #region Decrypt/Split
                        if (ClientDecrypt != null)
                        {
                            ClientDecrypt.Parse(Data);
                        }

                        if ((ToServerS + 1) == 4 && Protocol == HProtocols.Modern)
                        {
                            int DLength = BigEndian.DecypherInt(Data);
                            RequestEncrypted = (DLength > Data.Length - 4 || DLength < 6);
                        }

                        byte[][] Chunks = RequestEncrypted ? new byte[1][] { Data } : Chunks = ByteUtils.Split(ref SCache, Data, HDestinations.Server, Protocol);
                        #endregion
                        foreach (byte[] Chunk in Chunks)
                        {
                            ++ToServerS;
                            if (DataToServer == null)
                            {
                                SendToServer(Chunk);
                            }
                            else
                            {
                                DataToEventArgs Args = new DataToEventArgs(Chunk, HDestinations.Server, ToServerS);
                                try { DataToServer(this, Args); }
                                catch
                                {
                                    SendToServer(Chunk);
                                    continue;
                                }
                                if (!Args.Skip)
                                {
                                    SendToServer(Args.Packet.ToBytes());
                                }
                            }
                            if (CaptureEvents && !RequestEncrypted)
                            {
                                DataFromClient(Chunk);
                            }
                        }
                    }
                    catch { SendToServer(Data); }
                    ReadClientData();
                }
                else
                {
                    Disconnect();
                }
            }
            catch { Disconnect(); }
        }