PolyPeer.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. namespace Polycode {
  26. class Timer;
  27. typedef struct {
  28. unsigned int headerHash;
  29. unsigned int sequence;
  30. unsigned int ack;
  31. unsigned short reliableID;
  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() { localSequence = 0; remoteSequence = 0; reliableID = 1;}
  47. ~PeerConnection(){}
  48. void ackPackets(unsigned int ack);
  49. unsigned int localSequence;
  50. unsigned int remoteSequence;
  51. unsigned int reliableID;
  52. std::vector<SentPacketEntry> reliablePacketQueue;
  53. std::vector<unsigned short> recentReliableIDs;
  54. Address address;
  55. };
  56. class _PolyExport Peer : public Threaded, public EventDispatcher {
  57. public:
  58. Peer(unsigned int port);
  59. ~Peer();
  60. void handleEvent(Event *event);
  61. virtual void handlePacket(Packet *packet, PeerConnection *connection){};
  62. virtual void handlePeerConnection(PeerConnection *connection){};
  63. Packet *createPacket(const Address &target, char *data, unsigned int size, unsigned short type);
  64. void sendData(const Address &target, char *data, unsigned int size, unsigned short type);
  65. void sendReliableData(const Address &target, char *data, unsigned int size, unsigned short type);
  66. void sendReliableDataToAll(char *data, unsigned int size, unsigned short type);
  67. void sendDataToAll(char *data, unsigned int size, unsigned short type);
  68. void sendPacket(const Address &target, Packet *packet);
  69. bool checkPacketAcks(PeerConnection *connection, Packet *packet);
  70. PeerConnection *getPeerConnection(const Address &address);
  71. PeerConnection *addPeerConnection(const Address &address);
  72. void removePeerConnection(PeerConnection* connection);
  73. void updateReliableDataQueue();
  74. virtual void updatePeer(){}
  75. void updateThread();
  76. protected:
  77. Timer *updateTimer;
  78. std::vector<PeerConnection*> peerConnections;
  79. Socket *socket;
  80. };
  81. }