Transport.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #pragma once
  2. #include "mumlib/CryptState.hpp"
  3. #include "mumlib/VarInt.hpp"
  4. #include "enums.hpp"
  5. #include <boost/noncopyable.hpp>
  6. #include <boost/asio/ssl.hpp>
  7. #include <boost/asio.hpp>
  8. #include <boost/bind.hpp>
  9. #include <boost/pool/pool.hpp>
  10. #include <log4cpp/Category.hh>
  11. #include <google/protobuf/message.h>
  12. #include <chrono>
  13. #include <utility>
  14. namespace mumlib {
  15. constexpr int MAX_UDP_LENGTH = 1024;
  16. constexpr int MAX_TCP_LENGTH = 129 * 1024; // 128 kB + some reserve
  17. using namespace std;
  18. using namespace boost::asio;
  19. using namespace boost::asio::ip;
  20. typedef function<bool(MessageType, uint8_t *, int)> ProcessControlMessageFunction;
  21. typedef function<bool(AudioPacketType, uint8_t *, int)> ProcessEncodedAudioPacketFunction;
  22. class TransportException : public MumlibException {
  23. public:
  24. TransportException(string message) : MumlibException(std::move(message)) { }
  25. };
  26. class Transport : boost::noncopyable {
  27. public:
  28. Transport(io_service &ioService,
  29. ProcessControlMessageFunction processControlMessageFunc,
  30. ProcessEncodedAudioPacketFunction processEncodedAudioPacketFunction,
  31. bool noUdp = false);
  32. ~Transport();
  33. void connect(string host,
  34. int port,
  35. string user,
  36. string password);
  37. void disconnect();
  38. void reconnect();
  39. ConnectionState getConnectionState() {
  40. return state;
  41. }
  42. bool isUdpActive();
  43. void sendControlMessage(MessageType type, google::protobuf::Message &message);
  44. void sendEncodedAudioPacket(uint8_t *buffer, int length);
  45. private:
  46. log4cpp::Category &logger;
  47. io_service &ioService;
  48. pair<string, int> connectionParams;
  49. pair<string, string> credentials;
  50. ProcessControlMessageFunction processMessageFunction;
  51. ProcessEncodedAudioPacketFunction processEncodedAudioPacketFunction;
  52. const bool noUdp;
  53. volatile bool udpActive;
  54. ConnectionState state;
  55. udp::socket udpSocket;
  56. ip::udp::endpoint udpReceiverEndpoint;
  57. uint8_t udpIncomingBuffer[MAX_UDP_LENGTH];
  58. CryptState cryptState;
  59. ssl::context sslContext;
  60. ssl::stream<tcp::socket> sslSocket;
  61. uint8_t *sslIncomingBuffer;
  62. deadline_timer pingTimer;
  63. std::chrono::time_point<std::chrono::system_clock> lastReceivedUdpPacketTimestamp;
  64. boost::pool<> asyncBufferPool;
  65. void pingTimerTick(const boost::system::error_code &e);
  66. void sslConnectHandler(const boost::system::error_code &error);
  67. void sslHandshakeHandler(const boost::system::error_code &error);
  68. void doReceiveSsl();
  69. void sendSsl(uint8_t *buff, int length);
  70. void sendSslAsync(uint8_t *buff, int length);
  71. void sendControlMessagePrivate(MessageType type, google::protobuf::Message &message);
  72. void sendSslPing();
  73. void sendVersion();
  74. void sendAuthentication();
  75. void processMessageInternal(MessageType messageType, uint8_t *buffer, int length);
  76. void doReceiveUdp();
  77. void sendUdpAsync(uint8_t *buff, int length);
  78. void sendUdpPing();
  79. void throwTransportException(string message);
  80. void processAudioPacket(uint8_t *buff, int length);
  81. };
  82. }