First Commit, not finished

This commit is contained in:
Debucquoy Anthony 2022-11-04 13:25:09 +01:00
commit fa237eb31c
Signed by: tonitch
GPG Key ID: A78D6421F083D42E
5 changed files with 111 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*.o
mcping

47
Connection.cpp Normal file
View File

@ -0,0 +1,47 @@
#include "Connection.h"
#include <sys/socket.h>
#include <arpa/inet.h>
#include <iostream>
#include <unistd.h>
void Connection::p_HandleError(){
std::cout << "an error occured during execution" << std::endl;
switch (error) {
case ErrorTypes::socket_creation:
perror("error while trying to create a file descriptor");
break;
case ErrorTypes::get_ip:
perror("error while trying to get ip");
break;
case ErrorTypes::connect:
perror("error while trying to connect");
break;
default:
std::cerr << "An unknown error occured" << std::endl;
}
}
int Connection::status(){
return static_cast<int>(error);
}
Connection::Connection(std::string server_ip, int port){
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0){
error = ErrorTypes::socket_creation;
}
in_addr in_a;
if(!inet_aton(server_ip.data(), &in_a)){
error = ErrorTypes::get_ip;
}
sockaddr_in addr({ AF_INET, htons(port), in_a });
if(connect(fd, (sockaddr*) &addr, sizeof(addr)) != 0){
error = ErrorTypes::connect;
}
}
Connection::~Connection(){
close(fd);
}

38
Connection.h Normal file
View File

@ -0,0 +1,38 @@
#ifndef CONNECTION_H
#define CONNECTION_H
#include <string>
#include <vector>
class Payload; //TODO
class Connection
{
private:
int fd;
enum class ErrorTypes{
none = 0,
socket_creation = 10,
get_ip,
connect,
}error;
void p_HandleError();
public:
Connection(std::string server_ip, int port);
~Connection();
bool send(std::vector<Payload> payload);
bool recv(void* data, int size);
/**
* Return the error code from the last command if there is one
* @return error_code
*/
int status();
};
#endif /* CONNECTION_H */

16
Makefile Normal file
View File

@ -0,0 +1,16 @@
CXX=c++
CXXFLAGS= -g -Wall
LDLIBS=
SOURCES=$(wildcard *.cpp)
all: mcping
mcping: $(SOURCES)
$(CXX) $(CXXFLAGS) -o $< $(SOURCES) $(LDLIBS)
run: mcping
./mcping
clean:
rm -f *.o mcping

8
main.cpp Normal file
View File

@ -0,0 +1,8 @@
#include "Connection.h"
int main(int argc, char *argv[])
{
Connection c("145.239.73.162", 25565);
return 0;
}