PolyPeer.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. #include "PolyPeer.h"
  20. #include <string.h>
  21. #include "PolyCore.h"
  22. #include "PolyTimer.h"
  23. using namespace Polycode;
  24. PeerConnection::PeerConnection() {
  25. localSequence = 0;
  26. remoteSequence = 0;
  27. receivedPacketQueue.resize(32, 0);
  28. }
  29. PeerConnection::~PeerConnection() {
  30. }
  31. void PeerConnection::ackPackets(unsigned int ack) {
  32. std::vector<SentPacketEntry>::iterator it;
  33. for(it = reliablePacketQueue.begin(); it != reliablePacketQueue.end();) {
  34. if((*it).packet->header.sequence == ack) {
  35. delete (*it).packet;
  36. it = reliablePacketQueue.erase(it);
  37. } else {
  38. ++it;
  39. }
  40. }
  41. }
  42. void PeerConnection::ackPacketsWithBitfield(unsigned int ack, unsigned int ackBitfield) {
  43. if(reliablePacketQueue.size() == 0) {
  44. return;
  45. }
  46. for(int i=0; i <32; i++) {
  47. if(ackBitfield & (1<<i)) {
  48. ackPackets(ack-i);
  49. }
  50. }
  51. }
  52. #if USE_THREADED_SOCKETS == 1
  53. Peer::Peer(unsigned int port) : Threaded() {
  54. #else
  55. Peer::Peer(unsigned int port) : EventDispatcher() {
  56. #endif
  57. socket = new Socket(port);
  58. socket->addEventListener(this, SocketEvent::EVENT_DATA_RECEIVED);
  59. #if USE_THREADED_SOCKETS == 1
  60. CoreServices::getInstance()->getCore()->createThread(this);
  61. updateTimer = NULL;
  62. #else
  63. updateTimer = new Timer(true, SOCKET_POLL_INTERVAL);
  64. updateTimer->addEventListener(this, Timer::EVENT_TRIGGER);
  65. #endif
  66. reliableRetransmissionInverval = 1000;
  67. }
  68. Peer::~Peer() {
  69. delete socket;
  70. }
  71. PeerConnection *Peer::getPeerConnection(const Address &address) {
  72. for(int i=0; i < peerConnections.size(); i++) {
  73. if(peerConnections[i]->address == address) {
  74. return peerConnections[i];
  75. }
  76. }
  77. return NULL;
  78. }
  79. PeerConnection *Peer::addPeerConnection(const Address &address) {
  80. PeerConnection *newConnection = new PeerConnection();
  81. newConnection->address = address;
  82. peerConnections.push_back(newConnection);
  83. handlePeerConnection(newConnection);
  84. return newConnection;
  85. }
  86. void Peer::removePeerConnection(PeerConnection* connection) {
  87. for(unsigned int i=0;i<peerConnections.size();i++) {
  88. if(peerConnections[i] == connection) {
  89. peerConnections.erase(peerConnections.begin()+i);
  90. }
  91. }
  92. }
  93. void Peer::setReliableRetransmissionInterval(int interval) {
  94. reliableRetransmissionInverval = interval;
  95. }
  96. Packet *Peer::createPacket(const Address &target, char *data, unsigned int size, unsigned short type) {
  97. PeerConnection *connection = getPeerConnection(target);
  98. if(!connection)
  99. connection = addPeerConnection(target);
  100. Packet *packet = new Packet();
  101. packet->header.sequence = connection->localSequence;
  102. packet->header.headerHash = 20;
  103. packet->header.ack = connection->remoteSequence;
  104. packet->header.ackBitfield = 0;
  105. for(int i=0; i < 32; i++) {
  106. if(connection->receivedPacketQueue[31-i] == connection->remoteSequence-i) {
  107. packet->header.ackBitfield = (packet->header.ackBitfield & ~(1 << i)) | (1 << i);
  108. } else {
  109. packet->header.ackBitfield = (packet->header.ackBitfield & ~(1 << i)) | (0 << i);
  110. }
  111. }
  112. int sizeToCopy = size;
  113. if(size > MAX_PACKET_SIZE) {
  114. size = MAX_PACKET_SIZE;
  115. }
  116. packet->header.size = sizeToCopy;
  117. packet->header.type = type;
  118. if(size > 0)
  119. memcpy(packet->data, data, size);
  120. connection->localSequence++;
  121. return packet;
  122. }
  123. void Peer::sendReliableData(const Address &target, char *data, unsigned int size, unsigned short type) {
  124. PeerConnection *connection = getPeerConnection(target);
  125. if(!connection)
  126. connection = addPeerConnection(target);
  127. Packet *packet = createPacket(target, data, size, type);
  128. sendPacket(target, packet);
  129. SentPacketEntry entry;
  130. entry.packet = packet;
  131. entry.timestamp = CoreServices::getInstance()->getCore()->getTicks();
  132. connection->reliablePacketQueue.push_back(entry);
  133. }
  134. void Peer::sendDataToAll(char *data, unsigned int size, unsigned short type) {
  135. for(int i=0; i < peerConnections.size(); i++) {
  136. sendData(peerConnections[i]->address, data, size, type);
  137. }
  138. }
  139. void Peer::sendReliableDataToAll(char *data, unsigned int size, unsigned short type) {
  140. for(int i=0; i < peerConnections.size(); i++) {
  141. sendReliableData(peerConnections[i]->address, data, size, type);
  142. }
  143. }
  144. void Peer::sendData(const Address &target, char *data, unsigned int size, unsigned short type) {
  145. Packet *packet = createPacket(target, data, size, type);
  146. sendPacket(target, packet);
  147. delete packet;
  148. }
  149. void Peer::sendPacket(const Address &target, Packet *packet) {
  150. unsigned int packetSize = packet->header.size + sizeof(packet->header);
  151. socket->sendData(target, (char*)packet, packetSize);
  152. }
  153. bool Peer::checkPacketAcks(PeerConnection *connection, Packet *packet) {
  154. if(packet->header.sequence > connection->remoteSequence) {
  155. connection->remoteSequence = packet->header.sequence;
  156. } else {
  157. return false;
  158. }
  159. connection->receivedPacketQueue.push_back(packet->header.sequence);
  160. connection->receivedPacketQueue.pop_front();
  161. connection->ackPacketsWithBitfield(packet->header.ack, packet->header.ackBitfield);
  162. return true;
  163. }
  164. void Peer::handleEvent(Event *event) {
  165. if(event->getDispatcher() == socket) {
  166. SocketEvent *socketEvent = (SocketEvent*) event;
  167. switch(socketEvent->getEventCode()) {
  168. case SocketEvent::EVENT_DATA_RECEIVED:
  169. PeerConnection *connection = getPeerConnection(socketEvent->fromAddress);
  170. if(!connection)
  171. connection = addPeerConnection(socketEvent->fromAddress);
  172. if(checkPacketAcks(connection, (Packet*)socketEvent->data))
  173. handlePacket((Packet*)socketEvent->data, connection);
  174. break;
  175. }
  176. } else if(event->getDispatcher() == updateTimer) {
  177. updateThread();
  178. }
  179. }
  180. void Peer::updateReliableDataQueue() {
  181. for(int i=0; i < peerConnections.size(); i++) {
  182. for(int j=0; j < peerConnections[i]->reliablePacketQueue.size(); j++) {
  183. if(peerConnections[i]->reliablePacketQueue[j].timestamp < CoreServices::getInstance()->getCore()->getTicks() - reliableRetransmissionInverval) {
  184. peerConnections[i]->reliablePacketQueue[j].timestamp = CoreServices::getInstance()->getCore()->getTicks();
  185. Packet *oldPacket = peerConnections[i]->reliablePacketQueue[j].packet;
  186. peerConnections[i]->reliablePacketQueue[j].packet = createPacket(peerConnections[i]->address, oldPacket->data, oldPacket->header.size, oldPacket->header.type);
  187. delete oldPacket;
  188. sendPacket(peerConnections[i]->address, peerConnections[i]->reliablePacketQueue[j].packet);
  189. }
  190. }
  191. }
  192. }
  193. void Peer::updateThread() {
  194. updateReliableDataQueue();
  195. int received = 1;
  196. while( received > 0) {
  197. received = socket->receiveData();
  198. }
  199. }