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(); } }
private void ServerLoop() { try { console.Info($"Headless connecting to {startConfig.ServerAddress}"); while (true) { lock (serverStreamLock) { if (serverStream == null) { serverStream = ConnectToServer(); } } while (serverStream.DataAvailable) { try { var packet = serverConnection.Receive(new NetworkStreamToPullStreamAdapter(serverStream)); if (packet.Id == PacketDefinitions.ConnectToGameServer.Id) { HandleConnectToGameServer(packetRegistry.Materialize <ConnectToGameServerPacket>(packet)); } else { ServerConnectionOnPacketReceived(this, packet); } } catch (EndOfStreamException ex) { console.Debug(ex.ToString()); // just swallow this exception, wait for the next batch } } if (disconnectTokenSource.Token.IsCancellationRequested) { return; } Thread.Sleep(1); } } catch (Exception ex) { console.Debug(serverDiagnosticPullStream.Flush()); console.Debug(ex.ToString()); throw; } }
private static void ServerLoop() { try { while (true) { if (needServerReconnect) { lock (serverStreamLock) { DisconnectFromServer(); needServerReconnect = false; ServerStream = ConnectToServer(); } } if (ServerStream == null) { ServerStream = ConnectToServer(); } if (ServerStream.DataAvailable) { try { serverConnection.Receive(new NetworkStreamToPullStreamAdapter(ServerStream)); } catch (EndOfStreamException ex) { Console.Error(ex.ToString()); // just swallow this exception, wait for the next batch } } Thread.Sleep(1); } } catch (Exception ex) { Console.Error(serverDiagnosticPullStream.Flush()); Console.Error(ex.ToString()); throw; } }
private void ServerLoop() { try { Console.Info($"Connecting to {proxyStartConfig.ServerAddress}"); while (true) { lock (serverStreamLock) { if (ServerStream == null) { ServerStream = ConnectToServer(); } } if (ServerStream.DataAvailable) { try { serverConnection.Process(new NetworkStreamToPullStreamAdapter(ServerStream)); } catch (EndOfStreamException ex) { Console.Debug(ex.ToString()); // just swallow this exception, wait for the next batch } } Thread.Sleep(1); } } catch (Exception ex) { Console.Error("Disconnected from server. Please, restart Infusion."); Console.Debug(serverDiagnosticPullStream.Flush()); Console.Debug(ex.ToString()); throw; } }
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(); } }