facebookarchive / RakNet

RakNet is a cross platform, open source, C++ networking engine for game programmers.
Other
3.3k stars 1.02k forks source link

Send custom packet before kick (CloseConnection) #62

Open Mellnik opened 9 years ago

Mellnik commented 9 years ago

I'd like to send a custom packet to a client before kicking them with CloseConnection. So I put a IMMEDIATE_PRIORITY in my Send function right before CloseConnection but the packet never reaches the client.. So I wonder if there's a way to send some data to the client before kicking them?

Disintegrate commented 9 years ago

I don't think there is a direct way to guarantee that the packet arrives at the client before the connection is closed. When kicking I just put the client in a queue after sending the kick notification, and wait for a small delay before terminating the connection (100ms should be enough). I also use the 'SetTimeoutTime' function to lower the amount of time I wait for the client to disconnect in case he's not responding.

Mellnik commented 9 years ago

A queue is possible but also less secure. After doing more research I found out that IMMEDIATE_PRIORITY and CloseConnection with sendNotification = true works.

Disintegrate commented 9 years ago

I assumed you were already closing your connection with 'sendNotification = true'. On a local network this approach will always work, but when you use it over an Internet connection problems start to pop up.

Assume you have 1% packet loss, this means once every hundred kick attempts the kick message will never arrive at the client. Even if the kick message arrives it's possible it only arrives after the disconnected notification is received, causing it to be dropped. (of course this depends on the ordering channel, reliability and priority settings) So ideally you want to leave some time between sending the kick message and closing the connection (maybe a few times the round trip delay).

The reason I suggested to lower the timeout time is simply to avoid waiting for the full standard configured timeout when trying to kick a non-responsive client. The server will keep on trying to send the disconnect notification reliably until the configured timeout has passed.

Kiddinglife commented 9 years ago

not sure what you exactly want to solve.A possible solution in my view could be calling the close function in some function called back by client when the closing packets received.