static ebb_connection *ebb_connection_create(const struct sockaddr_in *addr)
{
    ebb_connection *conn;
    ebb_connection_info *conninfo;

    if (NULL == (conn = (ebb_connection *)calloc(1, sizeof(ebb_connection))))
        goto fail;
    if (NULL == (conninfo = ebb_connection_info_create()))
        goto fail;

    ebb_connection_init(conn);
    conn->data = (void *)conninfo;
    conninfo->conn = conn;

    strncpy(conninfo->ipaddr, inet_ntoa(addr->sin_addr), sizeof(conninfo->ipaddr));

    // don't reuse connids
    do
    {
        conninfo->connid = g_srvctx.httpdconnid++;
    }   // this won't spin forever as there will never be > 2^32 connections
    while(httpdconnlist_find(conninfo->connid) != NULL);

    conn->new_request = ebb_request_create;
    conn->on_close = on_close;
    httpd_open_cb(conninfo->connid);
    return conn;
fail:
    ebb_connection_destroy(conn);
    return NULL;
}
Esempio n. 2
0
ebb_connection *new_connection(ebb_server *server, struct sockaddr_in *addr) {
  ebb_connection *connection = (ebb_connection *)malloc(sizeof(ebb_connection));
  if (connection == nullptr) {
    return nullptr;
  }

  AsyncConnection *connection_data = new AsyncConnection;
  connection_data->addr = *addr;
  
  // Initializes the connection
  ebb_connection_init(connection);
  connection->data = connection_data;
  connection->new_request = new_request;
  connection->on_close = on_close;
  connection->on_timeout = on_timeout;

  connection_data->ev_loop = server->loop;
  connection_data->ev_write.data = connection_data;

  return connection;
}