Exemple #1
0
        /// <summary>
        /// Add client
        /// </summary>
        /// <param name="cl">Client</param>
        bool Add(XPloitSocketClient cl)
        {
            if (cl == null)
            {
                return(false);
            }

            cl.OnDisconnect += RaiseOnDisconnect;
            //cl.OnMessage += RaiseOnMessage;

            if (!_Protocol.Connect(cl))
            {
                cl.Disconnect(EDissconnectReason.Protocol);
                return(false);
            }

            lock (_Clients)
            {
                int l = _Clients.Length;
                Array.Resize(ref _Clients, l + 1);
                _Clients[l] = cl;
            }

            if (OnConnect != null)
            {
                OnConnect(this, cl);
            }
            return(true);
        }
Exemple #2
0
        static void noserver_thread(object o)
        {
            XPloitSocket       cs = (XPloitSocket)o;
            XPloitSocketClient c  = cs._Client;
            bool checkTimeOut     = cs._TimeOut != TimeSpan.Zero;

            try
            {
                while (!cs._IsStopping && c.IsConnected)
                {
                    //lectura sincrona en este hilo
                    if (!cs.Read(c) && checkTimeOut && c.HasTimeOut)
                    {
                        if (DateTime.Now - c.LastRead > cs._TimeOut && !cs.RaiseOnTimeOut(c))
                        {
                            c.Disconnect(EDissconnectReason.TimeOut);
                            break;
                        }
                    }
                    Thread.Sleep(0);
                }
            }
            catch { }

            cs.Remove(c, c.DisconnectReason);
            cs.Stop(true);
        }
Exemple #3
0
        void socket_acept(IAsyncResult ar)
        {
            Socket hold = ((XPloitSocket)ar.AsyncState)._Server;

            if (!Enable)
            {
                return;
            }

            if (hold != null /*&& hold.Connected*/)
            {
                try
                {
                    Socket work = hold.EndAccept(ar);
                    if (work.Connected)
                    {
                        XPloitSocketClient tc = new XPloitSocketClient(_Protocol, work, _UseSpeedLimit);

                        if (_IPFilter != null && !_IPFilter.IsAllowed(tc.IPAddress))
                        {
                            tc.Disconnect(EDissconnectReason.Banned);
                        }
                        else
                        if (Connections >= _MaxConnections)
                        {
                            tc.Disconnect(EDissconnectReason.MaxAllowed);
                        }
                        else
                        {
                            Add(tc);
                        }
                    }
                }
                catch { }
            }
            if (_Server == null)
            {
                return;
            }

            AsyncCallback callme = new AsyncCallback(socket_acept);

            _Server.BeginAccept(callme, this);
        }
Exemple #4
0
        /// <summary>
        /// En caso de servidor se permite conectar como cliente a otro servidor y que la lógica no cambie
        /// </summary>
        /// <param name="host">Host de conexión</param>
        /// <param name="prto">Puerto de conexión</param>
        /// <returns>Devuelve true si es capaz de conectar al server</returns>
        public bool ConnectClient(IPAddress ip, int prto)
        {
            if (!IsServer)
            {
                throw (new Exception("ONLY USE Y IN SERVER MODE"));
            }
            if (_IPFilter != null && !_IPFilter.IsAllowed(ip))
            {
                return(false);
            }
            if (Connections >= _MaxConnections)
            {
                return(false);
            }

            XPloitSocketClient cl  = null;
            Socket             cl2 = null;

            try
            {
                cl2 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                cl2.Connect(ip, prto);
                return(Add(new XPloitSocketClient(_Protocol, cl2, _UseSpeedLimit)));
            }
            catch
            {
                if (cl2 != null)
                {
                    cl2.Close();
                }
                if (cl != null)
                {
                    cl.Disconnect(EDissconnectReason.Error);
                }
            }
            return(false);
        }
Exemple #5
0
        void Remove(XPloitSocketClient c, EDissconnectReason dr)
        {
            lock (_Clients)
            {
                int l = _Clients.Length;
                XPloitSocketClient[] cs = new XPloitSocketClient[l - 1];
                for (int x = 0, y = 0; x < l; x++)
                {
                    XPloitSocketClient o = _Clients[x];
                    if (o == c)
                    {
                        continue;
                    }

                    cs[y] = o;
                    y++;
                }
                _Clients = cs;
            }
            if (c != null)
            {
                c.Disconnect(dr);
            }
        }
Exemple #6
0
        public void Stop(bool reconnect, bool raiseEvent)
        {
            lock (this)
            {
                if (reconnect && !_AutoReconnect)
                {
                    reconnect = false;
                }

                if (_Thread != null)
                {
                    if (raiseEvent && OnStop != null)
                    {
                        OnStop(this, null);
                    }

                    _IsStopping = true;
                    foreach (XPloitSocketClient c in _Clients)
                    {
                        c.Disconnect(EDissconnectReason.Requested);
                    }

                    if (_IsServer)
                    {
                        if (_Thread != null)
                        {
                            if (_Thread.IsAlive)
                            {
                                _Thread.Join(new TimeSpan(0, 0, 0, 0, 3));
                            }
                            //if (_t.IsAlive) _t.Abort();
                            _Thread = null;
                        }
                    }
                    else
                    {
                        if (!reconnect && _Thread != null)
                        {
                            lock (_Thread)
                            {
                                if (_Thread.IsAlive)
                                {
                                    _Thread.Join(new TimeSpan(0, 0, 0, 0, 3));
                                }
                                //if (_t.IsAlive) _t.Abort();
                                _Thread = null;
                            }
                        }
                    }
                    _IsStopping = false;
                }
                if (_Server != null)
                {
                    _Server.Close(); _Server = null;
                }
                if (_Client != null)
                {
                    _Client.Disconnect(EDissconnectReason.Requested); _Client = null;
                }
            }
            if (reconnect)
            {
                Thread.Sleep(1000); Start(reconnect);
            }
        }
Exemple #7
0
        void Remove(XPloitSocketClient c, EDissconnectReason dr)
        {
            lock (_Clients)
            {
                int l = _Clients.Length;
                XPloitSocketClient[] cs = new XPloitSocketClient[l - 1];
                for (int x = 0, y = 0; x < l; x++)
                {
                    XPloitSocketClient o = _Clients[x];
                    if (o == c) continue;

                    cs[y] = o;
                    y++;
                }
                _Clients = cs;
            }
            if (c != null) c.Disconnect(dr);
        }
Exemple #8
0
        /// <summary>
        /// Add client
        /// </summary>
        /// <param name="cl">Client</param>
        bool Add(XPloitSocketClient cl)
        {
            if (cl == null) return false;

            cl.OnDisconnect += RaiseOnDisconnect;
            //cl.OnMessage += RaiseOnMessage;

            if (!_Protocol.Connect(cl))
            {
                cl.Disconnect(EDissconnectReason.Protocol);
                return false;
            }

            lock (_Clients)
            {
                int l = _Clients.Length;
                Array.Resize(ref _Clients, l + 1);
                _Clients[l] = cl;
            }

            if (OnConnect != null) OnConnect(this, cl);
            return true;
        }
Exemple #9
0
        void socket_acept(IAsyncResult ar)
        {
            Socket hold = ((XPloitSocket)ar.AsyncState)._Server;
            if (!Enable) return;

            if (hold != null /*&& hold.Connected*/)
            {
                try
                {
                    Socket work = hold.EndAccept(ar);
                    if (work.Connected)
                    {
                        XPloitSocketClient tc = new XPloitSocketClient(_Protocol, work, _UseSpeedLimit);

                        if (_IPFilter != null && !_IPFilter.IsAllowed(tc.IPAddress)) tc.Disconnect(EDissconnectReason.Banned);
                        else
                            if (Connections >= _MaxConnections) tc.Disconnect(EDissconnectReason.MaxAllowed);
                            else Add(tc);
                    }
                }
                catch { }
            }
            if (_Server == null) return;

            AsyncCallback callme = new AsyncCallback(socket_acept);
            _Server.BeginAccept(callme, this);
        }