All pastes #624360 Raw Edit

Unnamed

public cpp v1 · immutable
·published 2007-07-18 06:22 UTC
rendered paste body
//Copyright (C) 2007 Ryan Houdek////This library is free software; you can redistribute it and/or//modify it under the terms of the GNU Lesser General Public//License as published by the Free Software Foundation; either//version 2.1 of the License, or (at your option) any later version.////This library is distributed in the hope that it will be useful,//but WITHOUT ANY WARRANTY; without even the implied warranty of//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU//Lesser General Public License for more details.////You should have received a copy of the GNU Lesser General Public//License along with this library; if not, write to the Free Software//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA#ifndef tcputil_h#define tcputil_h 1// receive a buffer from a TCP socket with error checking// this function handles the memory, so it can't use any [] arrays// returns 0 on any errors, or a valid char* on successchar *getMsg(TCPsocket sock, char **buf){	Uint32 len,result;	static char *_buf;	// allow for a NULL buf, use a static internal one...	if(!buf)		buf=&_buf;		// free the old buffer	if(*buf)		free(*buf);	*buf=NULL;	// receive the length of the string message	result=SDLNet_TCP_Recv(sock,&len,sizeof(len));	if(result<sizeof(len))	{		if(SDLNet_GetError() && strlen(SDLNet_GetError())) // sometimes blank!			printf("SDLNet_TCP_Recv: %s\n", SDLNet_GetError());		return(NULL);	}		// swap byte order to our local order	len=SDL_SwapBE32(len);		// check if anything is strange, like a zero length buffer	if(!len)		return(NULL);	*buf=(char*)malloc(1024);	if(!(*buf))		return(NULL);	// allocate the buffer memory	// get the string buffer over the socket	result=SDLNet_TCP_Recv(sock,*buf,len);	if(result<len)	{		if(SDLNet_GetError() && strlen(SDLNet_GetError())) // sometimes blank!			printf("SDLNet_TCP_Recv: %s\n", SDLNet_GetError());		free(*buf);		buf=NULL;	}	// return the new buffer	return(*buf);}// send a string buffer over a TCP socket with error checking// returns 0 on any errors, length sent on successint putMsg(TCPsocket sock, char *buf){	Uint32 len,result;	if(!buf || !strlen(buf) || !sock)		return(1);	// determine the length of the string	len=strlen(buf)+1; // add one for the terminating NULL	// change endianness to network order	len=SDL_SwapBE32(len);	// send the length of the string	result=SDLNet_TCP_Send(sock,&len,sizeof(len));	if(result<sizeof(len)) {		if(SDLNet_GetError() && strlen(SDLNet_GetError())) // sometimes blank!			printf("SDLNet_TCP_Send: %s\n", SDLNet_GetError());		return(0);	}		// revert to our local byte order	len=SDL_SwapBE32(len);	// send the buffer, with the NULL as well	result=SDLNet_TCP_Send(sock,buf,len);	if(result<len) {		if(SDLNet_GetError() && strlen(SDLNet_GetError())) // sometimes blank!			printf("SDLNet_TCP_Send: %s\n", SDLNet_GetError());		return(0);	}		// return the length sent	return(result);}#endif