Implement Socket class: add constructors, destructor, and methods for socket operations including listen, bind, accept, recv, send, and setNonBlocking.
This commit is contained in:
parent
29d6431143
commit
708a4ee25f
89
webserv/socket/Socket.cpp
Normal file
89
webserv/socket/Socket.cpp
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
#include <webserv/socket/Socket.hpp>
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <unistd.h> // For close()
|
||||||
|
|
||||||
|
#include <arpa/inet.h> // For inet_addr
|
||||||
|
#include <fcntl.h> // For fcntl()"
|
||||||
|
#include <netinet/in.h> // For sockaddr_in
|
||||||
|
#include <sys/socket.h>
|
||||||
|
|
||||||
|
Socket::Socket() : _fd(socket(AF_INET, SOCK_STREAM, 0))
|
||||||
|
{
|
||||||
|
if (_fd == -1)
|
||||||
|
{
|
||||||
|
throw std::runtime_error("Socket creation failed");
|
||||||
|
}
|
||||||
|
int opt = 1;
|
||||||
|
if (setsockopt(_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0)
|
||||||
|
{
|
||||||
|
close(_fd);
|
||||||
|
_fd = -1;
|
||||||
|
throw std::runtime_error("setsockopt failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Socket::Socket(int fd) : _fd(fd) // NOLINT readability-identifier-naming
|
||||||
|
{
|
||||||
|
if (_fd == -1)
|
||||||
|
{
|
||||||
|
throw std::runtime_error("Invalid file descriptor");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Socket::~Socket()
|
||||||
|
{
|
||||||
|
if (_fd != -1)
|
||||||
|
{
|
||||||
|
close(_fd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Socket::listen(int backlog) const
|
||||||
|
{
|
||||||
|
if (::listen(_fd, backlog) < 0)
|
||||||
|
{
|
||||||
|
throw std::runtime_error("Listen failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void Socket::bind(const std::string &host, const int port) const
|
||||||
|
{
|
||||||
|
|
||||||
|
struct sockaddr_in address{};
|
||||||
|
address.sin_family = AF_INET;
|
||||||
|
address.sin_addr.s_addr = inet_addr(host.c_str());
|
||||||
|
address.sin_port = htons(port);
|
||||||
|
|
||||||
|
if (::bind(_fd, (struct sockaddr *)&address, sizeof(address)) < 0) // NOLINT cppcoreguidelines-pro-type-cstyle-cast
|
||||||
|
{
|
||||||
|
throw std::runtime_error("Bind failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Socket Socket::accept() const
|
||||||
|
{
|
||||||
|
int client_fd = ::accept(_fd, nullptr, nullptr);
|
||||||
|
if (client_fd < 0)
|
||||||
|
{
|
||||||
|
throw std::runtime_error("Accept failed");
|
||||||
|
}
|
||||||
|
return {client_fd};
|
||||||
|
}
|
||||||
|
|
||||||
|
ssize_t Socket::recv(void *buf, size_t len) const
|
||||||
|
{
|
||||||
|
return ::recv(_fd, buf, len, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
ssize_t Socket::send(const void *buf, size_t len) const
|
||||||
|
{
|
||||||
|
return ::send(_fd, buf, len, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Socket::setNonBlocking() const
|
||||||
|
{
|
||||||
|
if (fcntl(_fd, F_SETFL, O_NONBLOCK) < 0)
|
||||||
|
{
|
||||||
|
throw std::runtime_error("Failed to set non-blocking mode");
|
||||||
|
}
|
||||||
|
}
|
||||||
29
webserv/socket/Socket.hpp
Normal file
29
webserv/socket/Socket.hpp
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class Socket
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Socket();
|
||||||
|
Socket(int fd); // NOLINT readability-identifier-naming
|
||||||
|
|
||||||
|
Socket(const Socket &other) = delete; // Disable copy constructor
|
||||||
|
Socket &operator=(const Socket &other) = delete; // Disable copy assignment
|
||||||
|
Socket(Socket &&other) noexcept; // Move constructor
|
||||||
|
Socket &operator=(Socket &&other) noexcept; // Move assignment
|
||||||
|
|
||||||
|
~Socket();
|
||||||
|
|
||||||
|
void listen(int backlog) const;
|
||||||
|
void bind(const std::string &host, const int port) const;
|
||||||
|
[[nodiscard]] Socket accept() const;
|
||||||
|
ssize_t recv(void *buf, size_t len) const;
|
||||||
|
ssize_t send(const void *buf, size_t len) const;
|
||||||
|
void setNonBlocking() const;
|
||||||
|
[[nodiscard]] int getFd() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
int _fd;
|
||||||
|
};
|
||||||
Loading…
Reference in New Issue
Block a user