Пример #1
0
        public void Close()
        {
            LinkedListNode<NetHandle> curNode = handleList_.First;
            LinkedListNode<NetHandle> nextNode;
            while (curNode != null)
            {
                nextNode = curNode.Next;
                curNode.Value.Close();
                handleList_.Remove(curNode);
                curNode = nextNode;
            }

            mainHandle_.Close();
            mainHandle_ = null;
        }
Пример #2
0
        public bool Connect(string ip, int port)
        {
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
            {
                sock.Blocking = false;
                sock.Connect(ip, port);
            }
            catch (SocketException e)
            {
                if (e.ErrorCode != 10035)
                {
                    Log.E(e.Message);
                    return false;
                }
            }

            mainHandle_ = new NetHandle(sock);
            onUpdate_ = ConnectorTryConnect;
            return true;
        }
Пример #3
0
        public bool Listen(int port)
        {
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
            {
                IPEndPoint ipe = new IPEndPoint(IPAddress.Any, port);
                sock.Bind(ipe);
                sock.Listen(backLog_);
                sock.Blocking = false;
                mainHandle_ = new NetHandle(sock);
            }
            catch (Exception e)
            {
                Log.E(e.Message);
                return false;
            }

            onUpdate_ = AcceptorUpdate;
            return true;
        }
Пример #4
0
        void AcceptedToHandle(int count)
        {
            Socket sock;
            NetHandle handle;
            LinkedListNode<Socket> curNode = acceptedList_.First;
            LinkedListNode<Socket> nextNode;
            while (curNode != null)
            {
                sock = curNode.Value;
                nextNode = curNode.Next;
                acceptedList_.Remove(curNode);
                curNode = nextNode;

                sock.Blocking = false;
                sock.ReceiveBufferSize = 65536;
                handle = new NetHandle(sock);
                handleList_.AddLast(handle);
                onConnect_(handle);

                if (--count <= 0)
                    return;
            }
        }