PolyPeer.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #include "PolyGlobals.h"
  21. #include "PolyCoreServices.h"
  22. #include "PolyThreaded.h"
  23. #include "PolySocket.h"
  24. #include <vector>
  25. #include <deque>
  26. namespace Polycode {
  27. class Timer;
  28. typedef struct {
  29. unsigned int headerHash;
  30. unsigned int sequence;
  31. unsigned int ack;
  32. unsigned int ackBitfield;
  33. unsigned short size;
  34. unsigned short type;
  35. } PacketHeader;
  36. typedef struct {
  37. PacketHeader header;
  38. char data[MAX_PACKET_SIZE];
  39. } Packet;
  40. typedef struct {
  41. Packet *packet;
  42. unsigned int timestamp;
  43. } SentPacketEntry;
  44. class _PolyExport PeerConnection {
  45. public:
  46. PeerConnection();
  47. ~PeerConnection();
  48. void ackPacketsWithBitfield(unsigned int ack, unsigned int ackBitfield);
  49. void ackPackets(unsigned int ack);
  50. unsigned int localSequence;
  51. unsigned int remoteSequence;
  52. std::vector<SentPacketEntry> reliablePacketQueue;
  53. std::deque <unsigned int> receivedPacketQueue;
  54. Address address;
  55. };
  56. /**
  57. * A network actor that can send and receive data.
  58. *
  59. * Peers are comparable to UDP sockets, but with extended functionality
  60. * to optionally allow for some of TCP's features(ordering, reliability).
  61. *
  62. * WARNING: Reliability(packets being resent on loss) is currently not
  63. * implemented, but is planned.
  64. *
  65. * @see PeerConnection
  66. */
  67. #if USE_THREADED_SOCKETS == 1
  68. class _PolyExport Peer : public Threaded {
  69. #else
  70. class _PolyExport Peer : public EventDispatcher {
  71. #endif
  72. public:
  73. /**
  74. * Create a peer. The peer will immediately start listening on the given
  75. * port and accept incoming packets.
  76. *
  77. * @param port The UDP port to listen for packets. Can not be omitted,
  78. * this will be the actual port this peer will use to send
  79. * and receive packets.
  80. */
  81. Peer(unsigned int port);
  82. ~Peer();
  83. void handleEvent(Event *event);
  84. virtual void handlePacket(Packet *packet, PeerConnection *connection){};
  85. virtual void handlePeerConnection(PeerConnection *connection){};
  86. Packet *createPacket(const Address &target, char *data, unsigned int size, unsigned short type);
  87. /**
  88. * Send raw binary data to the target address.
  89. *
  90. * @param target The network Address to send the data to.
  91. * @param data The binary data to send as a C byte array. Length must be supplied by size parameter.
  92. * @param size The size in bytes of the sent binary data.
  93. * @param type A number representing the packet type, used to define the purpose of the packet.
  94. */
  95. void sendData(const Address &target, char *data, unsigned int size, unsigned short type);
  96. /**
  97. * Send raw binary data to the target address, making sure it arrives in the right order.
  98. *
  99. * @param target The network address to send the data to.
  100. * @param data The binary data to send as a C byte array. Length must be supplied by size parameter.
  101. * @param size The size in bytes of the sent binary data.
  102. * @param type A number representing the packet type, used to define the purpose of the packet.
  103. */
  104. void sendReliableData(const Address &target, char *data, unsigned int size, unsigned short type);
  105. /**
  106. * Broadcast raw binary data to all connected peers, making sure it arrives in the right order.
  107. *
  108. * @param data The binary data to send as a C byte array. Length must be supplied by size parameter.
  109. * @param size The size in bytes of the sent binary data.
  110. * @param type A number representing the packet type, used to define the purpose of the packet.
  111. */
  112. void sendReliableDataToAll(char *data, unsigned int size, unsigned short type);
  113. /**
  114. * Broadcast raw binary data to all connected peers.
  115. *
  116. * @param data The binary data to send as a C byte array. Length must be supplied by size parameter.
  117. * @param size The size in bytes of the sent binary data.
  118. * @param type A number representing the packet type, used to define the purpose of the packet.
  119. */
  120. void sendDataToAll(char *data, unsigned int size, unsigned short type);
  121. void sendPacket(const Address &target, Packet *packet);
  122. bool checkPacketAcks(PeerConnection *connection, Packet *packet);
  123. PeerConnection *getPeerConnection(const Address &address);
  124. PeerConnection *addPeerConnection(const Address &address);
  125. void removePeerConnection(PeerConnection* connection);
  126. void setReliableRetransmissionInterval(int interval);
  127. void updateReliableDataQueue();
  128. virtual void updatePeer(){}
  129. void updateThread();
  130. protected:
  131. int reliableRetransmissionInverval;
  132. Timer *updateTimer;
  133. std::vector<PeerConnection*> peerConnections;
  134. Socket *socket;
  135. };
  136. }