private DateTime SendHeartbeat() { TcpIO.WriteStream(client.GetStream(), new Message() { MessageType = MessageType.Heartbeat }); return(DateTime.Now); }
public void WriteMessage(Message message) { if (State == ConnectionState.Alive || State == ConnectionState.Authenticating) { try { TcpIO.WriteStream(client.GetStream(), message); } catch (Exception e) { //Logger.GetInstance().NewInfoLine($"Failed to send info to: {Name}"); } } }
private Message ReadClient(TcpClient client) { try { Message message = TcpIO.ReadStream(client.GetStream()); //if (message != null) // Logger.GetInstance().NewInfoLine(message.ToDebugString()); return(message); } catch (Exception e) { return(null); } }
public void SendMessage(string message, string receiver, MessageReceiver receiverType) { NetworkStream stream = client.GetStream(); Message msgObject = new Message() { MessageType = MessageType.ChatMessage, ReceiverType = receiverType, ReceiverName = receiver, SenderName = Name, Text = message }; TcpIO.WriteStream(stream, msgObject); }
private bool LogInAs(string name) { try { client = new TcpClient(IP.ToString(), Port); } catch { return(false); } //client.LingerState.Enabled = true; //client.LingerState.LingerTime = 0; //Debug.WriteLine($"Connected: {client.Connected}"); Message logInMessage = new Message() { MessageType = MessageType.LoginInformation, ReceiverType = MessageReceiver.User, ReceiverName = null, SenderName = name, Text = $"My port is: {client.Client.LocalEndPoint.ToString()}" }; TcpIO.WriteStream(client.GetStream(), logInMessage); Message response = null; Debug.WriteLine("Started login..."); Stopwatch waitingForResponse = Stopwatch.StartNew(); while (response == null && waitingForResponse.Elapsed.TotalSeconds < 3) { response = TcpIO.ReadStream(client.GetStream()); Thread.Yield(); } waitingForResponse.Stop(); Debug.WriteLine("Login-response received!"); if (response.MessageType == MessageType.LoginInformation && response.ReceiverName == name) { return(true); } else { return(false); } }
private bool AuthenticateClient(TcpClient client) { Logger.GetInstance().NewInfoLine("Started authenticating new client."); Message login = null; while (login == null) { login = TcpIO.ReadStream(client.GetStream()); Thread.Yield(); } bool loginOK = (login.MessageType == MessageType.LoginInformation) && (!server.HasConnectionWithName(login.SenderName)); Logger.GetInstance().NewInfoLine(login.ToDebugString()); Logger.GetInstance().NewInfoLine($"Authentication ok for {login.SenderName}: {loginOK}"); Message response; if (loginOK) { Name = login.SenderName; aliveUntil = DateTime.Now.AddSeconds(5); response = new Message() { MessageType = MessageType.LoginInformation, ReceiverName = Name }; } else { response = new Message() { MessageType = MessageType.LoginInformation, ReceiverName = null }; } TcpIO.WriteStream(client.GetStream(), response); return(loginOK); }
private void Loop() { NetworkStream stream = client.GetStream(); Message newMessage; DateTime lastHeartbeat = SendHeartbeat(); while (client.Connected && keepListening) { if (client.Connected && stream.DataAvailable && stream.CanRead) { newMessage = TcpIO.ReadStream(stream); ChatData.GetInstance().AddMessage(newMessage); UIController.GetInstance().Refresh(); } if (DateTime.Now.Subtract(lastHeartbeat).TotalSeconds >= 2) { lastHeartbeat = SendHeartbeat(); } Thread.Yield(); } }
public Test() { _channel = new TcpIO(); _channel.DataReceived += OnDataReceived; }