Пример #1
0
void socket_destroy(struct socket_t* s) {
    
    socket_close(s);
    
    mutex_lock(s->mutex);
    
    if (s->local_end_point != NULL) {
        sockaddr_destroy(s->local_end_point);
        s->local_end_point = NULL;
    }
    
    if (s->remote_end_point != NULL) {
        sockaddr_destroy(s->remote_end_point);
        s->remote_end_point = NULL;
    }
    
    if (s->name != NULL) {
        free(s->name);
        s->name = NULL;
    }
    
    mutex_unlock(s->mutex);
    
    mutex_destroy(s->mutex);
    
    free(s);
    
}
Пример #2
0
void rtp_recorder_destroy(struct rtp_recorder_t* rr) {
    
    mutex_lock(rr->timer_mutex);
    
    if (rr->synchronization_thread != NULL) {
        condition_signal(rr->timer_cond);
        mutex_unlock(rr->timer_mutex);
        thread_join(rr->synchronization_thread);
        mutex_lock(rr->timer_mutex);
        thread_destroy(rr->synchronization_thread);
        rr->synchronization_thread = NULL;
    }
    
    mutex_unlock(rr->timer_mutex);
    
    rtp_socket_destroy(rr->streaming_socket);
    rtp_socket_destroy(rr->control_socket);
    rtp_socket_destroy(rr->timing_socket);
    
    sockaddr_destroy(rr->remote_control_end_point);
    sockaddr_destroy(rr->remote_timing_end_point);
    
    mutex_destroy(rr->timer_mutex);
    condition_destroy(rr->timer_cond);
    
    free(rr);
    
}
Пример #3
0
void rtp_socket_destroy(struct rtp_socket_t* rs) {
    
    mutex_lock(rs->mutex);
    
    while (rs->sockets_count > 0) {
        mutex_unlock(rs->mutex);
        socket_close(rs->sockets[0]->socket);
        mutex_lock(rs->mutex);
    }
    
    free(rs->sockets);
    
    mutex_unlock(rs->mutex);
    
    if (rs->allowed_remote_end_point)
        sockaddr_destroy(rs->allowed_remote_end_point);
    
    if (rs->name)
        free(rs->name);
    
    mutex_destroy(rs->mutex);
    
    free(rs);
    
}
Пример #4
0
rtp_socket_p _rtp_recorder_create_socket(struct rtp_recorder_t* rr, const char* name, struct sockaddr* local_end_point, struct sockaddr* remote_end_point) {
    
    rtp_socket_p ret = rtp_socket_create(name, remote_end_point);
    
    unsigned short p;
    for (p = 6000 ; p < 6100 ; p++) {
        struct sockaddr* ep = sockaddr_copy(local_end_point);
        sockaddr_set_port(ep, p);
        if (rtp_socket_setup(ret, ep)) {
            rtp_socket_set_data_received_callback(ret, _rtp_recorder_socket_data_received_callback, rr);
            log_message(LOG_INFO, "Setup socket on port %u", p);
            sockaddr_destroy(ep);
            return ret;
        }
        sockaddr_destroy(ep);
    }
    
    log_message(LOG_ERROR, "Unable to bind socket.");
    
    rtp_socket_destroy(ret);
    
    return NULL;
    
}
Пример #5
0
void socket_connect(struct socket_t* s, struct sockaddr* end_point) {
    
    if (!s->is_connected && !s->is_udp) {
        
        if (s->socket <= 0)
            s->socket = socket(end_point->sa_family, SOCK_STREAM, IPPROTO_TCP);
        
        if (s->socket <= 0)
            log_message(LOG_ERROR, "Socket creation error: %s", strerror(errno));
        
        if (s->remote_end_point != NULL)
            sockaddr_destroy(s->remote_end_point);
        
        s->remote_end_point = sockaddr_copy(end_point);
        
        s->receive_thread = thread_create(_socket_connect, s);
        
    }
    
}
Пример #6
0
bool socket_bind(struct socket_t* s, struct sockaddr* end_point) {
    
    assert(end_point != NULL);
    
    if (s->local_end_point != NULL) {
        sockaddr_destroy(s->local_end_point);
        s->local_end_point = NULL;
    }
    
    struct sockaddr* ep = sockaddr_copy(end_point);
    s->local_end_point = ep;
    
    if (s->socket < 0) {
        
        s->socket = socket(ep->sa_family, (s->is_udp ? SOCK_DGRAM : SOCK_STREAM), (s->is_udp ? IPPROTO_UDP : IPPROTO_TCP));
        
        if (s->socket < 0) {
            log_message(LOG_ERROR, "Socket creation error: %s", strerror(errno));
            return false;
        }
        
        if (sockaddr_is_ipv6(ep)) {
            int32_t on = 1;
            setsockopt(s->socket, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on));
        }
        
    }
    
    if (!s->is_udp) {
        int so_reuseaddr = 1;
        setsockopt(s->socket, SOL_SOCKET, SO_REUSEADDR, &so_reuseaddr, sizeof(int));
    }
    
    if (bind(s->socket, ep, ep->sa_len) == 0)
        return true;
    
    
    return false;
    
}
Пример #7
0
socket_p _web_server_bind(struct web_server_t* ws, uint16_t port, sockaddr_type socket_type) {

    socket_p socket = NULL;

    if (ws->socket_types & socket_type) {

        socket = socket_create("Web server", false);
        struct sockaddr* end_point = sockaddr_create(NULL, port, socket_type, 0);

        bool ret = socket_bind(socket, end_point);

        sockaddr_destroy(end_point);

        if (!ret) {
            socket_destroy(socket);
            socket = NULL;
        }

    }

    return socket;

}
Пример #8
0
void _socket_receive_loop(void* ctx) {
    
    struct socket_t* s = (struct socket_t*)ctx;
    
    mutex_lock(s->mutex);
    _socket_set_loop_name(s, "Receive Loop");
    
    s->is_connected = true;
    
    void* buffer = NULL;
    size_t buffer_size = 0;
    size_t write_pos = 0;
    ssize_t processed = 0;
    
    ssize_t read = 0;
    
    do {
        
        if (buffer_size - write_pos < 16384) {
            buffer_size += 16384;
            buffer = realloc(buffer, buffer_size);
        }
        
        if (s->is_udp) {
            
            struct sockaddr_storage remote_addr;
            socklen_t remote_addr_len = sizeof(struct sockaddr_storage);
            mutex_unlock(s->mutex);
            read = recvfrom(s->socket, buffer + write_pos, buffer_size - write_pos, 0, (struct sockaddr*) &remote_addr, &remote_addr_len);
            mutex_lock(s->mutex);
            
            if (s->remote_end_point != NULL) {
                sockaddr_destroy(s->remote_end_point);
                s->remote_end_point = NULL;
            }
            
            s->remote_end_point = sockaddr_copy((struct sockaddr*) &remote_addr);
            
        } else {
            mutex_unlock(s->mutex);
            read = recv(s->socket, buffer + write_pos, buffer_size - write_pos, 0);
            mutex_lock(s->mutex);
        }
        
        if (read > 0) {
            
            write_pos += read;
            processed = write_pos;
            
            if (s->callbacks.receive != NULL) {
                mutex_unlock(s->mutex);
                processed = s->callbacks.receive(s, buffer, write_pos, s->remote_end_point, s->callbacks.ctx.receive);
                mutex_lock(s->mutex);
            }
            
            if (processed > 0) {
                memcpy(buffer, buffer + processed, write_pos - processed);
                write_pos -= processed;
            }
            
        }
        
    } while (read > 0 && processed >= 0);
    
    if (buffer != NULL)
        free(buffer);
    
    mutex_unlock(s->mutex);
    
    socket_close(s);
    
}