Example #1
0
        private void DataFromServer(IAsyncResult iAr)
        {
            try
            {
                if (_serverS == null)
                {
                    return;
                }
                int length = _serverS.EndReceive(iAr);
                if (length < 1)
                {
                    Disconnect(); return;
                }

                byte[] data = new byte[length];
                Buffer.BlockCopy(_serverB, 0, data, 0, length);

                if (!_hasOfficialSocket)
                {
                    string possiblePolicyResponse = Encoding.UTF8.GetString(data);
                    if (possiblePolicyResponse.Contains("</cross-domain-policy>"))
                    {
                        possiblePolicyResponse = possiblePolicyResponse
                                                 .Replace("</cross-domain-policy>",
                                                          "<allow-access-from domain=\"*\" to-ports=\"*\"/>\r\n</cross-domain-policy>");
                    }
                    SendToClient(Encoding.UTF8.GetBytes(possiblePolicyResponse));
                    _htcpExt.BeginAcceptSocket(SocketAccepted, null);
                    return;
                }

                if (IncomingDecrypt != null)
                {
                    IncomingDecrypt.Parse(data);
                }

                if (_toClientS == 2)
                {
                    length = data.Length >= 6 ? BigEndian.DecypherInt(data) : 0;
                    IsIncomingEncrypted = (length != data.Length - 4);
                }
                IList <byte[]> chunks = ByteUtils.Split(ref _serverC, data, !IsIncomingEncrypted);

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

                ReadServerData();
            }
            catch { Disconnect(); }
        }
Example #2
0
        private void OnReceiving(IAsyncResult ar)
        {
            try
            {
                SocketError errorCode;
                int         length = _client.EndReceive(ar, out errorCode);
                if (errorCode != SocketError.Success)
                {
                    Disconnect();
                }
                else if (ShouldReceive)
                {
                    var data = new byte[length];
                    Buffer.BlockCopy(_inBuffer, 0, data, 0, length);

                    if (IncomingDecrypt != null)
                    {
                        IncomingDecrypt.Parse(data);
                    }

                    if (_inStep == 2)
                    {
                        length = (data.Length >= 6 ? BigEndian.DecypherInt(data) : 0);
                        IsIncomingEncrypted = (length != data.Length - 4);
                    }

                    foreach (byte[] packet in ByteUtils.Split(ref _inCache, data, true))
                    {
                    }

                    if (ShouldReceive) // 'ShouldReceive' could have been changed while processing the current data.
                    {
                        _client.BeginReceive(_inBuffer, 0, _inBuffer.Length, SocketFlags.None, OnReceiving, null);
                    }
                }
            }
            catch (ObjectDisposedException) { }
            catch { Disconnect(); }
        }