Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <sys/un.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <netdb.h>
- #include <arpa/inet.h>
- #include <stdexcept>
- #include <vector>
- #include <iostream>
- typedef std::vector<char> Buffer;
- void appendTo (Buffer *buffer, char *begin, size_t size) {
- buffer->insert(buffer->end(), begin, begin + size);
- }
- class OkOrException {
- private:
- bool is_good_;
- std::runtime_error exception_;
- public:
- OkOrException() :
- is_good_(true),
- exception_(std::runtime_error(""))
- {}
- void setException(const std::runtime_error &exception) {
- is_good_ = false;
- exception_ = exception;
- }
- void check() const {
- if (!is_good_) {
- throw exception_;
- }
- }
- };
- typedef OkOrException Status;
- struct InternetAddress {
- private:
- std::string address_;
- int port_;
- public:
- explicit InternetAddress(std::string address, int port) :
- address_(address),
- port_(port)
- {}
- struct sockaddr getSocketAddress() const {
- struct sockaddr_in address;
- address.sin_family = AF_INET;
- address.sin_port = htons(port_);
- address.sin_addr.s_addr = inet_addr(address_.c_str());
- memset(address.sin_zero, '\0', sizeof address.sin_zero);
- return *((sockaddr *) &address);
- }
- ~InternetAddress() {}
- };
- class ConnectionSocket {
- private:
- int connection_fd_;
- Status status_;
- bool have_resources_;
- public:
- ConnectionSocket(int connection_fd) :
- connection_fd_(connection_fd),
- have_resources_(true)
- {}
- ConnectionSocket(const ConnectionSocket &) = delete;
- ConnectionSocket &operator=(const ConnectionSocket &) = delete;
- ConnectionSocket(ConnectionSocket &&other) {
- connection_fd_ = other.connection_fd_;
- status_ = std::move(other.status_);
- other.have_resources_ = false;
- have_resources_ = true;
- }
- Buffer read() {
- constexpr int BUFFER_SIZE = 100;
- char buffer[BUFFER_SIZE];
- Buffer result;
- while (true) {
- memset(buffer, 0, BUFFER_SIZE);
- int recv_result = recv(connection_fd_, buffer, BUFFER_SIZE, 0);
- appendTo(&result, buffer, BUFFER_SIZE);
- if (recv_result < BUFFER_SIZE) {
- break;
- }
- }
- std::cout << "1\n";
- return std::move(result);
- }
- void write(Buffer buffer) {
- send(connection_fd_, buffer.data(), buffer.size(), 0);
- }
- ~ConnectionSocket() {
- if (have_resources_) {
- ::close(connection_fd_);
- }
- }
- };
- class ServerSocket {
- private:
- int fd_;
- Status status_;
- void initSocket() {
- fd_ = ::socket(AF_INET, SOCK_STREAM, 0);
- if (fd_ < 0) {
- throw std::runtime_error("Failed to create socket");
- }
- }
- void bindToAddress(const InternetAddress &internet_address) {
- struct sockaddr address = internet_address.getSocketAddress();
- int bound = ::bind(fd_, (struct sockaddr *) &address, sizeof(address));
- if (bound == -1) {
- throw std::runtime_error("Failed to bind socket");
- }
- }
- void listenTo() {
- int listen_result = ::listen(fd_, 10);
- if (listen_result == -1) {
- throw std::runtime_error("Failed to listen");
- }
- }
- void closeSocket() {
- ::close(fd_);
- //::unlink(fd_);
- }
- public:
- ServerSocket(const InternetAddress &internet_address) {
- try {
- initSocket();
- bindToAddress(internet_address);
- listenTo();
- } catch(const std::runtime_error &exception) {
- status_.setException(exception);
- }
- }
- ConnectionSocket newConnection() {
- int new_fd = ::accept(fd_, 0, 0);
- if (new_fd < 0) {
- throw std::runtime_error("Failed to accept socket");
- }
- return std::move(ConnectionSocket(new_fd));
- }
- ServerSocket(const ServerSocket &) = delete;
- ServerSocket &operator=(const ServerSocket &) = delete;
- void check() const {
- status_.check();
- }
- ~ServerSocket() {
- closeSocket();
- }
- };
- int main() {
- static constexpr char LOCALHOST[] = "127.0.0.1";
- static constexpr int DEFAULT_PORT = 8992;
- InternetAddress address = InternetAddress(LOCALHOST, DEFAULT_PORT);
- ServerSocket server_socket(address);
- try {
- server_socket.check();
- } catch (const std::exception &ex) {
- std::cerr << ex.what() << std::endl;
- return 1;
- }
- std::string respond("HTTP/1.1 200 OK\n\n<html>hello<html>\n");
- Buffer respond_buffer = Buffer(respond.begin(), respond.end());
- try {
- while (true) {
- ConnectionSocket connection_socket(server_socket.newConnection());
- Buffer read_buffer = connection_socket.read();
- connection_socket.write(respond_buffer);
- }
- } catch (const std::exception &ex) {
- std::cerr << ex.what() << std::endl;
- return 1;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment