First Commit, not finished
This commit is contained in:
commit
fa237eb31c
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
*.o
|
||||
mcping
|
47
Connection.cpp
Normal file
47
Connection.cpp
Normal 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
38
Connection.h
Normal 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
16
Makefile
Normal 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
|
Loading…
Reference in New Issue
Block a user