示例#1
0
    public override bool Connect(string ip, int port, uint conv)
    {
        try
        {
            _connected = false;
            _socket    = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            _socket.Connect(ip, port);
            _kcp = new KCP(conv, HandleKcpSend);
            _kcp.SetMtu(KCP.IKCP_MTU_DEF - KCPDatagram.CMD_SIZE);
            _kcp.SetInterval(1);
            _kcp.WndSize(512, 512);
            _kcp.NoDelay(2, 10, 2, 1);
            _connected = true;
            _conv      = conv;
            _socket.BeginReceive(_buffer, 0, _maxBufferSize, SocketFlags.None,
                                 new AsyncCallback(OnReceive), _socket);
        }
        catch (Exception e)
        {
            Console.WriteLine("connect failed: " + e);
            _connected = false;
        }

        // clear message queue
        lock (_queue) {
            _queue.Clear();
        }

        return(_connected);
    }