#include "v2g_context.h"


static int v2g_tls_client_send(struct v2g_connect* conn, uint8_t* buff, int length)
{
    return SSL_write(conn->ssl, buff, length);
}

static int v2g_tls_client_recv(struct v2g_connect* conn, uint8_t* buff, int length, int timeout)
{
    return SSL_read(conn->ssl, buff, length);
}

void v2g_tls_client_exit(struct v2g_connect* conn)
{
    if (conn)
    {
        if (conn->quit == 0)
        {
            conn->quit = 1;
            printd("de_pthread.\n");
            mthread_exit(&conn->thread);
        }
        if (conn->ssl)
        {
            SSL_shutdown(conn->ssl);
            SSL_free(conn->ssl);
            conn->ssl = NULL;
        }
        if (conn->tcp_socket > 0)
        {
            close(conn->tcp_socket);
            conn->tcp_socket = -1;
        }
    }
}

int v2g_tls_client_init(v2g_connect_t* conn, v2g_context_t* v2g)
{
    if (conn)
    {
        conn->quit = 0;
        conn->is_tls_connection = 1;
        conn->v2g = v2g;
        conn->v2g_connection_send = v2g_tls_client_send;
        conn->v2g_connection_recv = v2g_tls_client_recv;
        conn->v2g_connection_exit = v2g_tls_client_exit;
        conn->pubkey = NULL;
        printd("create_pthread.\n");
        mthread_init(&conn->thread, v2g_connection_loop, conn);
    }
}

