Ejemplo n.º 1
0
        private void CloseClientSocket(SocketAsyncEventArgs e)
        {
            AsyncUserToken token = e.UserToken as AsyncUserToken;

            // close the socket associated with the client
            try
            {
                token.Socket.Shutdown(SocketShutdown.Send);
            }
            // throws if client process has already closed
            catch (Exception ex)
            {
                LogTool.Log("close socket error: " + ex.Message);
            }
            token.Socket.Close();

            // decrement the counter keeping track of the total number of clients connected to the server
            //Interlocked.Decrement(ref m_numConnectedSockets);

            //Console.WriteLine("A client has been disconnected from the server. There are {0} clients connected to the server", m_numConnectedSockets);

            // Free the SocketAsyncEventArg so they can be reused by another client
            m_readWritePool.Push(e);
            m_maxNumberAcceptedClients.Release();
        }
Ejemplo n.º 2
0
        protected void BeginService(string ipaddress, int port)
        {
            IPAddress ip = IPAddress.Parse(ipaddress);

            host = new IPEndPoint(ip, port);

            //sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            mainServiceSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            mainServiceSocket.Bind(host);
            //mainServiceSocket.Listen(m_maxConnnections);

            //m_bufferManager = BufferManager.CreateBufferManager(m_maxConnnections * m_maxSize * opsToPreAlloc, m_maxSize);
            m_bufferManager = BufferManager.CreateBufferManager((m_maxReciveCount + m_maxSendCount) * m_maxSize, m_maxSize);

            for (int i = 0; i < (m_maxReciveCount * 2 + m_maxSendCount); i++)
            {
                SocketAsyncEventArgs socketAsyncEventArg = new SocketAsyncEventArgs();
                socketAsyncEventArg.UserToken  = new T();
                socketAsyncEventArg.Completed += new EventHandler <SocketAsyncEventArgs>(IO_Completed);
                //socketAsyncEventArg.SetBuffer(m_bufferManager.TakeBuffer(m_maxSize), 0, m_maxSize);
                m_readWritePool.Push(socketAsyncEventArg);
            }


            StartReceive();
        }
Ejemplo n.º 3
0
 private void CloseOneInstance(AsyncClientToken token, bool IsError = true)
 {
     if (token != null)
     {
         if (token.Socket != null)
         {
             token.Socket.Close();
             token.Socket = null;
         }
         m_readWritePool.Push(token);
     }
     m_maxNumberAcceptedClients.Release();
 }
Ejemplo n.º 4
0
        public AsyncSocketClient(int _maxSize, int _maxConnnections, ICommonLog _logTool)
        {
            m_readWritePool   = new SocketAsyncEventArgsPool <AsyncClientToken>();
            m_maxConnnections = _maxConnnections;
            m_maxSize         = _maxSize;
            LogTool           = _logTool;
            m_bufferManager   = BufferManager.CreateBufferManager(m_maxConnnections * m_maxSize, m_maxSize);

            for (int i = 0; i < _maxConnnections; i++)
            {
                AsyncClientToken asyncClientToken = new AsyncClientToken();
                asyncClientToken.Buffer = m_bufferManager.TakeBuffer(m_maxSize);
                m_readWritePool.Push(asyncClientToken);
            }

            m_maxNumberAcceptedClients = new Semaphore(m_maxConnnections, m_maxConnnections);
        }
Ejemplo n.º 5
0
        public AsyncSocketListener(int _maxSize, int _maxConnnections, ICommonLog _logTool)
        {
            m_readWritePool   = new SocketAsyncEventArgsPool <SocketAsyncEventArgs>();
            m_maxConnnections = _maxConnnections;
            m_maxSize         = _maxSize;
            LogTool           = _logTool;

            //m_bufferManager = BufferManager.CreateBufferManager(m_maxConnnections * m_maxSize * opsToPreAlloc, m_maxSize);
            m_bufferManager = BufferManager.CreateBufferManager(m_maxConnnections * m_maxSize, m_maxSize);

            for (int i = 0; i < m_maxConnnections; i++)
            {
                SocketAsyncEventArgs socketAsyncEventArg = new SocketAsyncEventArgs();
                socketAsyncEventArg.UserToken  = new T();
                socketAsyncEventArg.Completed += new EventHandler <SocketAsyncEventArgs>(IO_Completed);
                socketAsyncEventArg.SetBuffer(m_bufferManager.TakeBuffer(m_maxSize), 0, m_maxSize);
                m_readWritePool.Push(socketAsyncEventArg);
            }

            m_maxNumberAcceptedClients = new Semaphore(m_maxConnnections, m_maxConnnections);
        }