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 string GetResponceFromServer() { byte[] buffer = new byte[128]; ClientStream.Read(buffer); buffer = buffer.Where(b => b != 0).ToArray(); string responce = Encoding.UTF8.GetString(buffer); return(responce); }
public Package GetMessage() { Package pkg = new Package(); StringBuilder builder = new StringBuilder(); short Size = 0; byte[] SizeBytes = new byte[2]; ClientStream.Read(SizeBytes, 0, 2); Size = BitConverter.ToInt16(SizeBytes, 0); byte[] buff = new byte[Size]; ClientStream.Read(buff, 0, buff.Length); Console.WriteLine("Пришёл пакет размером :" + buff.Length); builder.Append(Encoding.Default.GetString(buff)); string str = builder.ToString().TrimEnd('\0'); pkg = XamlReader.Parse(str) as Package; return(pkg); }
private bool DoHandshake() { while (Client.Available == 0 && Client.Connected) { } if (!Client.Connected) { return(false); } byte[] handshake; using (var handshakeBuffer = new MemoryStream()) { while (Client.Available > 0) { var buffer = new byte[Client.Available]; ClientStream.Read(buffer, 0, buffer.Length); handshakeBuffer.Write(buffer, 0, buffer.Length); } handshake = handshakeBuffer.ToArray(); } if (!Encoding.UTF8.GetString(handshake).StartsWith("GET")) { return(false); } var response = Encoding.UTF8.GetBytes("HTTP/1.1 101 Switching Protocols" + Environment.NewLine + "Connection: Upgrade" + Environment.NewLine + "Upgrade: websocket" + Environment.NewLine + "Sec-WebSocket-Accept: " + Convert.ToBase64String( SHA1.Create().ComputeHash( Encoding.UTF8.GetBytes( new Regex("Sec-WebSocket-Key: (.*)").Match(Encoding.UTF8.GetString(handshake)).Groups[1].Value.Trim() + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" ) ) ) + Environment.NewLine + Environment.NewLine); ClientStream.Write(response, 0, response.Length); return(true); }
async private void ReceiveMessagesAsync() { await Task.Run(() => { try { while (TcpClient.Connected) { byte[] buffer = new byte[128]; ClientStream.Read(buffer); buffer = buffer.Where(b => b != 0).ToArray(); ChatBox += Encoding.UTF8.GetString(buffer) + "\n"; Task.Delay(10).Wait(); } } catch (Exception) { Disconnect(); } }); }
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(); } }