// This method is called when we need to create a new SAEA object to do // accept operations. The reason to put it in a separate method is so that // we can easily add more objects to the pool if we need to. // You can do that if you do NOT use a buffer in the SAEA object that does // the accept operations. internal SocketAsyncEventArgs CreateNewSaeaForAccept(SocketAsyncEventArgsPool pool) { //Allocate the SocketAsyncEventArgs object. SocketAsyncEventArgs acceptEventArg = new SocketAsyncEventArgs(); //SocketAsyncEventArgs.Completed is an event, (the only event,) //declared in the SocketAsyncEventArgs class. //See http://msdn.microsoft.com/en-us/library/system.net.sockets.socketasynceventargs.completed.aspx. //An event handler should be attached to the event within //a SocketAsyncEventArgs instance when an asynchronous socket //operation is initiated, otherwise the application will not be able //to determine when the operation completes. //Attach the event handler, which causes the calling of the //AcceptEventArg_Completed object when the accept op completes. acceptEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(AcceptEventArg_Completed); return acceptEventArg; // accept operations do NOT need a buffer. // You can see that is true by looking at the // methods in the .NET Socket class on the Microsoft website. AcceptAsync does // not take require a parameter for buffer size. }
// Constructor. public SocketListener(NetworkSettings theNetworkSettings) { this._networkSettings = theNetworkSettings; //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._networkSettings.ReceiveBufferSize * this._networkSettings.NumberOfSaeaForRecSend * this._networkSettings.OpsToPreAllocate, this._networkSettings.ReceiveBufferSize); this._poolOfReceiveEventArgs = new SocketAsyncEventArgsPool(this._networkSettings.NumberOfSaeaForRecSend); this._poolOfSendEventArgs = new SocketAsyncEventArgsPool(this._networkSettings.NumberOfSaeaForRecSend); this._poolOfAcceptEventArgs = new SocketAsyncEventArgsPool(this._networkSettings.MaxSimultaneousAcceptOps); // Create connections count enforcer this._theMaxConnectionsEnforcer = new Semaphore(this._networkSettings.MaxConnections, this._networkSettings.MaxConnections); ActiveChannels = new ObservableCollection<AsyncTransportChannel>(); //Microsoft's example called these from Main method, which you //can easily do if you wish. Init(); StartListen(); }