connectionthreads.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. class ConnectionSendThread : public Thread
  25. {
  26. public:
  27. friend class UDPPeer;
  28. ConnectionSendThread(unsigned int max_packet_size, float timeout);
  29. void *run();
  30. void Trigger();
  31. void setParent(Connection *parent)
  32. {
  33. assert(parent != NULL); // Pre-condition
  34. m_connection = parent;
  35. }
  36. void setPeerTimeout(float peer_timeout) { m_timeout = peer_timeout; }
  37. private:
  38. void runTimeouts(float dtime);
  39. void rawSend(const BufferedPacket &packet);
  40. bool rawSendAsPacket(
  41. session_t peer_id, u8 channelnum, SharedBuffer<u8> data, bool reliable);
  42. void processReliableCommand(ConnectionCommand &c);
  43. void processNonReliableCommand(ConnectionCommand &c);
  44. void serve(Address bind_address);
  45. void connect(Address address);
  46. void disconnect();
  47. void disconnect_peer(session_t peer_id);
  48. void send(session_t peer_id, u8 channelnum, SharedBuffer<u8> data);
  49. void sendReliable(ConnectionCommand &c);
  50. void sendToAll(u8 channelnum, SharedBuffer<u8> data);
  51. void sendToAllReliable(ConnectionCommand &c);
  52. void sendPackets(float dtime);
  53. void sendAsPacket(session_t peer_id, u8 channelnum, SharedBuffer<u8> data,
  54. bool ack = false);
  55. void sendAsPacketReliable(BufferedPacket &p, Channel *channel);
  56. bool packetsQueued();
  57. Connection *m_connection = nullptr;
  58. unsigned int m_max_packet_size;
  59. float m_timeout;
  60. std::queue<OutgoingPacket> m_outgoing_queue;
  61. Semaphore m_send_sleep_semaphore;
  62. unsigned int m_iteration_packets_avaialble;
  63. unsigned int m_max_commands_per_iteration = 1;
  64. unsigned int m_max_data_packets_per_iteration;
  65. unsigned int m_max_packets_requeued = 256;
  66. };
  67. class ConnectionReceiveThread : public Thread
  68. {
  69. public:
  70. ConnectionReceiveThread(unsigned int max_packet_size);
  71. void *run();
  72. void setParent(Connection *parent)
  73. {
  74. assert(parent); // Pre-condition
  75. m_connection = parent;
  76. }
  77. private:
  78. void receive();
  79. // Returns next data from a buffer if possible
  80. // If found, returns true; if not, false.
  81. // If found, sets peer_id and dst
  82. bool getFromBuffers(session_t &peer_id, SharedBuffer<u8> &dst);
  83. bool checkIncomingBuffers(Channel *channel, session_t &peer_id, SharedBuffer<u8> &dst);
  84. /*
  85. Processes a packet with the basic header stripped out.
  86. Parameters:
  87. packetdata: Data in packet (with no base headers)
  88. peer_id: peer id of the sender of the packet in question
  89. channelnum: channel on which the packet was sent
  90. reliable: true if recursing into a reliable packet
  91. */
  92. SharedBuffer<u8> processPacket(Channel *channel, SharedBuffer<u8> packetdata,
  93. session_t peer_id, u8 channelnum, bool reliable);
  94. SharedBuffer<u8> handlePacketType_Control(Channel *channel,
  95. SharedBuffer<u8> packetdata, Peer *peer, u8 channelnum,
  96. bool reliable);
  97. SharedBuffer<u8> handlePacketType_Original(Channel *channel,
  98. SharedBuffer<u8> packetdata, Peer *peer, u8 channelnum,
  99. bool reliable);
  100. SharedBuffer<u8> handlePacketType_Split(Channel *channel,
  101. SharedBuffer<u8> packetdata, Peer *peer, u8 channelnum,
  102. bool reliable);
  103. SharedBuffer<u8> handlePacketType_Reliable(Channel *channel,
  104. SharedBuffer<u8> packetdata, Peer *peer, u8 channelnum,
  105. bool reliable);
  106. struct PacketTypeHandler
  107. {
  108. SharedBuffer<u8> (ConnectionReceiveThread::*handler)(Channel *channel,
  109. SharedBuffer<u8> packet, Peer *peer, u8 channelnum,
  110. bool reliable);
  111. };
  112. static const PacketTypeHandler packetTypeRouter[PACKET_TYPE_MAX];
  113. Connection *m_connection = nullptr;
  114. };
  115. }