socket.c 1.26 KB
#include <stdio.h>      /* for printf() and fprintf() */
#include <sys/types.h>  /* SO_REUSEADDR */
#include <sys/socket.h> /* for socket(), bind(), and connect() */
#include <arpa/inet.h>  /* for sockaddr_in and inet_ntoa() */
#include <stdlib.h>     /* for atoi() and exit() */
#include <string.h>     /* for memset() */
#include <unistd.h>     /* for close() */
#include <errno.h>      /* for errno */
#include <stdarg.h>

#include "logger.h"
#include "cclass.h"
#include "socket.h"


INIT_CLASS(SOCK);

__construct(SOCK)
{
    int reUse = 1;  /* TODO: make this configurable */

	this->logger = va_arg(* params, LOGGER);
	this->port   = va_arg(* params, int);

    /* Create socket for incoming connections */
    if (-1 == (this->handle = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))) {
        logger_log(this->logger, LOGGER_CRIT,
                "error opening socket: %s - service terminated",
                strerror(errno));
        exit(EXIT_FAILURE);
    }

    /* Make the socket REUSE a TIME_WAIT socket */
    setsockopt(this->handle, SOL_SOCKET, SO_REUSEADDR, &reUse, sizeof (reUse));
}

__destruct(SOCK)
{
	if (0 != this->handle) {
		shutdown(this->handle, SHUT_RDWR);
		close(this->handle);
	}
}

__jsonConst(SOCK) {}
__toJson(SOCK) {}
__clear(SOCK) {}

// vim: set ts=4 sw=4: