Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
    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;
    }