Example #1
0
        private async Task ReceiveRemoteContractorDataAsync()
        {
            try
            {
                HMessage packet = await _remoteContractor.ReceivePacketAsync().ConfigureAwait(false);

                if (packet == null)
                {
                    Environment.Exit(0);
                }
                #region Switch: packet.Header
                switch (packet.Header)
                {
                case 0:
                {
                    _initStep++;
                    _hotel = (HHotel)packet.ReadShort();
                    break;
                }

                case 1:
                {
                    _initStep++;
                    _in  = new Incoming();
                    _out = new Outgoing();

                    string location = packet.ReadString();
                    if (!string.IsNullOrWhiteSpace(location))
                    {
                        _game = new HGame(location);
                        _game.Disassemble();

                        _game.GenerateMessageHashes();
                        if (packet.Readable > 0)
                        {
                            string hashesPath = packet.ReadString();
                            _in.Load(_game, hashesPath);
                            _out.Load(_game, hashesPath);
                        }
                        _module.ModifyGame(_game);
                    }
                    break;
                }

                case 2:
                {
                    _initStep++;
                    _gameData = new HGameData(packet.ReadString());
                    _module.ModifyGameData(_gameData);
                    break;
                }

                case 3:
                {
                    _initStep++;
                    var connection = (ContractorProxy)_connection;
                    connection.Port    = packet.ReadShort();
                    connection.Host    = packet.ReadString();
                    connection.Address = packet.ReadString();
                    break;
                }

                case 4:
                case 5:
                {
                    var destination = (HDestination)(packet.Header - 4);

                    string stamp       = packet.ReadString();
                    int    step        = packet.ReadInteger();
                    bool   isBlocked   = packet.ReadBoolean();
                    int    dataLength  = packet.ReadInteger();
                    byte[] data        = packet.ReadBytes(dataLength);
                    var    interPacket = new HMessage(data, destination);

                    var args = new DataInterceptedEventArgs(interPacket, step, (destination == HDestination.Server));
                    try
                    {
                        if (destination == HDestination.Server)
                        {
                            _module.HandleOutgoing(args);
                        }
                        else
                        {
                            _module.HandleIncoming(args);
                        }
                    }
                    finally
                    {
                        await SendInterceptedDataResponseAsync(
                            stamp, args).ConfigureAwait(false);
                    }
                    break;
                }
                }
                #endregion

                if (_initStep == 4)
                {
                    _initializationSource?.SetResult(true);
                }
            }
            finally
            {
                Task receiveRemContDataTask = ReceiveRemoteContractorDataAsync();
            }
        }