Ejemplo n.º 1
0
//===========================================================================
//
// Parameter:				-
// Returns:					-
// Changes Globals:		-
//===========================================================================
int WINS_OpenReliableSocket( int port ){
	int newsocket;
	struct sockaddr_in address;
	qboolean _true = 0xFFFFFFFF;

	//IPPROTO_TCP
	//
	if ( ( newsocket = socket( AF_INET, SOCK_STREAM, 0 ) ) == -1 ) {
		WinPrint( "WINS_OpenReliableSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
		return -1;
	} //end if

	memset( (char *) &address, 0, sizeof( address ) );
	address.sin_family = AF_INET;
	address.sin_addr.s_addr = htonl( INADDR_ANY );
	address.sin_port = htons( (u_short)port );
	if ( bind( newsocket, (void *)&address, sizeof( address ) ) == -1 ) {
		WinPrint( "WINS_OpenReliableSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
		closesocket( newsocket );
		return -1;
	} //end if

	//
	if ( setsockopt( newsocket, IPPROTO_TCP, TCP_NODELAY, (void *) &_true, sizeof( int ) ) == -1 ) {
		WinPrint( "WINS_OpenReliableSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
		WinPrint( "setsockopt error\n" );
	} //end if

	return newsocket;
} //end of the function WINS_OpenReliableSocket
Ejemplo n.º 2
0
//===========================================================================
//
// Parameter:				-
// Returns:					-
// Changes Globals:		-
//===========================================================================
int WINS_OpenSocket( int port ){
	int newsocket;
	struct sockaddr_in address;
	u_long _true = 1;

	if ( ( newsocket = socket( PF_INET, SOCK_DGRAM, IPPROTO_UDP ) ) == SOCKET_ERROR ) {
		WinPrint( "WINS_OpenSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
		return -1;
	} //end if

	if ( ioctlsocket( newsocket, FIONBIO, &_true ) == SOCKET_ERROR ) {
		WinPrint( "WINS_OpenSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
		closesocket( newsocket );
		return -1;
	} //end if

	memset( (char *) &address, 0, sizeof( address ) );
	address.sin_family = AF_INET;
	address.sin_addr.s_addr = INADDR_ANY;
	address.sin_port = htons( (u_short)port );
	if ( bind( newsocket, (void *)&address, sizeof( address ) ) == -1 ) {
		WinPrint( "WINS_OpenSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
		closesocket( newsocket );
		return -1;
	} //end if

	return newsocket;
} //end of the function WINS_OpenSocket
Ejemplo n.º 3
0
//===========================================================================
//
// Parameter:				-
// Returns:					-
// Changes Globals:		-
//===========================================================================
int WINS_Listen( int socket ){
	u_long _true = 1;

	if ( ioctlsocket( socket, FIONBIO, &_true ) == -1 ) {
		WinPrint( "WINS_Listen: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
		return -1;
	} //end if
	if ( listen( socket, SOMAXCONN ) == SOCKET_ERROR ) {
		WinPrint( "WINS_Listen: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
		return -1;
	} //end if
	return 0;
} //end of the function WINS_Listen
Ejemplo n.º 4
0
//===========================================================================
//
// Parameter:				-
// Returns:					-
// Changes Globals:		-
//===========================================================================
int WINS_Connect( int socket, struct sockaddr_s *addr ){
	int ret;
	u_long _true2 = 0xFFFFFFFF;

	ret = connect( socket, (struct sockaddr *)addr, sizeof( struct sockaddr_s ) );
	if ( ret == SOCKET_ERROR ) {
		WinPrint( "WINS_Connect: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
		return -1;
	} //end if
	if ( ioctlsocket( socket, FIONBIO, &_true2 ) == -1 ) {
		WinPrint( "WINS_Connect: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
		return -1;
	} //end if
	return 0;
} //end of the function WINS_Connect
Ejemplo n.º 5
0
//===========================================================================
// returns the number of bytes read
// 0 if no bytes available
// -1 on failure
//
// Parameter:				-
// Returns:					-
// Changes Globals:		-
//===========================================================================
int WINS_Read(int socket, byte *buf, int len, struct sockaddr_s *addr)
{
	int addrlen = sizeof (struct sockaddr_s);
	int ret, errno;

	if (addr)
	{
		ret = recvfrom(socket, buf, len, 0, (struct sockaddr *)addr, &addrlen);
		if (ret == -1)
		{
			errno = WSAGetLastError();

			if (errno == WSAEWOULDBLOCK || errno == WSAECONNREFUSED)
				return 0;
		} //end if
	} //end if
	else
	{
		ret = recv(socket, buf, len, 0);
		if (ret == SOCKET_ERROR)
		{
			errno = WSAGetLastError();

			if (errno == WSAEWOULDBLOCK || errno == WSAECONNREFUSED)
				return 0;
		} //end if
	} //end else
	if (ret == SOCKET_ERROR)
	{
		WinPrint("WINS_Read: %s\n", WINS_ErrorMessage(WSAGetLastError()));
	} //end if
	return ret;
} //end of the function WINS_Read
Ejemplo n.º 6
0
//===========================================================================
//
// Parameter:				-
// Returns:					-
// Changes Globals:		-
//===========================================================================
int WINS_Accept( int socket, struct sockaddr_s *addr ){
	socklen_t addrlen = sizeof( struct sockaddr_s );
	int newsocket;
	qboolean _true = 1;

	newsocket = accept( socket, (struct sockaddr *)addr, &addrlen );
	if ( newsocket == INVALID_SOCKET ) {
		if ( errno == EAGAIN ) {
			return -1;
		}
		WinPrint( "WINS_Accept: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
		return -1;
	} //end if
	  //
	if ( setsockopt( newsocket, IPPROTO_TCP, TCP_NODELAY, (void *) &_true, sizeof( int ) ) == SOCKET_ERROR ) {
		WinPrint( "WINS_Accept: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
		WinPrint( "setsockopt error\n" );
	} //end if
	return newsocket;
} //end of the function WINS_Accept
Ejemplo n.º 7
0
//===========================================================================
//
// Parameter:				-
// Returns:					-
// Changes Globals:		-
//===========================================================================
int WINS_CloseSocket( int socket ){
	/*
	   if (socket == net_broadcastsocket)
	    net_broadcastsocket = 0;
	 */
//	shutdown(socket, SD_SEND);

	if ( closesocket( socket ) == SOCKET_ERROR ) {
		WinPrint( "WINS_CloseSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
		return SOCKET_ERROR;
	} //end if
	return 0;
} //end of the function WINS_CloseSocket
Ejemplo n.º 8
0
//===========================================================================
// returns qtrue on success or qfalse on failure
//
// Parameter:				-
// Returns:					-
// Changes Globals:		-
//===========================================================================
int WINS_Write( int socket, byte *buf, int len, struct sockaddr_s *addr ){
	int ret, written;
	ret = 0;

	if ( addr ) {
		written = 0;
		while ( written < len )
		{
			ret = sendto( socket, &buf[written], len - written, 0, (struct sockaddr *)addr, sizeof( struct sockaddr_s ) );
			if ( ret == SOCKET_ERROR ) {
				if ( WSAGetLastError() != EAGAIN ) {
					return qfalse;
				}
				//++timo FIXME: what is this used for?
//				Sleep(1000);
			} //end if
			else
			{
				written += ret;
			}
		}
	} //end if
	else
	{
		written = 0;
		while ( written < len )
		{
			ret = send( socket, buf, len, 0 );
			if ( ret == SOCKET_ERROR ) {
				if ( WSAGetLastError() != EAGAIN ) {
					return qfalse;
				}
				//++timo FIXME: what is this used for?
//				Sleep(1000);
			} //end if
			else
			{
				written += ret;
			}
		}
	} //end else
	if ( ret == SOCKET_ERROR ) {
		WinPrint( "WINS_Write: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
	} //end if
	return ( ret == len );
} //end of the function WINS_Write
Ejemplo n.º 9
0
//===========================================================================
// returns the number of bytes read
// 0 if no bytes available
// -1 on failure
//
// Parameter:				-
// Returns:					-
// Changes Globals:		-
//===========================================================================
int WINS_Read( int socket, byte *buf, int len, struct sockaddr_s *addr ){
	socklen_t addrlen = sizeof( struct sockaddr_s );
	int ret;

	if ( addr ) {
		ret = recvfrom( socket, buf, len, 0, (struct sockaddr *)addr, &addrlen );
		if ( ret == -1 ) {
//			errno = WSAGetLastError();

			if ( errno == EAGAIN || errno == ENOTCONN ) {
				return 0;
			}
		} //end if
	} //end if
	else
	{
		ret = recv( socket, buf, len, 0 );
		// if there's no data on the socket ret == -1 and errno == EAGAIN
		// MSDN states that if ret == 0 the socket has been closed
		// man recv doesn't say anything
		if ( ret == 0 ) {
			return -1;
		}
		if ( ret == SOCKET_ERROR ) {
//			errno = WSAGetLastError();

			if ( errno == EAGAIN || errno == ENOTCONN ) {
				return 0;
			}
		} //end if
	} //end else
	if ( ret == SOCKET_ERROR ) {
		WinPrint( "WINS_Read: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
	} //end if
	return ret;
} //end of the function WINS_Read