Beispiel #1
0
fa_buffer_t fa_buffer_create(size_t size)
{
    fa_ptr_t buffer_impl(fa_id_t interface);

    fa_buffer_t buffer = fa_new(buffer);

    buffer->impl = &buffer_impl;
    buffer->size = size;
    buffer->data = fa_malloc(size);
    buffer->ref_count = fa_atomic();
    buffer->marked_for_destruction = fa_atomic();
    
    //fa_slog_info("fa_buffer_create ", fa_i32(size));

    buffer->destroy_function = default_destroy;
    buffer->destroy_data     = NULL;

    buffer->meta = fa_map_empty();
    fa_map_set_value_destructor(buffer->meta, fa_destroy);

    memset(buffer->data, 0, buffer->size);

    if (!buffer->data) {
        if (errno == ENOMEM) {
            buffer_fatal("Out of memory", errno);
        } else {
            buffer_fatal("Unknown", errno);
        }
    }

    buffer_warn(fa_string("Buffer created"));
    return buffer;
}
Beispiel #2
0
fa_buffer_t fa_buffer_create(size_t size)
{
    fa_ptr_t buffer_impl(fa_id_t interface);

    buffer_t buffer = fa_new(buffer);

    buffer->impl = &buffer_impl;
    buffer->size = size;
    buffer->data = fa_malloc(size);

    buffer->destroy_function = default_destroy;
    buffer->destroy_data     = NULL;

    memset(buffer->data, 0, buffer->size);

    if (!buffer->data) {
        if (errno == ENOMEM) {
            buffer_fatal("Out of memory", errno);
        } else {
            buffer_fatal("Unknown", errno);
        }
    }

    return buffer;
}
Beispiel #3
0
fa_buffer_t fa_buffer_resize(size_t size, fa_buffer_t buffer)
{
    fa_ptr_t buffer_impl(fa_id_t interface);

    buffer_t copy           = fa_new(buffer);
    copy->impl              = &buffer_impl;
    copy->size              = size;
    copy->data              = fa_malloc(size);
    copy->destroy_function  = buffer->destroy_function;
    copy->destroy_data      = buffer->destroy_data;

    if (!copy->data) {
        if (errno == ENOMEM) {
            buffer_fatal("Out of memory", errno);
        } else {
            buffer_fatal("Unknown", errno);
        }
    }

    copy->data = memcpy(copy->data, buffer->data, size);
    return copy;
}
Beispiel #4
0
static int udpsrv_open(fa_trans_t *trans, char *hostname, int port)
{
	
    struct addrinfo hints, *cur_ai;
	udpsrv_context_t *s = NULL;
	int ret;
    char portstr[10];
	int fd = -1;
    int tmp;

	if (port <= 0 || port >= 65536) {
        FA_PRINT("FAIL: %s , [err at: %s-%d]\n", FA_ERR_NETWORK_PORTNO, __FILE__, __LINE__);
		return -1;	//port is not correct
	}

    s = fa_malloc(sizeof(udpsrv_context_t));
    if (!s) {
        FA_PRINT("FAIL: %s , [err at: %s-%d]\n", FA_ERR_SYS_NOMEM, __FILE__, __LINE__);
        return -1;
    }

    memset(&hints, 0, sizeof(hints));
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_DGRAM;
    snprintf(portstr, sizeof(portstr), "%d", port);
    ret = getaddrinfo(hostname, portstr, &hints, &(s->ai));	//cvt the hostname to the address(support ipv6)
    if (ret) {
        FA_PRINT("FAIL: %s , [err at: %s-%d]\n", FA_ERR_SYS_IO, __FILE__, __LINE__);
        goto fail;
    }
    cur_ai = s->ai;

    fd = socket(cur_ai->ai_family, cur_ai->ai_socktype, cur_ai->ai_protocol);
    if (fd < 0)
        goto fail;

    tmp = 1;
    setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof(tmp));

    if (bind (fd, cur_ai->ai_addr, cur_ai->ai_addrlen) < 0) {
        char bindmsg[32];
        /*snprintf(bindmsg, sizeof(bindmsg), "bind(port %d)", ntohs(cur_ai->ai_addr->sin_port));*/
        snprintf(bindmsg, sizeof(bindmsg), "bind fail");
//        perror (bindmsg);
        FA_PRINT("FAIL: %s , [err at: %s-%d]\n", bindmsg, __FILE__, __LINE__);
        closesocket(fd);
        return -1;
    }

#ifdef UDP_USE_BLOCK
    fa_socket_nonblock(fd, 0);
#else 
    fa_socket_nonblock(fd, 1);
#endif

    s->fd = fd;

    trans->priv_data = s;

    return 0;

fail:
    FA_PRINT("FAIL: %s , [err at: %s-%d]\n", FA_ERR_SYS_IO, __FILE__, __LINE__);
    if (fd >= 0)
        closesocket(fd);
    if (s) {
        if (s->ai)
            freeaddrinfo(s->ai);
        fa_free(s);
    }

    return -1;
}