Added basic server functionality from ex 2 lesson 11
This commit is contained in:
68
lib/utils/conn.h
Normal file
68
lib/utils/conn.h
Normal file
@ -0,0 +1,68 @@
|
||||
#ifndef CONN_H
|
||||
#define CONN_H
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/uio.h>
|
||||
#include <sys/un.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#if !defined(BUFSIZE)
|
||||
#define BUFSIZE 256
|
||||
#endif
|
||||
#if !defined(SOCKNAME)
|
||||
#define SOCKNAME "./cs_sock"
|
||||
#endif
|
||||
#if !defined(MAXBACKLOG)
|
||||
#define MAXBACKLOG 32
|
||||
#endif
|
||||
|
||||
/** Evita letture parziali
|
||||
*
|
||||
* \retval -1 errore (errno settato)
|
||||
* \retval 0 se durante la lettura da fd leggo EOF
|
||||
* \retval size se termina con successo
|
||||
*/
|
||||
static inline int readn(long fd, void *buf, size_t size) {
|
||||
size_t left = size;
|
||||
int r;
|
||||
char *bufptr = (char*)buf;
|
||||
while(left>0) {
|
||||
if ((r=read((int)fd ,bufptr,left)) == -1) {
|
||||
if (errno == EINTR) continue;
|
||||
return -1;
|
||||
}
|
||||
if (r == 0) return 0; // EOF
|
||||
left -= r;
|
||||
bufptr += r;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
/** Evita scritture parziali
|
||||
*
|
||||
* \retval -1 errore (errno settato)
|
||||
* \retval 0 se durante la scrittura la write ritorna 0
|
||||
* \retval 1 se la scrittura termina con successo
|
||||
*/
|
||||
static inline int writen(long fd, void *buf, size_t size) {
|
||||
size_t left = size;
|
||||
int r;
|
||||
char *bufptr = (char*)buf;
|
||||
while(left>0) {
|
||||
if ((r=write((int)fd ,bufptr,left)) == -1) {
|
||||
if (errno == EINTR) continue;
|
||||
return -1;
|
||||
}
|
||||
if (r == 0) return 0;
|
||||
left -= r;
|
||||
bufptr += r;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
#endif /* CONN_H */
|
||||
Reference in New Issue
Block a user