-
Notifications
You must be signed in to change notification settings - Fork 2.5k
EN_Cooperate With Tcp Server
When integrating KCP to TCP server, when client handshake to server, server can response the port of UDP(Maybe with other information, such as security key), then client can try UDP handshake with server(Maybe with other informations, for instance, security key or TCP id). Finally, both TCP and UDP channel are ready, and server can find out about the map between TCP and UDP for a client. User can use UDP first, then fallback to TCP if has any problem.
For example:
- Client connect to TCP server, the
id
for this client is generated as identify on server-side. - Server response client, including
port
of UDP,id
, a 32 bits securitykey
. - Client send UDP packet to server, with (
id
andkey
). - Server send UDP packet to client, map the TCP and UDP as a pair.
- Client try UDP, each UDP packet carry (
id
andkey
). - Server can validate the client by (
id and
key).
Remark: Client should send ping message for UDP to keep-alive, or NAT may change the address or port. Client can also reconnect when network changed, for example, the mobile device change base station.
Client can use a general API like:
connection.send(channel, data, size)
When channel is 0, send data over TCP; over KCP when channel is 1; use original UDP when channel is 2. It's similar on server-side.
In china, it's recommented to try UDP, and fallback to TCP when failed.