private void CreateAndStartPort(object portOwner, int listeningPort)
        {
            if (!(portOwner is IUseP2PCommunicationsScheme))
            {
                throw (new Exception("The port owner object must implement the interface \'IUseP2PCommunicationsScheme\'"));
            }

            this._portOwnerComponent    = portOwner;
            this._ConnectedClientsTable = new Hashtable();
            this._startDateTime         = DateTime.Now;
            this._statisticsHandler     = new P2PPortStatisticsHandler();
            this._listeningPort         = listeningPort;
            string host = System.Net.Dns.GetHostName();

            this._IPAddress          = CommunicationsLibrary.Utilities.CommunicationsUtilities.GetActiveIPAddress();
            this._connectionendpoint = new IPEndPoint(this._IPAddress, this._listeningPort);

            this._prefixHandler  = new PrefixHandler();
            this._messageHandler = new DataReceptionHandler();


            Int32 totalBytes = P2PNetworkingDefinitions.DATABUFFER_SIZE * NUMBER_OF_SAEA_OBJECTS_FOR_REC_AND_SEND * NUMBER_OF_OPERATIONS_TO_ALLOCATE;
            Int32 totalBufferBytesInEachSaeaObject = P2PNetworkingDefinitions.DATABUFFER_SIZE * NUMBER_OF_OPERATIONS_TO_ALLOCATE;

            this._bufferManager = new BufferManager(totalBytes, totalBufferBytesInEachSaeaObject);

            this._poolOfRecSendEventArgs = new SocketAsyncEventArgsPool(NUMBER_OF_SAEA_OBJECTS_FOR_REC_AND_SEND);
            this._poolOfAcceptEventArgs  = new SocketAsyncEventArgsPool(NUMBER_OF_OPERATIONS_TO_ALLOCATE);

            this._maxConnectionsEnforcer = new Semaphore(MAX_CONNECTIONS, MAX_CONNECTIONS);


            //creation of the pool of SEAE Object used to handle incomming connections
            for (Int32 i = 0; i < MAX_SIMULTANEOUS_ACCEPT_OPS; i++)
            {
                SocketAsyncEventArgs SAEA = CreateNewSaeaForAccept(_poolOfAcceptEventArgs);
                this._poolOfAcceptEventArgs.Push(SAEA);
            }

            //creation of the pool of SEAE objects used for send and receive data operations
            SocketAsyncEventArgs eventArgObjectForPool;

            for (Int32 i = 0; i < NUMBER_OF_SAEA_OBJECTS_FOR_REC_AND_SEND; i++)
            {
                eventArgObjectForPool = new SocketAsyncEventArgs();
                this._bufferManager.SetBuffer(eventArgObjectForPool);
                eventArgObjectForPool.Completed += new EventHandler <SocketAsyncEventArgs>(EventHandling_IO_Completed);
                P2PPortClientConnectionHandler _P2PClientCnnHandler = new P2PPortClientConnectionHandler(eventArgObjectForPool, eventArgObjectForPool.Offset, eventArgObjectForPool.Offset + P2PNetworkingDefinitions.DATABUFFER_SIZE, P2PNetworkingDefinitions.DATA_SEND_RECEIVE_PREFIX_LENGHT, P2PNetworkingDefinitions.DATA_SEND_RECEIVE_PREFIX_LENGHT);
                _P2PClientCnnHandler.CreateNewDataReceivedBuffer(P2PNetworkingDefinitions.DATABUFFER_SIZE);
                eventArgObjectForPool.UserToken = _P2PClientCnnHandler;
                this._poolOfRecSendEventArgs.Push(eventArgObjectForPool);
            }

            //creation of the socker and binging to a listening port
            _listenSocket = new Socket(this._connectionendpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            _listenSocket.Bind(this._connectionendpoint);
            _listenSocket.Listen(BACK_LOG);
            StartAccept();
        }
Exemple #2
0
            // creates th server port  and all the requiered resorces
            private void P2PPortCreation(object portOwner, int listeningPort)
            {
                if (!(portOwner is IUseP2PCommunicationsScheme))
                {
                    throw (new Exception("The port owner object must implement the interface \'IUseP2PCommunicationsScheme\'"));
                }
                exceptionthrown = false;

                //definition of the port owner object
                this._portOwnerComponent = portOwner;

                // First we set up our mutex and semaphore
                _mutex = new Mutex();

                // Then we create our stack of read sockets
                _socketPool = new P2PAsyncEventStack(P2PNetworkingDefinitions.DEFAULT_MAX_CONNECTIONS);

                // Now we create enough read sockets to service the maximum number of clients
                // that we will allow on the server
                // We also assign the event handler for IO Completed to each socket as we create it
                // and set up its buffer to the right size.
                // Then we push it onto our stack to wait for a client connection
                for (Int32 i = 0; i < P2PNetworkingDefinitions.DEFAULT_MAX_CONNECTIONS; i++)
                {
                    SocketAsyncEventArgs asyncEvtArgs = new SocketAsyncEventArgs();
                    asyncEvtArgs.Completed += new EventHandler <SocketAsyncEventArgs>(OnIOCompleted);
                    asyncEvtArgs.SetBuffer(new Byte[P2PNetworkingDefinitions.DATABUFFER_SIZE], 0, P2PNetworkingDefinitions.DATABUFFER_SIZE);
                    _socketPool.Push(asyncEvtArgs);
                }

                //creates a queue to process incomming requests
                _incommingDataProcessingQueue = new TimedProducerConsumerQueue();
                _incommingDataProcessingQueue.NewItemDetected += new TimedProducerConsumerQueue.NewItemDetectedEventHandler(_incommingDataProcessingQueue_NewItemDetected);


                //creates the table of connected clients
                this._ConnectedClientsTable = new Hashtable();

                //initiualizes statistical and startup data
                this._startDateTime     = DateTime.Now;
                this._statisticsHandler = new P2PPortStatisticsHandler();

                //sets the listening port
                this._listeningPort = listeningPort;

                string host = System.Net.Dns.GetHostName();

                this._IPAddress = CommunicationsLibrary.Utilities.CommunicationsUtilities.GetActiveIPAddress();

                // creates the end point of the port
                this._connectionendpoint = new IPEndPoint(this._IPAddress, this._listeningPort);

                //creates the communications socket
                this._connectionsocket = new Socket(this._connectionendpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

                // Now make it a listener socket at the IP address and port that we specified
                this._connectionsocket.Bind(this._connectionendpoint);

                this._connectionsocket.Listen(P2PNetworkingDefinitions.DEFAULT_MAX_CONNECTIONS);
                StartAcceptAsync(null);
                _mutex.WaitOne();
            }