text: #include "netcommon.hh"
  
  #include <sys/socket.h>
  
  bool available(SOCKFD sockfd, uint16_t bytes, bool blocking) {
  	int n = 0;
  	char buf[bytes];
  
  	if(blocking) {
  		while((n = recv(sockfd, buf, bytes, MSG_PEEK)) < (int32_t) bytes) {
  			if(n == -1) {
  				perror("available()");
  				return false;
  			}
  		}
  
  		return true;
  	}
  
  	return !((n = recv(sockfd, buf, bytes, MSG_PEEK)) < (int32_t) bytes);
  }
  
  int deserializeEvent(BYTE * evtbuf, uint32_t evtlen, uint32_t eventtype, Event ** out_evt) {
  
  	ByteBuffer bbuf((BYTE *) evtbuf, evtlen);
  	Event * evt;
  
  	/* Construct correct Event */
  	switch(eventtype) {
  		case EventType::KEY: {
  			evt = new KeyEvent();
  			break;
  		}
  
  		case EventType::CLIENTINFO: {
  			evt = new ClientInfoEvent();
  			break;
  		}
  
  		default: {
  			return -1;
  		}
  	}
  
  	evt->Deserialize(&bbuf);
  
  	*out_evt = evt;
  	return 0;
  }
  
  int readmsg(SOCKFD sockfd, Event ** out_evt) {
  
  	/* Read Protocol Header (which is only the Length of the following Message/Event) */
  	int32_t msglen = 0;
  	char buf_int[4];
  
  	/* Wait until 4 Bytes are available */
  	available(sockfd, 4, true);
  
  	/* Take the Bytes from the Stream */
  	recv(sockfd, buf_int, 4, 0);
  
  	/* Convert Bytes to integer */
  	ByteBuffer bbuf_msglen((BYTE *) buf_int, 4);
  	bbuf_msglen.GetInt32(&msglen);
  
  	/* Wait until next 4 Bytes available to read the Event Type */
  	available(sockfd, 4, true);
  
  	/* Take the Bytes from the Stream */
  	recv(sockfd, buf_int, 4, 0);
  
  	/* Convert Bytes to integer */
  	int32_t eventtype = 0;
  	ByteBuffer bbuf_eventtype((BYTE *) buf_int, 4);
  	bbuf_eventtype.GetInt32(&eventtype);
  
  	/* Wait for the actual Event-Data */
  	int eventlen = msglen - 8;
  	char eventbuf[eventlen];
  	available(sockfd, eventlen, true);
  
  	/* Retrieve Event-Data from the Stream all at once */
  	recv(sockfd, eventbuf, eventlen, 0);
  
  	if(deserializeEvent((BYTE *) eventbuf, eventlen, eventtype, out_evt) == -1) {
  		printf("Received Event is not registered!\n");
  		return -1;
  	}
  
  	return 0;
  }
  
  int sendmsg(SOCKFD sockfd, Event * in_evt) {
  
  	/* Serialize the Event itself */
  	ByteBuffer eventBuffer(0);
  	in_evt->Serialize(&eventBuffer);
  
  	/* Create a Wrapper for the Event which contains the Message's actual Length */
  	/* This is here, so the Event doesn't have to handle this on Serialization on its own. */
  	ByteBuffer wrapBuffer(eventBuffer.GetLength() + 8);
  
  	/* Give Length and Type of Event to the Wrapper */
  	wrapBuffer.PutInt32(eventBuffer.GetLength() + 8);
  	wrapBuffer.PutInt32((int32_t) in_evt->GetType());
  	
  	/* Get the Event as a BYTE Array */
  	BYTE * buf;
  	uint32_t buflen;
  	eventBuffer.ToArray(&buf, &buflen);
  
  	/* Put BYTE Array into the Wrapper */
  	wrapBuffer.Put(buf, buflen);
  
  	/* From this Point on, the Event can be sent */
  
  	BYTE * msgBuffer;
  	uint32_t msgBufferLen;
  
  	wrapBuffer.ToArray(&msgBuffer, &msgBufferLen);
  
  	int n = send(sockfd, msgBuffer, msgBufferLen, 0);
  
  	if(n == -1) {
  		perror("sendmsg() at send()");
  		return -1;
  	} 
  
  	return 0;
  
  }