<commit_before>// net.c

#include "net.h"

int _mqx_io_tx(_TlsSocket *s, char *, int, void *);
int _mqx_io_rx(_TlsSocket *s, char *, int, void *);

int uzmtp_net_connect(_UzmtpSocket *s, const char *ip, int port) {
    struct in_addr binip;
    struct sockaddr_in addr;

    s->sock = socket(AF_INET, SOCK_STREAM, 0);
    if (s->sock == RTCS_SOCKET_ERROR) return -1;
    inet_pton(AF_INET, ip, &binip, sizeof(binip));
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = port;
    addr.sin_addr = binip;
    uint32_t is_err = 0;
    uint32_t option = 25000;
    is_err |= setsockopt(s->sock, SOL_TCP, OPT_SEND_TIMEOUT, &option,  //
			 sizeof(option));
    option = 2750;
    is_err |= setsockopt(s->sock, SOL_TCP, OPT_CONNECT_TIMEOUT, &option,
			 sizeof(option));
    option = 25000;
    is_err |= setsockopt(s->sock, SOL_TCP, OPT_RECEIVE_TIMEOUT, &option,
			 sizeof(option));
    option = FALSE;
    is_err |= setsockopt(s->sock, SOL_TCP, OPT_RECEIVE_NOWAIT, &option,  //
			 sizeof(option));
    option = FALSE;
    is_err |= setsockopt(s->sock, SOL_TCP, OPT_SEND_NOWAIT, &option,  //
			 sizeof(option));
    option = TRUE;
    is_err |= setsockopt(s->sock, SOL_TCP, OPT_RECEIVE_PUSH, &option,  //
			 sizeof(option));
    option = 2048;
    is_err |= setsockopt(s->sock, SOL_TCP, OPT_TBSIZE, &option,  //
			 sizeof(option));
    option = 2048;
    is_err |= setsockopt(s->sock, SOL_TCP, OPT_RBSIZE, &option,  //
			 sizeof(option));

    const int rc = connect(s->sock, &addr, sizeof(addr));
    if (rc != RTCS_OK) {
	// freeaddrinfo(addrinfo);
	uint32_t err = RTCS_geterror(s->sock);
	((void)err);
	shutdown(s->sock, FLAG_ABORT_CONNECTION);
	return -1;
    }
    return 0;
}

int uzmtp_tls_connect(_TlsCtx **ctx, _UzmtpSocket *sock) {
    sock->tls = tls_connect(ctx, sock->sock);
    if (!sock->tls) {
	return -1;
    } else {
	return 0;
    }
}
int uzmtp_net_socket(_UzmtpSocket *s) { return s->sock; }

int uzmtp_net_recv(_UzmtpSocket *s, unsigned char *b, size_t len) {
    return uzmtp_net_recv_fd(s->sock, b, len);
}
int uzmtp_net_recv_fd(int sockfd, unsigned char *b, size_t len) {
    int bytes_read = 0;
    while (bytes_read < len) {
	const int n = recv(sockfd, (char *)b + bytes_read, (len - bytes_read),
			   MSG_DONTWAIT);
	if (n == -1) {
	    uint32_t error = RTCS_geterror(sockfd);
	    ((void)error);
	    break;
	} else if (n == 0) {
	    break;
	} else {
	    bytes_read += n;
	}
    }
    return bytes_read;
}

int uzmtp_net_send(_UzmtpSocket *s, const unsigned char *b, size_t len) {
    return uzmtp_net_send_fd(s->sock, b, len);
}

int uzmtp_net_send_fd(int sockfd, const unsigned char *b, size_t len) {
    int bytes_sent = 0;
    while (bytes_sent < len) {
	const int rc =
	    send(sockfd, (char *)b + bytes_sent, (len - bytes_sent), 0);
	if (rc == 0) {
	    break;
	} /*!<socket closed */
	else if (rc == -1) {
	    if (errno == EINTR) continue;
	    bytes_sent = -1;
	    break; /*!<Socket error */
	} else
	    bytes_sent += rc;
    }
    return bytes_sent;
}

int uzmtp_net_select(int *sock, int nsock, int time) {
    return RTCS_selectset(sock, nsock, time);
}<commit_msg>added select inside read loop is massive help on MQX TCP performance...<commit_after>// net.c

#include "net.h"

int _mqx_io_tx(_TlsSocket *s, char *, int, void *);
int _mqx_io_rx(_TlsSocket *s, char *, int, void *);

int uzmtp_net_connect(_UzmtpSocket *s, const char *ip, int port) {
    struct in_addr binip;
    struct sockaddr_in addr;

    s->sock = socket(AF_INET, SOCK_STREAM, 0);
    if (s->sock == RTCS_SOCKET_ERROR) return -1;
    inet_pton(AF_INET, ip, &binip, sizeof(binip));
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = port;
    addr.sin_addr = binip;
    uint32_t is_err = 0;
    uint32_t option = 25000;
    is_err |= setsockopt(s->sock, SOL_TCP, OPT_SEND_TIMEOUT, &option,  //
			 sizeof(option));
    option = 2750;
    is_err |= setsockopt(s->sock, SOL_TCP, OPT_CONNECT_TIMEOUT, &option,
			 sizeof(option));
    option = 25000;
    is_err |= setsockopt(s->sock, SOL_TCP, OPT_RECEIVE_TIMEOUT, &option,
			 sizeof(option));
    option = FALSE;
    is_err |= setsockopt(s->sock, SOL_TCP, OPT_RECEIVE_NOWAIT, &option,  //
			 sizeof(option));
    option = FALSE;
    is_err |= setsockopt(s->sock, SOL_TCP, OPT_SEND_NOWAIT, &option,  //
			 sizeof(option));
    option = TRUE;
    is_err |= setsockopt(s->sock, SOL_TCP, OPT_RECEIVE_PUSH, &option,  //
			 sizeof(option));
    option = 2048;
    is_err |= setsockopt(s->sock, SOL_TCP, OPT_TBSIZE, &option,  //
			 sizeof(option));
    option = 2048;
    is_err |= setsockopt(s->sock, SOL_TCP, OPT_RBSIZE, &option,  //
			 sizeof(option));

    const int rc = connect(s->sock, &addr, sizeof(addr));
    if (rc != RTCS_OK) {
	// freeaddrinfo(addrinfo);
	uint32_t err = RTCS_geterror(s->sock);
	((void)err);
	shutdown(s->sock, FLAG_ABORT_CONNECTION);
	return -1;
    }
    return 0;
}

int uzmtp_tls_connect(_TlsCtx **ctx, _UzmtpSocket *sock) {
    sock->tls = tls_connect(ctx, sock->sock);
    if (!sock->tls) {
	return -1;
    } else {
	return 0;
    }
}
int uzmtp_net_socket(_UzmtpSocket *s) { return s->sock; }

int uzmtp_net_recv(_UzmtpSocket *s, unsigned char *b, size_t len) {
    return uzmtp_net_recv_fd(s->sock, b, len);
}
int uzmtp_net_recv_fd(int sockfd, unsigned char *b, size_t len) {
    int bytes_read = 0;
    while (bytes_read < len) {
    	int active = uzmtp_net_select(&sockfd, 1, 100);
    	if(active == sockfd){
    		const int n = recv(sockfd, (char *)b + bytes_read, (len - bytes_read),
    				MSG_DONTWAIT);
    		if (n == -1) {
    			uint32_t error = RTCS_geterror(sockfd);
    			if(error == RTCSERR_TCP_TIMED_OUT) continue;
    			break;
    		} else if (n == 0) {
    			break;
    		} else {
    			bytes_read += n;
    		}
    	}else{
    		break;
    	}
    }
    return bytes_read;
}

int uzmtp_net_send(_UzmtpSocket *s, const unsigned char *b, size_t len) {
    return uzmtp_net_send_fd(s->sock, b, len);
}

int uzmtp_net_send_fd(int sockfd, const unsigned char *b, size_t len) {
    int bytes_sent = 0;
    while (bytes_sent < len) {
	const int rc =
	    send(sockfd, (char *)b + bytes_sent, (len - bytes_sent), 0);
	if (rc == 0) {
	    break;
	} /*!<socket closed */
	else if (rc == -1) {
	    if (errno == EINTR) continue;
	    bytes_sent = -1;
	    break; /*!<Socket error */
	} else
	    bytes_sent += rc;
    }
    return bytes_sent;
}

int uzmtp_net_select(int *sock, int nsock, int time) {
    return RTCS_selectset(sock, nsock, time);
}
{'fraction_non_alphanumeric': 0.14622569284948705, 'fraction_numerical': 0.014699127239320165, 'mean_word_length': 3.163161249203314, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '197912', 'n_tokens_mistral': 2754, 'n_tokens_neox': 2592, 'n_words': 596}