Esempio n. 1
0
        /// <summary>
        /// 开始连接
        /// </summary>
        /// <param name="dns">一个代表远程节点的IPEndPoint</param>
        /// <param name="encrypter">封装连接管理类</param>
        /// <returns>返回一个单套接字</returns>
        public ISingleSocket StartConnect(IPEndPoint dns, IEncrypter encrypter)
        {
            if (encrypter == null)
            {
                encrypter = this.handler;
            }

            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            sock.Blocking = false;
            try
            {
                sock.Connect(dns);
            }

            catch
            {
            }

            poll.Register(sock, PollMode.PollIn);
            SingleSocket singleSocket = new SingleSocket(this, sock, encrypter);

            singleSocketDictionary[sock.Handle] = singleSocket;
            return(singleSocket);
        }
Esempio n. 2
0
        /// <summary>
        /// 处理服务器套接字
        /// </summary>
        /// <param name="item">待处理的PollItem</param>
        private void HandleServerSocket(PollItem item)
        {
            //如果出错或者挂起,则关闭连接
            if ((item.Mode & (PollMode.PollError | PollMode.PollHangUp)) != 0)
            {
                poll.Unregister(server);
                server.Close();
            }

            //否则建立一个新的socket,然后让侦听中的插口去accept它,以后数据的读写应该在新的socket中进行。
            else
            {
                Socket newSocket = server.Accept();
                newSocket.Blocking = false;
                SingleSocket newSingleSocket = new SingleSocket(this, newSocket, handler);
                singleSocketDictionary[newSocket.Handle] = newSingleSocket;
                poll.Register(newSocket, PollMode.PollIn);
                handler.MakeExternalConnection(newSingleSocket);
            }
        }