Esempio n. 1
0
        /// <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;
            }
        }
Esempio n. 2
0
 /// <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;
 }
Esempio n. 3
0
 /// <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);
 }
Esempio n. 4
0
 /// <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);
 }
Esempio n. 5
0
 /// <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);
 }
Esempio n. 6
0
 /// <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);
 }
Esempio n. 7
0
        /// <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);
        }
Esempio n. 8
0
        /// <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);
        }
Esempio n. 9
0
        /// <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;
            }
        }
Esempio n. 10
0
 /// <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);
 }
Esempio n. 11
0
        /// <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);
        }