Skip to content
Snippets Groups Projects
Commit e9f74705 authored by Leon Dietrich's avatar Leon Dietrich
Browse files

add: TCP socket constructor

parent 7d956fe3
No related branches found
No related tags found
No related merge requests found
/*
* netio_exception.cpp
*
* Created on: 02.01.2021
* Author: doralitze
*/
#include "netio_exception.hpp"
namespace rmrf::net {
netio_exception::netio_exception(const std::string cause_) : cause(cause_) {
}
const char* netio_exception::what() const throw(){
return this->cause.c_str();
}
}
/*
* netio_exception.hpp
*
* Created on: 02.01.2021
* Author: doralitze
*/
#pragma once
#include <exception>
#include <string>
namespace rmrf::net {
class netio_exception: public std::exception {
private:
std::string cause;
public:
netio_exception(const std::string cause_);
virtual const char* what() const throw();
};
}
/*
* tcp_server_socket.cpp
*
* Created on: 02.01.2021
* Author: doralitze
*/
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include "tcp_server_socket.hpp"
namespace rmrf::net {
tcp_server_socket::tcp_server_socket(uint16_t port) : ss{nullptr} {
auto raw_socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if(raw_socket_fd < 0) {
// TODO implement propper error handling
throw netio_exception("Failed to create socket fd.");
}
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = INADDR_ANY; // TODO FIXME
if (bind(raw_socket_fd, (struct sockaddr *) &addr, sizeof(addr)) != 0) {
throw netio_exception("Failed to bind to all addresses (FIXME)");
}
fcntl(raw_socket_fd, F_SETFL, fcntl(raw_socket_fd, F_GETFL, 0) | O_NONBLOCK);
if (listen(raw_socket_fd, 5) == -1) {
throw netio_exception("Failed to enable listenting mode for raw socket");
}
this->ss = std::make_shared<async_server_socket>(auto_fd(raw_socket_fd));
}
}
/*
* tcp_server_socket.hpp
*
* Created on: 02.01.2021
* Author: doralitze
*/
#pragma once
#include <cstdio>
#include <memory>
#include "async_server.hpp"
#include "netio_exception.hpp"
namespace rmrf::net {
class tcp_server_socket {
private:
std::shared_ptr<async_server_socket> ss;
public:
tcp_server_socket(uint16_t port);
};
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment