示例#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);
        }
示例#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);
        }
示例#3
0
 public int Send(XPloitSocketClient cl, params IXPloitSocketMsg[] msg)
 {
     if (cl == null)
     {
         return(0);
     }
     return(cl.Send(msg));
 }
示例#4
0
        void s_OnMessage2(XPloitSocket sender, XPloitSocketClient cl, IXPloitSocketMsg msg)
        {
            // Server receive msg
            XPloitMsgString msgS = (XPloitMsgString)msg;

            //XPloitTelnetProtocol.Send(cl, XPloitTelnetProtocol.GetColorMessage());
            //XPloitTelnetProtocol.Send(cl, new byte[] { 255, 247 });
            XPloitTelnetProtocol.Send(cl, "Received: " + msgS.Data + Environment.NewLine);

            //isover = true;
        }
示例#5
0
 void RaiseOnDisconnect(XPloitSocketClient cl, EDissconnectReason dr)
 {
     if (cl == null)
     {
         return;
     }
     if (OnDisconnect != null)
     {
         OnDisconnect(this, cl, dr);
     }
 }
示例#6
0
 public void RaiseOnMessage(XPloitSocketClient cl, IXPloitSocketMsg msg)
 {
     if (msg == null)
     {
         return;
     }
     if (OnMessage != null)
     {
         OnMessage(this, cl, msg);
     }
 }
示例#7
0
        bool Read(XPloitSocketClient c)
        {
            IXPloitSocketMsg msg = c.Read();

            if (msg != null)
            {
                RaiseOnMessage(c, msg);
                return(true);
            }

            return(false);
        }
示例#8
0
        bool Start(bool reconnect)
        {
            if (!reconnect)
            {
                Stop(false);
            }
            try
            {
                lock (this)
                {
                    if (IsServer)
                    {
                        _Server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                        _Server.ExclusiveAddressUse = true;
                        _Server.NoDelay             = true;
                        _Server.Bind(_IPEndPoint);
                        _Server.Listen(50);
                        //_t1.Start();

                        _Thread      = new Thread(new ParameterizedThreadStart(server_thread));
                        _Thread.Name = "SOCKET-SERVER";
                    }
                    else
                    {
                        Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                        s.Connect(_IPEndPoint);

                        _Client      = new XPloitSocketClient(_Protocol, s, _UseSpeedLimit);
                        _Thread      = new Thread(new ParameterizedThreadStart(noserver_thread));
                        _Thread.Name = "SOCKET-CLIENT";

                        if (!Add(_Client))
                        {
                            throw (new ProtocolException());
                        }
                    }

                    _Thread.SetApartmentState(ApartmentState.MTA);
                    _Thread.IsBackground = true;
                    _Thread.Start(this);
                    if (OnStart != null)
                    {
                        OnStart(this, null);
                    }
                }
                return(true);
            }
            catch { }
            Stop(reconnect);
            return(false);
        }
示例#9
0
        bool RaiseOnTimeOut(XPloitSocketClient cl)
        {
            if (OnOverrideTimeout != null)
            {
                CancelEventArgs cn = new CancelEventArgs(false);
                OnOverrideTimeout(this, cl, cn);

                if (cn.Cancel)
                {
                    return(true);
                }
            }
            return(false);
        }
示例#10
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);
        }
示例#11
0
        static void server_thread(object o)
        {
            XPloitSocket cs   = (XPloitSocket)o;
            DateTime     time = DateTime.Now;

            bool          check_time_out = cs._TimeOut != TimeSpan.Zero;
            AsyncCallback callme         = new AsyncCallback(cs.socket_acept);

            cs._Server.BeginAccept(callme, cs);

            while (!cs._IsStopping)
            {
                //lock (cs._Clients) ya hace el lock el añadido
                //{
                for (int x = cs._Clients.Length - 1; x >= 0; x--)
                {
                    XPloitSocketClient c = cs._Clients[x];

                    //lectura sincrona en este hilo
                    if (!cs.Read(c) && check_time_out && c.HasTimeOut)
                    {
                        if (DateTime.Now - c.LastRead > cs._TimeOut && !cs.RaiseOnTimeOut(c))
                        {
                            cs.Remove(c, EDissconnectReason.TimeOut);
                            continue;
                        }
                    }

                    if (!c.IsConnected)
                    {
                        cs.Remove(c, c.DisconnectReason);
                    }
                    //}
                }
                Thread.Sleep(0);
            }
        }
示例#12
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);
        }
示例#13
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);
            }
        }
示例#14
0
        void _Socket_OnMessage(XPloitSocket sender, XPloitSocketClient client, IXPloitSocketMsg msg)
        {

        }
示例#15
0
 void _Socket_OnDisconnect(XPloitSocket sender, XPloitSocketClient client, EDissconnectReason e)
 {
     
 }
示例#16
0
 void _Socket_OnConnect(XPloitSocket sender, XPloitSocketClient client)
 {
 }
示例#17
0
        bool RaiseOnTimeOut(XPloitSocketClient cl)
        {
            if (OnOverrideTimeout != null)
            {
                CancelEventArgs cn = new CancelEventArgs(false);
                OnOverrideTimeout(this, cl, cn);

                if (cn.Cancel) return true;
            }
            return false;
        }
示例#18
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);
        }
示例#19
0
 public void RaiseOnMessage(XPloitSocketClient cl, IXPloitSocketMsg msg)
 {
     if (msg == null) return;
     if (OnMessage != null) OnMessage(this, cl, msg);
 }
示例#20
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;
        }
示例#21
0
 public int Send(XPloitSocketClient cl, params  IXPloitSocketMsg[] msg)
 {
     if (cl == null) return 0;
     return cl.Send(msg);
 }
示例#22
0
        bool Start(bool reconnect)
        {
            if (!reconnect) Stop(false);
            try
            {
                lock (this)
                {
                    if (IsServer)
                    {
                        _Server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                        _Server.ExclusiveAddressUse = true;
                        _Server.NoDelay = true;
                        _Server.Bind(_IPEndPoint);
                        _Server.Listen(50);
                        //_t1.Start();

                        _Thread = new Thread(new ParameterizedThreadStart(server_thread));
                        _Thread.Name = "SOCKET-SERVER";
                    }
                    else
                    {
                        Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                        s.Connect(_IPEndPoint);

                        _Client = new XPloitSocketClient(_Protocol, s, _UseSpeedLimit);
                        _Thread = new Thread(new ParameterizedThreadStart(noserver_thread));
                        _Thread.Name = "SOCKET-CLIENT";

                        if (!Add(_Client)) throw (new ProtocolException());
                    }

                    _Thread.SetApartmentState(ApartmentState.MTA);
                    _Thread.IsBackground = true;
                    _Thread.Start(this);
                    if (OnStart != null) OnStart(this, null);
                }
                return true;
            }
            catch { }
            Stop(reconnect);
            return false;
        }
示例#23
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);
        }
示例#24
0
 void RaiseOnDisconnect(XPloitSocketClient cl, EDissconnectReason dr)
 {
     if (cl == null) return;
     if (OnDisconnect != null) OnDisconnect(this, cl, dr);
 }
示例#25
0
 void client_OnMessage(XPloitSocket sender, XPloitSocketClient cl, IXPloitSocketMsg msg)
 {
     // Client receive message
     cl.Send(new XPloitMsgLogin() { Domain = "?", User = "******", Password = "******", InResponseTo = msg.Id });
 }
示例#26
0
        bool Read(XPloitSocketClient c)
        {
            IXPloitSocketMsg msg = c.Read();

            if (msg != null)
            {
                RaiseOnMessage(c, msg);
                return true;
            }

            return false;
        }
示例#27
0
 void s_OnMessage(XPloitSocket sender, XPloitSocketClient cl, IXPloitSocketMsg msg)
 {
     // Server receive msg
     isover = true;
 }
示例#28
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);
            }
        }
示例#29
0
 void s_OnConnect2(XPloitSocket sender, XPloitSocketClient cl)
 {
     //XPloitTelnetProtocol.Send(cl, new byte[] { 255, 247 });
     //XPloitTelnetProtocol.Send(cl, XPloitTelnetProtocol.GetColorMessage());
     XPloitTelnetProtocol.Send(cl, "server to client" + Environment.NewLine);
 }
示例#30
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); }
        }