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