Transport.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. ConnectionState getConnectionState() {
  39. return state;
  40. }
  41. bool isUdpActive();
  42. void sendControlMessage(MessageType type, google::protobuf::Message &message);
  43. void sendEncodedAudioPacket(uint8_t *buffer, int length);
  44. private:
  45. log4cpp::Category &logger;
  46. io_service &ioService;
  47. pair<string, int> connectionParams;
  48. pair<string, string> credentials;
  49. ProcessControlMessageFunction processMessageFunction;
  50. ProcessEncodedAudioPacketFunction processEncodedAudioPacketFunction;
  51. const bool noUdp;
  52. volatile bool udpActive;
  53. ConnectionState state;
  54. udp::socket udpSocket;
  55. ip::udp::endpoint udpReceiverEndpoint;
  56. uint8_t udpIncomingBuffer[MAX_UDP_LENGTH];
  57. CryptState cryptState;
  58. ssl::context sslContext;
  59. ssl::stream<tcp::socket> sslSocket;
  60. uint8_t *sslIncomingBuffer;
  61. deadline_timer pingTimer;
  62. std::chrono::time_point<std::chrono::system_clock> lastReceivedUdpPacketTimestamp;
  63. boost::pool<> asyncBufferPool;
  64. void pingTimerTick(const boost::system::error_code &e);
  65. void sslConnectHandler(const boost::system::error_code &error);
  66. void sslHandshakeHandler(const boost::system::error_code &error);
  67. void doReceiveSsl();
  68. void sendSsl(uint8_t *buff, int length);
  69. void sendSslAsync(uint8_t *buff, int length);
  70. void sendControlMessagePrivate(MessageType type, google::protobuf::Message &message);
  71. void sendSslPing();
  72. void sendVersion();
  73. void sendAuthentication();
  74. void processMessageInternal(MessageType messageType, uint8_t *buffer, int length);
  75. void doReceiveUdp();
  76. void sendUdpAsync(uint8_t *buff, int length);
  77. void sendUdpPing();
  78. void throwTransportException(string message);
  79. void processAudioPacket(uint8_t *buff, int length);
  80. };
  81. }