Refactor keep-alive process logic. #7
                
     Merged
            
            
          
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
When a keep alive echo is received, the old logic used to compare the size of the encrypted received data with a default encrypted keep alive packet. One major flaw in this is that encrypted data, regardless if it's a packet or client request, will be 512 bytes in size when encrypted with the RSA request keys (4096 bit keys), this logic causes improper checking and can mistake any encrypted data for a successful keep-alive echo.
Furthermore, in the receiving logic, SendKeepAlivePackets(long cuid) and TCPReceiveMessagesFromClient(long cuid) would both be receiving at the same time. Thus, TCPReceiveMessagesFromClient would actually receive the Packet and not SendKeepAlivePackets. Take advantage of this by removing the ReceiveData() call in SendKeepAlivePackets, and just set a flag in the client when a keep alive packet is received while the KeepAliveProcess flag is set to true when we receive data in TCPReceiveMessagesFromClient. This allows for better readablility, and more efficient code. In SendKeepAlivePackets(), we can use Sleep(), and consistently check if
If all of these are successful, we have received a valid response. Otherwise, keep waiting and them declare a time-out by the client.
Overall, this new logic is more efficient compared to the old code, and more readable, with TCPReceiveMessagesFromClient doing all the receiving and SendKeepAlivePackets doing all the sending and handling timeout.