public static void SendDataPackage(Connection.Client client, Connection.DataPackages.Template.DataPackage dataPacket) { try { XmlSerializer xmlSerializer = new XmlSerializer(typeof(Connection.DataPackages.Template.DataPackage)); if (client.stream.CanWrite) { xmlSerializer.Serialize(client.stream, dataPacket); } else { Events.ErrorEvents.NetworkStreamError.OnErrorAppeared(new Events.Args.ErrorEventArgs() { ErrorMessage = "In den NetworkStream des Clients mit dem Nutzernamen: " + client.Username + " kann nicht geschrieben werden! Datenpacket nicht gesendet!" }); } } catch { Events.ErrorEvents.ConnectionError.OnErrorAppeared(new Events.Args.ErrorEventArgs() { ErrorMessage = "Fehler beim Senden eines Datenpackets an Client: " + client.Username + "!" }); } }
// Used to write the data from the client public static async void write(Server serv, Connection con, Connection.Client cli) { while (con.Status == Connection.StatusFlag.Active && cli.IsAuth) { await cli.WriteBytesAsync(Encoding.ASCII.GetBytes(Console.ReadLine() + "\r\n")); await Task.Delay(100); } }
public Workspace(Connection.Client pClient) { _objClient = pClient; WorkspaceItem = new WorkspaceItemAction(pClient); //ToDo: Dependency should be the other way around, actions should start 'something' AutoRender.Subscription.Messaging.Action.Request.SendWorkspaceUpdatedRequestAction.WorkspaceUpdated += SendWorkspaceUpdatedRequestAction_WorkspaceUpdated; AutoRender.Messaging.Actions.Request.ReloadRequestAction.ReloadRequested += ReloadRequestAction_ReloadRequested;; }
// Used to read the data from the client public static async void read(Server serv, Connection con, Connection.Client cli) { byte[] read; while (con.Status == Connection.StatusFlag.Active && cli.IsAuth) { read = await cli.ReadBytesAsync(2048); if (read != null) { if (read.Length > 0) { Console.Write(Encoding.ASCII.GetString(read)); } } await Task.Delay(100); } }
public static bool CheckUser(Connection.Client client) { if (File.Exists(Environment.CurrentDirectory + "\\Users\\" + client.Username)) { StreamReader STR = new StreamReader(Environment.CurrentDirectory + "\\Users\\" + client.Username); string Password = STR.ReadLine(); if (Password == client.Password) { return(true); } else { return(false); } } else { return(false); } }
public static Connection.ClientRank GetUserRank(Connection.Client client) { if (File.Exists(Environment.CurrentDirectory + "\\Users\\" + client.Username)) { try { StreamReader STR = new StreamReader(Environment.CurrentDirectory + "\\Users\\" + client.Username); STR.ReadLine(); int Rank = Int32.Parse(STR.ReadLine()); STR.Close(); Connection.ClientRank clientRank = (Connection.ClientRank)Rank; return(clientRank); } catch { return(0); } } else { return(0); } }
static void Main(string[] args) { Server server = new Server(null, 2222, "xs8\n"); // '\n' used if the client is sending data from a terminal which appends a new line at the end of the data Connection connection = new Connection(server); Connection.Client client = new Connection.Client(connection); Start: if (connection.Status == Connection.StatusFlag.NotActive) { connection.StartAsync(); Console.WriteLine("[Server created]\r\n"); } Console.WriteLine(" > Waiting client..."); while (client.Status == Connection.Client.StatusFlag.UnknownOrDisconnected) { Thread.Sleep(10); } Console.WriteLine($" > Client connected : {{{client.IP}:{client.Port}}}"); if (client.Status == Connection.Client.StatusFlag.AuthNoPassword) { client.WriteLine("\r\n[Successfully connected to the server!]"); read(server, connection, client); write(server, connection, client); } else if (server.AuthRequired) { do { if (!client.IsAuth && client.Status == Connection.Client.StatusFlag.Connected) { client.AskAuth("\r\n[Connected to the server]\r\n > Please insert the password\r\n"); } else if (client.Status == Connection.Client.StatusFlag.AuthWrongPassword) { client.AskAuth("\r\n[Wrong password!]\r\n > Please insert the password\r\n"); } } while (!client.IsAuth && client.Status != Connection.Client.StatusFlag.AuthTimeout && client.Status != Connection.Client.StatusFlag.UnknownOrDisconnected); if (client.Status == Connection.Client.StatusFlag.AuthPassword) { Console.WriteLine("\r\n[Auth Success]\r\n"); client.WriteLine("\r\n[Auth Success]\r\n"); read(server, connection, client); write(server, connection, client); } else if (client.Status == Connection.Client.StatusFlag.AuthTimeout) { client.WriteLine("\r\n[Disconnected from the server because of timeout]\r\n"); Console.WriteLine("\r\n[Auth failed! - Timeout]\r\n"); client.Disconnect(); goto Start; } } // We block the main thread while the client is successfully connected while (client.Connected) { Thread.Sleep(100); } if (client.Status == Connection.Client.StatusFlag.UnknownOrDisconnected) { Console.WriteLine("\r\n[Client disconnected!]\r\n"); goto Start; } }