mcpp
C++ Minecraft Library
connection.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstdint>
4 #include <iostream>
5 #include <sstream>
6 #include <string>
7 
8 #define FAIL_RESPONSE "Fail"
9 
14 namespace mcpp {
16  private:
17  int socketHandle;
18  std::string lastSent;
19 
20  static std::string resolveHostname(const std::string& hostname);
21 
22  public:
23  SocketConnection(const std::string& address_str, uint16_t port);
24 
25  void send(const std::string& dataString);
26 
36  template <typename... Types>
37  void sendCommand(const std::string& prefix, const Types&... args) {
38  std::stringstream ss;
39 
40  ss << prefix << "(";
41 
42  // Iterate over args pack
43  ((ss << args << ','), ...);
44  // Remove trailing comma
45  ss.seekp(-1, std::ios_base::end);
46 
47  ss << ")\n";
48 
49  send(ss.str());
50  }
51 
61  template <typename T, typename... Types>
62  std::string sendReceiveCommand(const T& prefix, const Types&... args) {
63  sendCommand(prefix, args...);
64  auto result = recv();
65  return result;
66  }
67 
68  [[nodiscard]] std::string recv() const;
69 };
70 } // namespace mcpp
Definition: connection.h:15
SocketConnection(const std::string &address_str, uint16_t port)
std::string recv() const
void sendCommand(const std::string &prefix, const Types &... args)
Definition: connection.h:37
std::string sendReceiveCommand(const T &prefix, const Types &... args)
Definition: connection.h:62
void send(const std::string &dataString)
Namespace containing all the the mcpp library classes.
Definition: block.h:9