/// <summary> /// Thread function that actually run the server socket work. /// </summary> private void MainThread() { try { while (Enabled == true) { TcpClient client = _socket.AcceptTcpClient(); CancelArgs args = new CancelArgs(false); ClientS cl = CreateClient(client); if (OnClientBeforeConnect != null) { OnClientBeforeConnect(this, cl, args); } if (args.Cancel != true) { lock (_clients) { _clients.Add(cl); } ASCIIEncoding ae = new ASCIIEncoding(); byte[] arr = ae.GetBytes("CONNECTEDTCPSERVER"); cl.Send(new Data(arr)); if (OnClientAfterConnect != null) { OnClientAfterConnect(this, cl); } cl.Start(); } else { client.GetStream().Close(); client.Close(); } } } catch (SocketException) { Enabled = false; } }
/// <summary> /// Overridable function that create the structure to memorize the client data. /// </summary> /// <param name="socket">Socket of the client.</param> /// <returns>The structure in which memorize all the information of the client.</returns> protected virtual ClientS CreateClient(TcpClient socket) { ClientS cl = new ClientS(this, socket); return cl; }
/// <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 OnClientBeforeDisconnected event. /// </summary> /// <param name="cl">Client that raised the event.</param> internal void RaiseClientBeforeDisconnectedEvent(ClientS cl) { if (OnClientBeforeDisconnected != null) OnClientBeforeDisconnected(this, cl); }
/// <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> /// 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> /// 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> /// 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> /// Kick the client if the server reached the maximum allowed number of clients. /// </summary> /// <param name="server">Server raising the event.</param> /// <param name="client">Client connecting to the server.</param> /// <param name="args">Specify if the client should be accepted into the server.</param> void BaseMessageServer_OnClientBeforeConnect(Server server, ClientS client, CancelArgs args) { if ((Clients.Count >= UserLimit) && (UserLimit != 0)) { TCPMessage msg = new TCPMessage(); msg.MessageType = MessageType.MaxUsers; ((BaseMessageClientS)client).Send(msg); args.Cancel = true; } }
/// <summary> /// Raise the OnClientBeforeMessageSent event. /// </summary> /// <param name="cl">Client that raised the event.</param> /// <param name="msg">Message to be sent.</param> internal void RaiseBeforeMessageSentEvent(ClientS cl, TCPMessage msg) { if (OnClientBeforeMessageSent != null) OnClientBeforeMessageSent(this, (BaseMessageClientS)cl, msg); }
/// <summary> /// Overridable function that create the structure to memorize the client data. /// </summary> /// <param name="socket">Socket of the client.</param> /// <returns>The structure in which memorize all the information of the client.</returns> protected virtual ClientS CreateClient(TcpClient socket) { ClientS cl = new ClientS(this, socket); return(cl); }