add write function to netsource

this is useful for listening sockets to also send responses,
i.e. implement bi-directional communication
master
Andre Puschmann 5 years ago
parent 4ac5448ad7
commit 4869509c7b

@ -67,6 +67,8 @@ SRSLTE_API int srslte_netsource_read(srslte_netsource_t *q,
void *buffer, void *buffer,
int nof_bytes); int nof_bytes);
SRSLTE_API int srslte_netsource_write(srslte_netsource_t* q, void* buffer, int nbytes);
SRSLTE_API int srslte_netsource_set_timeout(srslte_netsource_t *q, SRSLTE_API int srslte_netsource_set_timeout(srslte_netsource_t *q,
uint32_t microseconds); uint32_t microseconds);

@ -111,6 +111,22 @@ int srslte_netsource_read(srslte_netsource_t *q, void *buffer, int nbytes) {
} }
} }
int srslte_netsource_write(srslte_netsource_t* q, void* buffer, int nbytes)
{
// Loop until all bytes are sent
char* ptr = (char*)buffer;
while (nbytes > 0) {
ssize_t i = send(q->connfd, ptr, nbytes, 0);
if (i < 1) {
perror("Error calling send()\n");
return SRSLTE_ERROR;
}
ptr += i;
nbytes -= i;
}
return SRSLTE_SUCCESS;
}
int srslte_netsource_set_nonblocking(srslte_netsource_t *q) { int srslte_netsource_set_nonblocking(srslte_netsource_t *q) {
if (fcntl(q->sockfd, F_SETFL, O_NONBLOCK)) { if (fcntl(q->sockfd, F_SETFL, O_NONBLOCK)) {
perror("fcntl"); perror("fcntl");

Loading…
Cancel
Save