TCPMessageConnection.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* Copyright The kNet Project.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License. */
  11. #pragma once
  12. /** @file TCPMessageConnection.h
  13. @brief The TCPMessageConnection class.*/
  14. #include "MessageConnection.h"
  15. #include "RingBuffer.h"
  16. /*
  17. TCP stream format:
  18. .Message.
  19. .Message.
  20. ...
  21. .Message.
  22. Message format:
  23. u8-u32 Whole Message length (including MessageID + ContentData, but not this field itself) VLE-encoded 1.7/1.7/16
  24. u8-u32 MessageID VLE-encoded 1.7/1.7/16
  25. N bytes Content data
  26. */
  27. namespace kNet
  28. {
  29. class TCPMessageConnection : public MessageConnection
  30. {
  31. public:
  32. TCPMessageConnection(Network *owner, NetworkServer *ownerServer, Socket *socket, ConnectionState startingState);
  33. ~TCPMessageConnection();
  34. private:
  35. /// Maintains a byte buffer that contains partial messages. [worker thread]
  36. RingBuffer tcpInboundSocketData;
  37. /// Reads all available bytes from a stream socket.
  38. SocketReadResult ReadSocket(size_t &bytesRead); // [worker thread]
  39. PacketSendResult SendOutPacket(); // [worker thread]
  40. void SendOutPackets(); // [worker thread]
  41. void DoUpdateConnection(); // [worker thread]
  42. unsigned long TimeUntilCanSendPacket() const;
  43. /// Parses the raw inbound byte stream into messages. [used internally by worker thread]
  44. void ExtractMessages();
  45. // The following are temporary data structures used by various internal routines for processing.
  46. std::vector<NetworkMessage*> serializedMessages; // MessageConnection::TCPSendOutPacket()
  47. void PerformDisconnection();
  48. void DumpConnectionStatus() const; // [main thread]
  49. };
  50. } // ~kNet