示例#1
0
    /// <summary>
    /// 连接到服务端。
    /// 在update中,状态将会更新。
    /// 如果status为ConnectFail,则需要重新Connect
    /// </summary>
    /// <param name="host"></param>
    /// <param name="port"></param>
    /// <param name="index"></param>
    /// <param name="key"></param>
    public void Connect(string host, UInt16 port, uint index, int key)
    {
        m_RecvQueue = new SwitchQueue <byte[]>(128);

        m_UdpClient = new UdpClient();
        m_UdpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 5000);
        server_addr = new IPEndPoint(IPAddress.Parse(host), port);
        //m_UdpClient.Connect(new IPEndPoint(IPAddress.Parse(host), port));

        Status = ClientSessionStatus.InConnect;
        //init_kcp((UInt32)new Random((int)DateTime.Now.Ticks).Next(1, Int32.MaxValue));
        m_NetIndex = index;
        m_Key      = key;
        init_kcp(index);
        m_sw = Stopwatch.StartNew();
        SendHandshake();
        m_LastSendHandshake = m_sw.ElapsedMilliseconds;
        //m_UdpClient.BeginReceive(ReceiveCallback, this);

        recvThraed = new Thread(OnRecievedData);
        recvThraed.Start();


        sendThread = new Thread(ThreadSendAction);
        sendThread.Start();
    }
示例#2
0
        /// <summary>
        /// 创建kcp服务端
        /// </summary>
        /// <param name="port"></param>
        public KServer(int port = 7777) : base()
        {
            //创建下层协议,并绑定本地IP,端口
            client = new UdpClient(new IPEndPoint(IPAddress.Any, port));
            client.BeginReceive(OnRecive, null);

            recvQueue = new SwitchQueue <byte[]>();
        }
示例#3
0
 public void Clean()
 {
     kcp?.Dispose();
     kcp            = null;
     recvListener   = null;
     sendBufferTemp = null;
     recvBufferTemp = null;
     tempSendData   = null;
     recvBuffQueue.Clear();
     recvBuffQueue = null;
     Close();
 }
示例#4
0
 public void Init(uint sessionid)
 {
     sessionID              = sessionid;
     sendBufferTemp         = new byte[4096];
     recvBufferTemp         = new byte[4096];
     tempSendData           = new FSPDataCToS();
     recvBuffQueue          = new SwitchQueue <byte[]>();
     tempSendData.sessionID = sessionid;
     tempSendData.msg       = new FSPMessage();
     kcp = new KCP(sessionid, HandleKcpSend);
     kcp.NoDelay(1, 10, 2, 1);
     kcp.WndSize(128, 128);
 }
示例#5
0
    /// <summary>
    /// 连接
    /// </summary>
    public void Connect(string host, int port, uint index)
    {
        m_RecvQueue = new SwitchQueue <byte[]>(128);

        socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); //创建socket
        socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 5000);
        srvAddr  = IPAddress.Parse(host);                                                    //服务器IP地址
        srvIpEnd = new IPEndPoint(srvAddr, port);                                            //服务器地址
        srvEnd   = (EndPoint)srvIpEnd;                                                       //必须要经过这个类型转换,EndPoint是一个抽象类

        state = 1;
        init_kcp(index);
        RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
        if (ConnectResultEvent != null)
        {
            ConnectResultEvent(true);
        }
    }