Process theProcess; //for testing only

        #endregion Fields

        #region Constructors

        //__END variables for real app____________________________________________
        //_______________________________________________________________________________
        // Constructor.
        public SocketListener(SocketListenerSettings theSocketListenerSettings)
        {
            if (Program.watchProgramFlow == true)   //for testing
            {
                Program.testWriter.WriteLine("SocketListener constructor");
            }
            if (Program.watchThreads == true)   //for testing
            {
                theProcess = Process.GetCurrentProcess(); //for testing only
                DealWithThreadsForTesting("constructor");
            }

            this.numberOfAcceptedSockets = 0; //for testing
            this.socketListenerSettings = theSocketListenerSettings;
            this.prefixHandler = new PrefixHandler();
            this.messageHandler = new MessageHandler();

            //Allocate memory for buffers. We are using a separate buffer space for
            //receive and send, instead of sharing the buffer space, like the Microsoft
            //example does.
            this.theBufferManager = new BufferManager(this.socketListenerSettings.BufferSize * this.socketListenerSettings.NumberOfSaeaForRecSend * this.socketListenerSettings.OpsToPreAllocate,
            this.socketListenerSettings.BufferSize * this.socketListenerSettings.OpsToPreAllocate);

            this.poolOfRecSendEventArgs = new SocketAsyncEventArgsPool(this.socketListenerSettings.NumberOfSaeaForRecSend);
            this.poolOfAcceptEventArgs = new SocketAsyncEventArgsPool(this.socketListenerSettings.MaxAcceptOps);

            // Create connections count enforcer
            this.theMaxConnectionsEnforcer = new Semaphore(this.socketListenerSettings.MaxConnections, this.socketListenerSettings.MaxConnections);

            //Microsoft's example called these from Main method, which you
            //can easily do if you wish.
            Init();
            StartListen();
        }
Beispiel #2
0
        public SocketListener(string ipAddress, int port, int maxConnections, int receiveBufferSize)
        {
            var socketSetting = SocketListenerSettings.CreateSetting(ipAddress, port, maxConnections, receiveBufferSize);

            this.numberOfAcceptedSockets = 0; //for testing
            this.socketListenerSettings  = socketSetting;
            this.prefixHandler           = new PrefixHandler();
            this.messageHandler          = new MessageHandler();

            //Allocate memory for buffers. We are using a separate buffer space for
            //receive and send, instead of sharing the buffer space, like the Microsoft
            //example does.
            this.theBufferManager = new BufferManager(this.socketListenerSettings.BufferSize * this.socketListenerSettings.NumberOfSaeaForRecSend * this.socketListenerSettings.OpsToPreAllocate,
                                                      this.socketListenerSettings.BufferSize * this.socketListenerSettings.OpsToPreAllocate);

            this.poolOfRecSendEventArgs = new SocketAsyncEventArgsPool(this.socketListenerSettings.NumberOfSaeaForRecSend);
            this.poolOfAcceptEventArgs  = new SocketAsyncEventArgsPool(this.socketListenerSettings.MaxAcceptOps);

            // Create connections count enforcer
            this.theMaxConnectionsEnforcer = new Semaphore(this.socketListenerSettings.MaxConnections, this.socketListenerSettings.MaxConnections);

            //Microsoft's example called these from Main method, which you
            //can easily do if you wish.
            Init();
            StartListen();
        }
Beispiel #3
0
        static void Main(String[] args)
        {
            // Just used to calculate # of received transmissions at the end.
            startingTid = mainTransMissionId;

            // Before the app starts, let's build strings for you to use the console.
            BuildStringsForServerConsole();

            // Create List<T> to hold data, unless we are running a long test, which
            // would create too much data to store in a list.
            if (runLongTest == false)
            {
                listOfDataHolders = new List <DataHolder>();
            }

            //Create a log file writer, so you can see the flow easily.
            //It can be printed. Makes it easier to figure out complex program flow.
            //The log StreamWriter uses a buffer. So it will only work right if you close
            //the server console properly at the end of the test.
            testWriter = new TestFileWriter();

            try
            {
                // Get endpoint for the listener.
                IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, port);

                WriteInfoToConsole(localEndPoint);

                //This object holds a lot of settings that we pass from Main method
                //to the SocketListener. In a real app, you might want to read
                //these settings from a database or windows registry settings that
                //you would create.
                SocketListenerSettings theSocketListenerSettings = new SocketListenerSettings
                                                                       (maxNumberOfConnections, excessSaeaObjectsInPool, backlog, maxSimultaneousAcceptOps, receivePrefixLength, testBufferSize, sendPrefixLength, opsToPreAlloc, localEndPoint);

                //instantiate the SocketListener.
                SocketListener socketListener = new SocketListener(theSocketListenerSettings);

                ManageClosing(socketListener);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }
            finally
            {
                // close the stream for test file writing
                try
                {
                    testWriter.Close();
                }
                catch
                {
                    Console.WriteLine("Could not close log properly.");
                }
            }
        }
Beispiel #4
0
        //__END variables for real app____________________________________________
        
        //_______________________________________________________________________________
        // Constructor.
        public SocketListener(SocketListenerSettings theSocketListenerSettings)        
        {                        
            this.numberOfAcceptedSockets = 0; //for testing
            this.socketListenerSettings = theSocketListenerSettings;
            this.prefixHandler = new PrefixHandler();
            this.messageHandler = new MessageHandler();
                        
            //Allocate memory for buffers. We are using a separate buffer space for
            //receive and send, instead of sharing the buffer space, like the Microsoft
            //example does.            
            this.theBufferManager = new BufferManager(this.socketListenerSettings.BufferSize * this.socketListenerSettings.NumberOfSaeaForRecSend * this.socketListenerSettings.OpsToPreAllocate,
            this.socketListenerSettings.BufferSize * this.socketListenerSettings.OpsToPreAllocate);

            this.poolOfRecSendEventArgs = new SocketAsyncEventArgsPool(this.socketListenerSettings.NumberOfSaeaForRecSend);
            this.poolOfAcceptEventArgs = new SocketAsyncEventArgsPool(this.socketListenerSettings.MaxAcceptOps);     
            
            // Create connections count enforcer
            this.theMaxConnectionsEnforcer = new Semaphore(this.socketListenerSettings.MaxConnections, this.socketListenerSettings.MaxConnections);

            //Microsoft's example called these from Main method, which you 
            //can easily do if you wish.
            Init();
            StartListen();
        }
Beispiel #5
0
        private void StartAsyncTcpService()
        {
            Log.Write("开始启动异步TCP服务:");
            IPAddress ipd = IPAddress.Any;
            if (Configs.Address.ToUpper() != "ANY")
                ipd = IPAddress.Parse(Configs.Address);
            IPEndPoint localEndPoint = new IPEndPoint(ipd, Configs.Port);
            //This object holds a lot of settings that we pass from Main method
            //to the SocketListener. In a real app, you might want to read
            //these settings from a database or windows registry settings that
            //you would create.
            SocketListenerSettings theSocketListenerSettings = new SocketListenerSettings
            (Configs.MaxNumberOfConnections, Configs.ExcessSaeaObjectsInPool, Configs.Backlog, Configs.MaxSimultaneousAcceptOps
            , Configs.ReceivePrefixLength, Configs.ReceiveBufferSize, Configs.sendPrefixLength, Configs.opsToPreAlloc, localEndPoint);

            //instantiate the SocketListener.
            SocketListener sl = new SocketListener(theSocketListenerSettings);
            sl.DataReceived += Socket_DataReceived;
            Log.Write("异步TCP服务启动成功:");
        }
Beispiel #6
0
        //   public static     ILogger Logger = new TestFileWriter();
        static void Main(String[] args)
        {
            // Just used to calculate # of received transmissions at the end.
            startingTid = mainTransMissionId;

            // Before the app starts, let's build strings for you to use the console.
            BuildStringsForServerConsole();

            // Create List<T> to hold data, unless we are running a long test, which
            // would create too much data to store in a list.
            if (runLongTest == false)
            {
                listOfDataHolders = new List<DataHolder>();
            }

            //Create a log file writer, so you can see the flow easily.
            //It can be printed. Makes it easier to figure out complex program flow.
            //The log StreamWriter uses a buffer. So it will only work right if you close
            //the server console properly at the end of the test.
            testWriter = new TestFileWriter();

            try
            {
                // Get endpoint for the listener.
                var localEndPoint = new IPEndPoint(IPAddress.Any, port);

                WriteInfoToConsole(localEndPoint);

                //This object holds a lot of settings that we pass from Main method
                //to the SocketListener. In a real app, you might want to read
                //these settings from a database or windows registry settings that
                //you would create.
                var theSocketListenerSettings = new SocketListenerSettings
            (maxNumberOfConnections, excessSaeaObjectsInPool, backlog, maxSimultaneousAcceptOps, receivePrefixLength, testBufferSize, sendPrefixLength, opsToPreAlloc, localEndPoint, true, true);

                //instantiate the SocketListener.
                var socketListener = new SocketListener(theSocketListenerSettings, testWriter);
                socketListener.Init();
                socketListener.StartListen();
                ManageClosing(socketListener);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }
            finally
            {
                // close the stream for test file writing
                try
                {
                    testWriter.Close();
                }
                catch
                {
                    Console.WriteLine("Could not close log properly.");
                }
            }
        }