public bool SendMessage(Message message, bool autoconnect = false) { try { if (!client.Connected && !autoconnect) { return(false); } CheckConnection(); lock (criticalSection) { MemoryStream buffer = new MemoryStream(); message.Write(new BinaryWriter(buffer)); buffer.Flush(); UInt32 length = (UInt32)buffer.Length; NetworkStream stream = client.GetStream(); BinaryWriter writer = new BinaryWriter(stream); writer.Write(Message.MESSAGE_MARK); writer.Write(length); buffer.WriteTo(stream); stream.Flush(); } return(true); } catch (Exception ex) { lock (criticalSection) { Application.Current.Dispatcher.BeginInvoke(new Action(() => { ConnectionChanged?.Invoke(IpAddress, Port, State.Disconnected, ex.Message); })); Reconnect(); } } return(false); }
public bool SendMessage(Message message) { try { CheckConnection(); lock (criticalSection) { MemoryStream buffer = new MemoryStream(); message.Write(new BinaryWriter(buffer)); buffer.Flush(); UInt32 length = (UInt32)buffer.Length; NetworkStream stream = client.GetStream(); BinaryWriter writer = new BinaryWriter(stream); writer.Write(Message.MESSAGE_MARK); writer.Write(length); buffer.WriteTo(stream); stream.Flush(); } return(true); } catch (Exception ex) { if (MessageBox.Show(ex.Message) == MessageBoxResult.OK) { lock (criticalSection) { //client.Client.Shutdown(SocketShutdown.Both); client = new TcpClient(); } } } return(false); }
public bool SendMessage(Message message) { try { CheckConnection(); lock (criticalSection) { MemoryStream buffer = new MemoryStream(); message.Write(new BinaryWriter(buffer)); buffer.Flush(); UInt32 length = (UInt32)buffer.Length; NetworkStream stream = client.GetStream(); BinaryWriter writer = new BinaryWriter(stream); writer.Write(Message.MESSAGE_MARK); writer.Write(length); buffer.WriteTo(stream); stream.Flush(); } return true; } catch (Exception ex) { if (MessageBox.Show(ex.Message) == MessageBoxResult.OK) { lock (criticalSection) { //client.Client.Shutdown(SocketShutdown.Both); client = new TcpClient(); } } } return false; }