// callback, udp used for kcp handshake public void OnUdpMessage(byte[] vBytes) { UdpMessage.KcpStruct msg = UdpMessage.clientUnpack(vBytes); if (kcpState == KCP_STATE.SYN_SEND) { if (msg.oper == UdpMessage.S2C_SYN) { udpFd = msg.fd; token = msg.token; StartKcp(udpFd, token, recvCallback); kcpState = KCP_STATE.ACK_SEND; UdpSend(UdpMessage.c2sAck(udpFd, token)); } } else if (kcpState == KCP_STATE.ACK_SEND) { if (msg.oper == UdpMessage.S2C_ACK) { kcpState = KCP_STATE.UP; if (connectCallback != null) { connectCallback((int)CONNECT_FLAG.NORMAL); } } } }
// expand update for handshake and ping-keep public void KcpExpandUpdate() { if (kcpState == KCP_STATE.SYN_SEND || kcpState == KCP_STATE.ACK_SEND) { int waitTime = (int)(TimeHelper.GetMilliseconds() - startConnTime); if (waitTime > connTimeout) { connectCallback((int)CONNECT_FLAG.TIMEOUT); kcpState = KCP_STATE.DOWN; Close(); } else if (waitTime > connRetryTime) { connRetryTime = connRetryTime * 2; if (kcpState == KCP_STATE.SYN_SEND) { UdpSend(UdpMessage.c2sSyn()); } else if (kcpState == KCP_STATE.ACK_SEND) { UdpSend(UdpMessage.c2sAck(udpFd, token)); } } } else if (kcpState == KCP_STATE.UP) { uint curTime = TimeHelper.GetMilliseconds(); if (curTime - lastPingTime > pingInterval) { lastPingTime = curTime; } } }
public void Close() { kcpState = KCP_STATE.DOWN; lock (mSocketLock) { mState = ModuleState.CLOSE; if (mUdpSocket != null) { mUdpSocket.Close(); } } }
public void Connect(int vTimeout, Action <int> nConnectCallback) { connectCallback = nConnectCallback; if (kcpState == KCP_STATE.UP) { //这里有些问题,lua中没有赋值,这里赋值一个没有的值 connectCallback(-2); } else { StartUdpSocket(); UdpSend(UdpMessage.c2sSyn()); kcpState = KCP_STATE.SYN_SEND; //timeout timer set startConnTime = TimeHelper.GetMilliseconds(); connTimeout = vTimeout; connRetryTime = 50; } }