sockets: recv

This commit is contained in:
Matheus
2022-10-18 13:28:06 -03:00
parent 2b9cdff5ba
commit 1a48d652f5
3 changed files with 62 additions and 3 deletions
+3
View File
@@ -146,6 +146,9 @@ int vfs_read(int* error, vnode_t* node, void* buff, size_t count, size_t offset)
int type = GETTYPE(node->st.st_mode);
if(type == TYPE_SOCKET)
return socket_recv(node->objdata, buff, count, 0, error);
if(type == TYPE_FIFO){
return pipe_read(node->objdata, buff, count, error);
}
+9
View File
@@ -92,3 +92,12 @@ int socket_send(socket_t* socket, void* buff, size_t len, int flags, int* count)
return EINVAL;
}
}
int socket_recv(socket_t* socket, void* buff, size_t len, int flags, int* count){
switch(socket->family){
case AF_UNIX:
return unsocket_recv(socket, buff, len, flags, count);
default:
return EINVAL;
}
}
+50 -3
View File
@@ -213,14 +213,12 @@ int unsocket_accept(socket_t** peer, socket_t* sock, void* addr, size_t* addrlen
}
int unsocket_send(socket_t* socket, void* buff, size_t len, int flags, int* error){
if(flags){ // right now no flags are supported
*error = EOPNOTSUPP;
return 0;
}
int err = 0;
int writec = 0;
socket_t* peer = socket->peer;
@@ -265,3 +263,52 @@ int unsocket_send(socket_t* socket, void* buff, size_t len, int flags, int* erro
return writec;
}
int unsocket_recv(socket_t* socket, void* buff, size_t len, int flags, int* error){
if(flags){ // right now no flags are supported
*error = EOPNOTSUPP;
return 0;
}
int readc = 0;
spinlock_acquire(&socket->lock);
if(socket->state != SOCKET_STATE_CONNECTED){
*error = ENOTCONN;
goto _return;
}
for(;;){
arch_interrupt_disable();
readc = ringbuffer_read(&socket->buffer, buff, len);
if(readc > 0){
event_signal(&socket->dataevent, true);
break;
}
spinlock_release(&socket->lock);
if(event_wait(&socket->dataevent, true)){
spinlock_acquire(&socket->lock);
readc = -1;
*error = EINTR;
break;
}
spinlock_acquire(&socket->lock);
}
*error = 0;
_return:
spinlock_release(&socket->lock);
return readc;
}