connectionthreads.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. Minetest
  3. Copyright (C) 2013-2017 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. Copyright (C) 2017 celeron55, Loic Blot <loic.blot@unix-experience.fr>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU Lesser General Public License as published by
  7. the Free Software Foundation; either version 2.1 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License along
  14. with this program; if not, write to the Free Software Foundation, Inc.,
  15. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. */
  17. #pragma once
  18. #include <cassert>
  19. #include "threading/thread.h"
  20. #include "connection.h"
  21. namespace con
  22. {
  23. class Connection;
  24. struct OutgoingPacket
  25. {
  26. session_t peer_id;
  27. u8 channelnum;
  28. SharedBuffer<u8> data;
  29. bool reliable;
  30. bool ack;
  31. OutgoingPacket(session_t peer_id_, u8 channelnum_, const SharedBuffer<u8> &data_,
  32. bool reliable_,bool ack_=false):
  33. peer_id(peer_id_),
  34. channelnum(channelnum_),
  35. data(data_),
  36. reliable(reliable_),
  37. ack(ack_)
  38. {
  39. }
  40. };
  41. class ConnectionSendThread : public Thread
  42. {
  43. public:
  44. friend class UDPPeer;
  45. ConnectionSendThread(unsigned int max_packet_size, float timeout);
  46. void *run();
  47. void Trigger();
  48. void setParent(Connection *parent)
  49. {
  50. assert(parent != NULL); // Pre-condition
  51. m_connection = parent;
  52. }
  53. void setPeerTimeout(float peer_timeout) { m_timeout = peer_timeout; }
  54. private:
  55. void runTimeouts(float dtime);
  56. void rawSend(const BufferedPacket *p);
  57. bool rawSendAsPacket(session_t peer_id, u8 channelnum,
  58. const SharedBuffer<u8> &data, bool reliable);
  59. void processReliableCommand(ConnectionCommandPtr &c);
  60. void processNonReliableCommand(ConnectionCommandPtr &c);
  61. void serve(Address bind_address);
  62. void connect(Address address);
  63. void disconnect();
  64. void disconnect_peer(session_t peer_id);
  65. void send(session_t peer_id, u8 channelnum, const SharedBuffer<u8> &data);
  66. void sendReliable(ConnectionCommandPtr &c);
  67. void sendToAll(u8 channelnum, const SharedBuffer<u8> &data);
  68. void sendToAllReliable(ConnectionCommandPtr &c);
  69. void sendPackets(float dtime);
  70. void sendAsPacket(session_t peer_id, u8 channelnum, const SharedBuffer<u8> &data,
  71. bool ack = false);
  72. void sendAsPacketReliable(BufferedPacketPtr &p, Channel *channel);
  73. bool packetsQueued();
  74. Connection *m_connection = nullptr;
  75. unsigned int m_max_packet_size;
  76. float m_timeout;
  77. std::queue<OutgoingPacket> m_outgoing_queue;
  78. Semaphore m_send_sleep_semaphore;
  79. unsigned int m_iteration_packets_avaialble;
  80. unsigned int m_max_commands_per_iteration = 1;
  81. unsigned int m_max_data_packets_per_iteration;
  82. unsigned int m_max_packets_requeued = 256;
  83. };
  84. class ConnectionReceiveThread : public Thread
  85. {
  86. public:
  87. ConnectionReceiveThread(unsigned int max_packet_size);
  88. void *run();
  89. void setParent(Connection *parent)
  90. {
  91. assert(parent); // Pre-condition
  92. m_connection = parent;
  93. }
  94. private:
  95. void receive(SharedBuffer<u8> &packetdata, bool &packet_queued);
  96. // Returns next data from a buffer if possible
  97. // If found, returns true; if not, false.
  98. // If found, sets peer_id and dst
  99. bool getFromBuffers(session_t &peer_id, SharedBuffer<u8> &dst);
  100. bool checkIncomingBuffers(
  101. Channel *channel, session_t &peer_id, SharedBuffer<u8> &dst);
  102. /*
  103. Processes a packet with the basic header stripped out.
  104. Parameters:
  105. packetdata: Data in packet (with no base headers)
  106. peer_id: peer id of the sender of the packet in question
  107. channelnum: channel on which the packet was sent
  108. reliable: true if recursing into a reliable packet
  109. */
  110. SharedBuffer<u8> processPacket(Channel *channel,
  111. const SharedBuffer<u8> &packetdata, session_t peer_id,
  112. u8 channelnum, bool reliable);
  113. SharedBuffer<u8> handlePacketType_Control(Channel *channel,
  114. const SharedBuffer<u8> &packetdata, Peer *peer, u8 channelnum,
  115. bool reliable);
  116. SharedBuffer<u8> handlePacketType_Original(Channel *channel,
  117. const SharedBuffer<u8> &packetdata, Peer *peer, u8 channelnum,
  118. bool reliable);
  119. SharedBuffer<u8> handlePacketType_Split(Channel *channel,
  120. const SharedBuffer<u8> &packetdata, Peer *peer, u8 channelnum,
  121. bool reliable);
  122. SharedBuffer<u8> handlePacketType_Reliable(Channel *channel,
  123. const SharedBuffer<u8> &packetdata, Peer *peer, u8 channelnum,
  124. bool reliable);
  125. struct PacketTypeHandler
  126. {
  127. SharedBuffer<u8> (ConnectionReceiveThread::*handler)(Channel *channel,
  128. const SharedBuffer<u8> &packet, Peer *peer, u8 channelnum,
  129. bool reliable);
  130. };
  131. static const PacketTypeHandler packetTypeRouter[PACKET_TYPE_MAX];
  132. Connection *m_connection = nullptr;
  133. };
  134. }