示例#1
0
        public void Start(string protocolName, IPAddress iPAddress, int port)
        {
            log.InfoFormat("{0}/Start:", _fileNm);

            this.ProtocolName = protocolName;
            this.Port         = port;


            log.DebugFormat("{0}/Start: ProtocolName: {1}, Port: {2}", _fileNm, this.ProtocolName, this.Port);

            //Type.GetType("Tracker.Protocol.***Protocol, Tracker.Protocol");
            Type t = Type.GetType("Tracker.Parser." + this.ProtocolName + "Protocol, Tracker.Protocol");

            if (t != null)
            {
                log.InfoFormat("{0}/Start: Known ProtocolName", _fileNm);
                this.ProtocolParser = (Tracker.Parser.Protocol)Activator.CreateInstance(t);
            }
            else
            {
                log.InfoFormat("{0}/Start: Unknown ProtocolName", _fileNm);
                this.ProtocolParser = new Tracker.Parser.UnknownProtocol();
            }
            // Create a TCP/IP socket.
            try
            {
                listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                listener.Bind(new IPEndPoint(iPAddress, port));
                listener.Listen(100);

                ProtocolServerData.SaveData(this.ProtocolName, this.Port, "STARTED", string.Format("Started for: {0}", this.ProtocolParser.GetType().FullName));

                while (isListening)
                {
                    // Set the event to nonsignaled state.
                    allDone.Reset();

                    // Start an asynchronous socket to listen for connections.
                    log.InfoFormat("{0}/Start: {1}:{2} Waiting for a connection...", _fileNm, this.ProtocolName, this.Port);
                    Console.WriteLine("Waiting for a connection...");
                    listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);

                    // Wait until a connection is made before continuing.
                    allDone.WaitOne();
                }

                // https://msdn.microsoft.com/en-us/library/system.net.sockets.socket.begindisconnect(v=vs.110).aspx
                // Release the socket.

                // Will prevent from connecting new client
                listener.Close(0);

                log.DebugFormat("{0}/Start: Server stop command executed", _fileNm);

                ProtocolServerData.SaveData(this.ProtocolName, this.Port, "CLOSED", null);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                log.ErrorFormat("/Start: {0}", ex);
                ProtocolServerData.SaveData(this.ProtocolName, this.Port, "ERROR", ex.Message + ex.StackTrace);
            }
        }
示例#2
0
        public void Start(string protocolName, IPAddress iPAddress, int port)
        {
            log.InfoFormat("{0}/Start:", _fileNm);

            this.ProtocolName = protocolName;
            this.Port = port;

            log.DebugFormat("{0}/Start: ProtocolName: {1}, Port: {2}", _fileNm, this.ProtocolName, this.Port);

            //Type.GetType("Tracker.Protocol.***Protocol, Tracker.Protocol");
            Type t = Type.GetType("Tracker.Parser." + this.ProtocolName + "Protocol, Tracker.Protocol");
            if (t != null)
            {
                log.InfoFormat("{0}/Start: Known ProtocolName", _fileNm);
                this.ProtocolParser = (Tracker.Parser.Protocol)Activator.CreateInstance(t);
            }
            else
            {
                log.InfoFormat("{0}/Start: Unknown ProtocolName", _fileNm);
                this.ProtocolParser = new Tracker.Parser.UnknownProtocol();
            }
            // Create a TCP/IP socket.
            try
            {
                listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                listener.Bind(new IPEndPoint(iPAddress, port));
                listener.Listen(100);

                ProtocolServerData.SaveData(this.ProtocolName, this.Port, "STARTED", string.Format("Started for: {0}", this.ProtocolParser.GetType().FullName));

                while (isListening)
                {
                    // Set the event to nonsignaled state.
                    allDone.Reset();

                    // Start an asynchronous socket to listen for connections.
                    log.InfoFormat("{0}/Start: {1}:{2} Waiting for a connection...", _fileNm, this.ProtocolName, this.Port);
                    Console.WriteLine("Waiting for a connection...");
                    listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);

                    // Wait until a connection is made before continuing.
                    allDone.WaitOne();
                }

                // https://msdn.microsoft.com/en-us/library/system.net.sockets.socket.begindisconnect(v=vs.110).aspx
                // Release the socket.

                // Will prevent from connecting new client
                listener.Close(0);

                log.DebugFormat("{0}/Start: Server stop command executed", _fileNm);

                ProtocolServerData.SaveData(this.ProtocolName, this.Port, "CLOSED", null);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                log.ErrorFormat("/Start: {0}", ex);
                ProtocolServerData.SaveData(this.ProtocolName, this.Port, "ERROR", ex.Message + ex.StackTrace);
            }
        }