Esempio n. 1
0
        public HeadlessClient(Legacy legacyApi, IConsole console, HeadlessStartConfig startConfig)
        {
            this.legacyApi             = legacyApi;
            this.console               = console;
            this.startConfig           = startConfig;
            packetRegistry             = PacketDefinitionRegistryFactory.CreateClassicClient(startConfig.ClientVersion);
            diagnosticProvider         = new InfusionDiagnosticPushStreamProvider(LogConfig, console);
            serverDiagnosticPushStream =
                new CompositeDiagnosticPushStream(new ConsoleDiagnosticPushStream(packetLogger, "headless -> server", packetRegistry),
                                                  new InfusionBinaryDiagnosticPushStream(DiagnosticStreamDirection.ClientToServer, diagnosticProvider.GetStream));
            serverDiagnosticPullStream = new ConsoleDiagnosticPullStream(packetLogger, "server -> headless", packetRegistry);

            serverConnection = new ServerConnection(ServerConnectionStatus.Initial, serverDiagnosticPullStream,
                                                    serverDiagnosticPushStream, packetRegistry, startConfig.Encryption);
            serverConnection.PacketReceived += ServerConnectionOnPacketReceived;
            ultimaServer = new UltimaServer(serverPacketHandler, SendToServer, packetRegistry);

            serverPacketHandler = new ServerPacketHandler(packetRegistry);
            clientPacketHandler = new ClientPacketHandler(packetRegistry);
            serverEndpoint      = startConfig.ServerEndPoint;

            ultimaClient = new UltimaClient(clientPacketHandler, SendToClient);

            serverPacketHandler.RegisterFilter(FilterServerPacket);
            serverPacketHandler.Subscribe(PacketDefinitions.GameServerList, HandleGameServerList);
            serverPacketHandler.Subscribe(PacketDefinitions.CharactersStartingLocations, HandleCharactersStartingLocationsPacket);
            serverPacketHandler.Subscribe(PacketDefinitions.EnableLockedClientFeatures, HandleEnableLockedClientFeatures);

            clientPacketHandler.Subscribe(PacketDefinitions.LoginRequest, HandleLoginRequest);
            clientPacketHandler.Subscribe(PacketDefinitions.GameServerLoginRequest, HandleGameServerLoginRequest);
            clientPacketHandler.Subscribe(PacketDefinitions.SelectServerRequest, HandleSelectServerRequest);
        }
Esempio n. 2
0
        private static void ClientLoop(ILogger packetLogger)
        {
            var diagnosticProvider = new InfusionDiagnosticPushStreamProvider(Configuration, Console);

            serverDiagnosticPushStream =
                new CompositeDiagnosticPushStream(new ConsoleDiagnosticPushStream(packetLogger, "proxy -> server"),
                                                  new InfusionBinaryDiagnosticPushStream(DiagnosticStreamDirection.ClientToServer, diagnosticProvider.GetStream));
            serverDiagnosticPullStream = new ConsoleDiagnosticPullStream(packetLogger, "server -> proxy");

            serverConnection = new ServerConnection(ServerConnectionStatus.Initial, serverDiagnosticPullStream,
                                                    serverDiagnosticPushStream);
            serverConnection.PacketReceived += ServerConnectionOnPacketReceived;

            clientConnection = new UltimaClientConnection(UltimaClientConnectionStatus.Initial,
                                                          new ConsoleDiagnosticPullStream(packetLogger, "client -> proxy"),
                                                          new CompositeDiagnosticPushStream(new ConsoleDiagnosticPushStream(packetLogger, "proxy -> client"),
                                                                                            new InfusionBinaryDiagnosticPushStream(DiagnosticStreamDirection.ServerToClient, diagnosticProvider.GetStream)));
            clientConnection.PacketReceived += ClientConnectionOnPacketReceived;

            diagnosticProvider.ClientConnection = clientConnection;
            diagnosticProvider.ServerConnection = serverConnection;

            Task.Run(() => ServerLoop());

            try
            {
                while (true)
                {
                    var client = listener.AcceptTcpClient();
                    ClientStream = client.GetStream();

                    int receivedLength;
                    var receiveBuffer = new byte[65535];

                    while ((receivedLength = ClientStream.Read(receiveBuffer, 0, receiveBuffer.Length)) > 0)
                    {
                        var memoryStream = new MemoryStream(receiveBuffer, 0, receivedLength, false);

                        clientConnection.ReceiveBatch(new MemoryStreamToPullStreamAdapter(memoryStream));
                    }

                    Thread.Yield();
                }
            }
            catch (IOException ioex) when(ioex.InnerException is SocketException socex && socex.SocketErrorCode == SocketError.ConnectionReset)
            {
                Console.Error("Connection to client lost.");
                throw;
            }
            catch (Exception ex)
            {
                Console.Error(serverDiagnosticPullStream.Flush());
                Console.Error(ex.ToString());
                throw;
            }
            finally
            {
                diagnosticProvider.Dispose();
            }
        }
Esempio n. 3
0
        private void ClientLoop(ILogger packetLogger)
        {
            var diagnosticProvider = new InfusionDiagnosticPushStreamProvider(LogConfig, Console);

            serverDiagnosticPushStream =
                new CompositeDiagnosticPushStream(new ConsoleDiagnosticPushStream(packetLogger, "proxy -> server", packetRegistry),
                                                  new InfusionBinaryDiagnosticPushStream(DiagnosticStreamDirection.ClientToServer, diagnosticProvider.GetStream));
            serverDiagnosticPullStream = new ConsoleDiagnosticPullStream(packetLogger, "server -> proxy", packetRegistry);

            serverConnection = new ServerConnection(ServerConnectionStatus.Initial, serverDiagnosticPullStream,
                                                    serverDiagnosticPushStream, packetRegistry, proxyStartConfig.Encryption);
            serverConnection.PacketReceived += ServerConnectionOnPacketReceived;

            clientConnection = new UltimaClientConnection(UltimaClientConnectionStatus.Initial,
                                                          new ConsoleDiagnosticPullStream(packetLogger, "client -> proxy", packetRegistry),
                                                          new CompositeDiagnosticPushStream(new ConsoleDiagnosticPushStream(packetLogger, "proxy -> client", packetRegistry),
                                                                                            new InfusionBinaryDiagnosticPushStream(DiagnosticStreamDirection.ServerToClient, diagnosticProvider.GetStream)), packetRegistry,
                                                          proxyStartConfig.Encryption, proxyStartConfig.LoginEncryptionKey);
            clientConnection.PacketReceived           += ClientConnectionOnPacketReceived;
            clientConnection.NewGameEncryptionStarted += ClientConnectionOnNewGameEncryptionStarted;
            clientConnection.LoginEncryptionStarted   += ClientConnectionOnLoginEncryptionStarted;

            diagnosticProvider.ClientConnection = clientConnection;
            diagnosticProvider.ServerConnection = serverConnection;
            bool serverLoopStarted = false;

            try
            {
                while (true)
                {
                    var client = listener.AcceptTcpClient();
                    ClientStream = client.GetStream();

                    lock (serverStreamLock)
                    {
                        if (ServerStream == null)
                        {
                            ServerStream = ConnectToServer();
                        }
                    }

                    if (!serverLoopStarted)
                    {
                        Task.Run(() => ServerLoop());
                        serverLoopStarted = true;
                    }

                    int receivedLength;
                    var receiveBuffer = new byte[65535];

                    while ((receivedLength = ClientStream.Read(receiveBuffer, 0, receiveBuffer.Length)) > 0)
                    {
#if DUMP_RAW
                        Console.Info("client -> proxy");
                        Console.Info(receiveBuffer.Take(receivedLength).Select(x => x.ToString("X2")).Aggregate((l, r) => l + " " + r));
#endif

                        var memoryStream = new MemoryStream(receiveBuffer, 0, receivedLength, false);
                        clientConnection.ReceiveBatch(new MemoryStreamToPullStreamAdapter(memoryStream), receivedLength);
                    }

                    lock (serverStreamLock)
                    {
                        DisconnectFromServer();
                        ServerStream = ConnectToServer();
                    }

                    Thread.Yield();
                }
            }
            catch (IOException ioex) when(ioex.InnerException is SocketException socex && socex.SocketErrorCode == SocketError.ConnectionReset)
            {
                Console.Error("Connection to client lost. Please restart infusion.");
                Console.Debug(socex.ToString());
            }
            catch (Exception ex)
            {
                Console.Error("Connection to client lost. Please restart infusion.");
                Console.Debug(serverDiagnosticPullStream.Flush());
                Console.Debug(ex.ToString());
                throw;
            }
            finally
            {
                diagnosticProvider.Dispose();
            }
        }