/* common.c - Common functions used by all programs/modules
*
* (c) Tiago Alves Macambira, 2003
* 
* $Id: common.c,v 1.1 2003/08/04 03:09:43 tmacam Exp $
*
* Parts of this code are base on the work of Brian "Beej" Hall
* See http://www.ecst.csuchico.edu/~beej/guide/net
*
*/

#include <sys/types.h>
#include <sys/socket.h>

/* Medição de tempo */
#include <sys/time.h>

#include "common.h"

int sendall(int s, char *buf, int *len)
{
	int total = 0;		// how many bytes we've sent
	int bytesleft = *len;	// how many we have left to send
	int n;

	while (total < *len) {
		n = send(s, buf + total, bytesleft, MSG_NOSIGNAL);
		if (n == -1) {
			break;
		}
		total += n;
		bytesleft -= n;
	}

	*len = total;		// return number actually sent here

	return n == -1 ? -1 : 0;	// return -1 on failure, 0 on success
}

/* get_long_time
 *
 * return       - Retorna o tempo atual com precisão em us como um long
 */
unsigned long get_long_time()
{
        struct timeval v;

	gettimeofday(&v,NULL);
        return v.tv_sec * 1000000 + v.tv_usec;
}
