/// <summary> /// Raise the OnDataReceived event. /// </summary> /// <param name="cl">Client that raised the event.</param> /// <param name="data">Line of data received.</param> internal void RaiseDataReceivedEvent(ClientS cl, Data data) { if (OnClientDataReceived != null) OnClientDataReceived(this, cl, data); }
/// <summary> /// Raise the OnClientAfterDataSent event. /// </summary> /// <param name="cl">Client that raised the event.</param> /// <param name="data">Line of data sent.</param> internal void RaiseAfterDataSentEvent(ClientS cl, Data data) { if (OnClientAfterDataSent != null) OnClientAfterDataSent(this, cl, data); }
/// <summary> /// Raise the OnClientBeforeDataSent event. /// </summary> /// <param name="cl">Client that raised the event.</param> /// <param name="data">Line of data sent.</param> internal void RaiseBeforeDataSentEvent(ClientS cl, Data data) { if (OnClientBeforeDataSent != null) OnClientBeforeDataSent(this, cl, data); }
/// <summary> /// Trasform the line of data sent into a Message structure and raise /// the event linked. /// </summary> /// <param name="server">Server raising the event.</param> /// <param name="receiver">Client that will receive the Message.</param> /// <param name="Data">Line of data sent.</param> void BaseMessageServer_OnDataSent(Server server, ClientS receiver, Data Data) { TCPMessage msg = null; try { msg = TCPMessage.FromByteArray(Data.Message); } catch (Exception) { } if (msg != null) if (OnClientAfterMessageSent != null) OnClientAfterMessageSent(this, (BaseMessageClientS)receiver, msg); }
/// <summary> /// Broadcast a line of data to all the clients connected to the server. /// </summary> /// <param name="Data">Line of data to be sent.</param> /// <exception cref="TCPLibrary.Core.ServerNonAttivoException" /> public void Broadcast(Data Data) { if (Enabled) { lock (_clients) { foreach (var itm in _clients) if (itm.Connected) itm.Send(Data); } } else throw new ArgumentException("Unable to execute this operation when the server is inactive."); }
/// <summary> /// Trasform the line of data received into a Message structure and raise /// the event linked. /// </summary> /// <param name="server">Server raising the event.</param> /// <param name="sender">Client sending the data.</param> /// <param name="Data">Line of data received.</param> void BaseMessageServer_OnDataReceived(Server server, ClientS sender, Data Data) { TCPMessage msg = null; try { msg = TCPMessage.FromByteArray(Data.Message); } catch (Exception) { } if (msg != null) if (OnClientMessageReceived != null) OnClientMessageReceived(this, (BaseMessageClientS)sender, msg); }
/// <summary> /// Parse the raw data sent to the server and create Message structures. /// </summary> /// <param name="sender">Sender of the event.</param> /// <param name="Data">Line of data sent.</param> void BaseMessageClient_OnDataSent(Client sender, Data Data) { TCPMessage msg = TCPMessage.FromByteArray(Data.Message); if (OnAfterMessageSent != null) OnAfterMessageSent(sender, msg); }
/// <summary> /// Parse the raw data received from the server and create Message structures. /// </summary> /// <param name="sender">Sender of the event.</param> /// <param name="Data">Line of data received.</param> void BaseMessageClient_OnDataReceived(Client sender, Data Data) { TCPMessage msg = null; try { msg = TCPMessage.FromByteArray(Data.Message); } catch (Exception) { } if (msg != null) if (OnMessageReceived != null) OnMessageReceived(sender, msg); }
/// <summary> /// Send a line of data to the server. /// </summary> /// <param name="msg">Data to send to the server.</param> /// <exception cref="TCPLibrary.Core.NotConnectedException" /> public void Send(Data msg) { if (_active) { if (OnBeforeDataSent != null) OnBeforeDataSent(this, msg); try { tw.Write(msg.Message.Length); tw.Write(msg.Message); tw.Flush(); if (OnAfterDataSent != null) OnAfterDataSent(this, msg); } catch (IOException) { // Pensare a cosa fare quà. Questo è il caso in cui il server ha chiuso forzatamente // la connessione mentre il client mandava roba. } } else throw new ArgumentException("The link is closed. Unable to send data."); }
/// <summary> /// Send a line of data to the client. /// </summary> /// <param name="Data">Data to send to the client.</param> /// <exception cref="TCPLibrary.Core.NotConnectedException" /> public void Send(Data Data) { if (_active) { _server.RaiseBeforeDataSentEvent(this, Data); try { tw.Write(Data.Message.Length); tw.Write(Data.Message); tw.Flush(); _server.RaiseAfterDataSentEvent(this, Data); } catch (Exception ex) { Debug.Write(ex.ToString()); // Pensare a cosa fare quà. Questo è il caso in cui il client ha chiuso forzatamente // la connessione mentre il server mandava roba. } } else throw new ArgumentException("The link is closed. Unable to send data."); }