Ejemplo n.º 1
0
        /// <summary>
        /// Send a message to all connected tcp clients
        /// </summary>
        /// <param name="data"></param>
        public void Send(string IdConection, string data)
        {
            try
            {
                lock (this.sem)
                {
                    TcpServersConnection connection = this.connections.FirstOrDefault(x => x.IdConection == IdConection);

                    if (connection != null)
                    {
                        connection.sendData(data);
                    }

                    Thread.Yield();

                    if (this.waiting)
                    {
                        this.sem.Release();
                        this.waiting = false;
                    }
                }
            }
            catch (Exception ex)
            {
                OnError?.Invoke(this, ex);
            }
        }
Ejemplo n.º 2
0
        private void runListener()
        {
            while (m_isOpen && (m_port >= 0))
            {
                try
                {
                    if (this.listener.Pending())
                    {
                        TcpClient            sock = listener.AcceptTcpClient();
                        TcpServersConnection conn = new TcpServersConnection(sock, m_encoding);

                        if (connections.Count < m_maxConexionClients)
                        {
                            lock (connections)
                            {
                                conn.m_IdConection = Guid.NewGuid().ToString();
                                connections.Add(conn);
                            }

                            if (OnConnect != null)
                            {
                                lock (activeThreadsLock)
                                {
                                    activeThreads++;
                                }

                                OnConnect?.Invoke(conn);
                            }

                            OnConnectMultiPort?.Invoke(this, conn);

                            continue;
                        }
                        else
                        {
                            byte[] outStream = Encoding.ASCII.GetBytes("$MaxConexionClients$");
                            conn.Socket.Client.Send(outStream);

                            this.OnError?.Invoke(this, new Exception("MaxConexionClients"));

                            conn.forceDisconnect();
                        }
                    }

                    Thread.Sleep(m_idleTime);
                    continue;
                }
                catch (ThreadInterruptedException)
                {
                    continue;
                }
                catch (Exception ex)
                {
                    if (m_isOpen && (OnError != null))
                    {
                        OnError(this, ex);
                    }

                    continue;
                }
            }
        }
Ejemplo n.º 3
0
        private bool processConnection(TcpServersConnection conn)
        {
            ThreadStart start = null;
            bool        flag  = false;

            if (conn.processOutgoing(this.m_maxSendAttempts))
            {
                flag = true;
            }

            if (((OnData != null || OnMessage != null) && (this.activeThreads < this.m_maxCallbackThreads)) && (conn.Socket.Available > 0))
            {
                lock (this.activeThreadsLock)
                {
                    this.activeThreads++;
                }

                if (start == null)
                {
                    start = delegate
                    {
                        byte[] data    = Methods.ReadStream(conn.Socket, conn.Socket.ReceiveBufferSize);
                        string Message = Encoding.ASCII.GetString(data, 0, data.Length);

                        if (data != null)
                        {
                            if (conn.To != null)
                            {
                                List <TcpServersConnection> list_connection = connections.Where(x => x.From == conn.To).ToList();

                                if (list_connection != null)
                                {
                                    foreach (TcpServersConnection connection in list_connection)
                                    {
                                        connection.sendData(data);
                                    }
                                }
                                else
                                {
                                    OnError?.Invoke(this, new Exception(string.Format("The recipient {0} is not found.", conn.To)));
                                }
                            }

                            if (this.OnData != null && ((SnifferMode && conn.SnifferServer) || conn.To == null))
                            {
                                try
                                {
                                    OnData.Invoke(conn, data);
                                }
                                catch (Exception ex)
                                {
                                    OnError?.Invoke(this, ex);
                                }
                            }

                            if (this.OnMessage != null && (SnifferMode || conn.To == null))
                            {
                                try
                                {
                                    OnMessage.Invoke(conn, Message);
                                }
                                catch (Exception ex)
                                {
                                    OnError?.Invoke(this, ex);
                                }
                            }
                        }
                        else
                        {
                            OnError?.Invoke(this, new Exception("processConnection-Empty-Message"));
                        }
                    };
                }

                conn.CallbackThread = new Thread(start);
                conn.CallbackThread.Start();
                Thread.Yield();
            }

            return(flag);
        }