本文整理汇总了C++中SDLNet_TCP_Recv函数的典型用法代码示例。如果您正苦于以下问题:C++ SDLNet_TCP_Recv函数的具体用法?C++ SDLNet_TCP_Recv怎么用?C++ SDLNet_TCP_Recv使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了SDLNet_TCP_Recv函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: ngl_net_read_client
unsigned char
ngl_net_read_client(char num)
{
if (num==0) {
if (SDLNet_TCP_Recv(csd0, buffer0, 512) > 0) {
return 1;
}
return 0;
}
if (num==1) {
if (SDLNet_TCP_Recv(csd1, buffer1, 512) > 0) {
return 1;
}
return 0;
}
if (num==2) {
if (SDLNet_TCP_Recv(csd2, buffer2, 512) > 0) {
return 1;
}
return 0;
}
return 0;
}
示例2: while
PMRECV PacketManager::Recv(TCPsocket socket, Uint16 *packetID, Uint16 *packetLength){
if(bufferSize<4)if(!Allocate(4))return PMRECV_ALLOCFAIL;
Uint8 *buff = buffer; Uint16 len = 4; int ret = 0;
while(len > 0){
ret = SDLNet_TCP_Recv(socket, buff, len);
if((ret<=0)||(ret>len))return ((ret == 0)?PMRECV_DISCONNECT:PMRECV_ERROR);
len -= ret;
buff += ret;
}
lastPackID = SDLNet_Read16(buffer);
lastPackLen = SDLNet_Read16(buffer + sizeof(Uint16));
Uint32 size = (Uint32)lastPackLen + 4;
if((maxAllocSize != 0)&&(size > maxAllocSize))return PMRECV_OVERFLOW;
if(size > bufferSize)if(!Allocate(size))return PMRECV_ALLOCFAIL;
buff = buffer + 4; len = lastPackLen; ret = 0;
while(len > 0){
ret = SDLNet_TCP_Recv(socket,buff,len);
if((ret<=0)||(ret>len))return ((ret == 0)?PMRECV_DISCONNECT:PMRECV_ERROR);
len -= ret;
buff += ret;
}
dataLength = size;
dataPos = 4;
return PMRECV_SUCCESS;
}
示例3: Treception
static int Treception(void *ptr)
{
Data data; /* buffer de réception */
DataList dataList; /* buffer de réception pour les listes de salles*/
DataGame dataGame; /* buffer de réception pour les données de jeu*/
int cnt;
bool stopNetwork=false;
while(!stopNetwork)
{
cnt = SDLNet_TCP_Recv(sd, &data, sizeof(Data));
if(cnt <= 0)
return cnt;
(*mCData)(&data);
if(data.dataType == CONN)
{
if(data.car == SWITCH) // on nous prévient que une dataList va être transmise
{
do
{
cnt = SDLNet_TCP_Recv(sd, &dataList, sizeof(DataList));
(*mCList)(&dataList);
}
while(cnt>0 && !dataList.end);
}
else if(data.car == CONN_QUIT)
stopNetwork = true;
else if(data.car == CONN_ERROR)
stopNetwork=true;
else if(data.car == CONN_STOP)
stopNetwork=true;
}
else if(data.dataType == GAME)
{
if(data.car == SWITCH) // on nous prévient que une dataGame va être transmise
{
if(waiting)
{
printf("reception thread is waiting main thread to switch to game\n");
while(waiting){}
}
cnt = SDLNet_TCP_Recv(sd, &dataGame, sizeof(DataGame));
(*mCGame)(&dataGame);
}
else if(data.car == GAME_ERROR)
{
stopNetwork=true;
}
}
if(cnt <= 0)
stopNetwork=true;
}
printf("Treception terminé\n");
return cnt;
}
示例4: memset
std::string TCPConnectionServer::ReadMessages( int connectionNr )
{
if ( !CheckForActivity( connectionNr ) )
return "";
char buffer[1024];
memset( buffer, 0, bufferSize );
int byteCount = 0;
std::string received("");
if ( isSocketConnected.size() < ( connectionNr + 1 ) )
return received;
if ( isServer )
byteCount = SDLNet_TCP_Recv( serverSocket[connectionNr], buffer, bufferSize );
else
byteCount = SDLNet_TCP_Recv( tcpSocket, buffer, bufferSize );
if ( byteCount > 0 )
{
//memset( &buffer[byteCount], 0, static_cast< size_t > (512 - byteCount ) );
buffer[byteCount] = '\0';
received = buffer;
if ( byteCount >= bufferSize )
{
std::cout << "[email protected]" << __LINE__
<< " Too much data received : " << byteCount
<< " / " << bufferSize
<< std::endl;
}
}
// A bytecount of 0 means the connection has been terminated
else if ( byteCount == 0 )
{
std::cout << "[email protected]" << __LINE__ << " Connection terminated" << std::endl;
isSocketConnected[ connectionNr ] = false;
// A bytecount of < 0 means an error occured
} else if ( byteCount < 0 )
{
std::cout << "[email protected]" << __LINE__ << " Read failed!" <<
"\nSocket : " << ( isServer ? serverSocket[connectionNr] : tcpSocket ) <<
"\nByte count : " << byteCount <<
"\nError : " << SDLNet_GetError() <<
std::endl;
}
return received;
}
示例5: SDLNet_TCP_Recv
char *network_read_string_1(TCPsocket sock) {
uint8_t len;
SDLNet_TCP_Recv(sock,&len,1);
int read = 0;
char *text = malloc(len+1);
while (read != len) {
int more = SDLNet_TCP_Recv(sock,text,len-read);
if (more < 1)
return 0;
read += more;
}
text[len] = 0;
return text;
}
示例6: messages
void NetworkManager::NetworkCommunicator() //make it receive paddlemanagers and ballmanagers to change positions into messages (break up server messages since it'll send ball and paddle to network... easy to write, not to read)
{ //call this from game loop
int len; // commented out code should be correct (I can't check it yet though) once networking works right
char buffer[512];
//
char str[512];
if(!server)
{
//strcpy(buffer,NetworkManager::Vector3ToString(clientPaddle->getPosition()));
len = strlen(buffer) + 1;
if (SDLNet_TCP_Send(targetSocket, (void *)buffer, len) < len)
{
fprintf(stderr, "SDLNet_TCP_Send: %s\n", SDLNet_GetError());
exit(EXIT_FAILURE);
}
if (SDLNet_TCP_Recv(targetSocket, buffer, 512) > 0)
{
std::string decode = std::string(buffer);
unsigned found = decode.find_first_of("&");
Ogre::Vector3 v = NetworkManager::StringToVector3(buffer,0);
//hostPaddle->setPosition(v);
v = NetworkManager::StringToVector3(buffer,found+1);
//ball->setPosition(v);
printf("Host say: %s\n", buffer);
}
}
else
{
if (SDLNet_TCP_Recv(targetSocket, buffer, 512) > 0)
{
Ogre::Vector3 v = NetworkManager::StringToVector3(buffer,0);
//clientPaddle->setPosition(v);
printf("Client say: %s\n", buffer);
}
//strcpy(str, NetworkManager::Vector3ToString(hostPaddle->getPosition()));
strcat(str, " ");
//strcat(str, NetworkManager::Vector3ToString(ball->getPosition()));
strcpy(buffer,str);
//strcpy(buffer,"test reply");
int len = strlen(buffer) + 1;
if (SDLNet_TCP_Send(targetSocket, (void *)buffer, len) < len)
{
fprintf(stderr, "SDLNet_TCP_Send: %s\n", SDLNet_GetError());
exit(EXIT_FAILURE);
}
}
}
示例7: free
// 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 success
char *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);
// allocate the buffer memory
*buf=(char*)malloc(len);
if(!(*buf)){
return(NULL);
}
// 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);
}
示例8: listenToCode
int listenToCode(TCPsocket tcpclient, char *answer)
{
int brakets = 0;
int i;
char *data = MALLOCN(char, 4096);
int size = 0;
for (;;) {
answer += size;
size = SDLNet_TCP_Recv(tcpclient, data, 4095);
data[size] = '\0';
memcpy(answer, data, size);
for (i = 0; i < size; i++) {
switch (answer[i]) {
case '(':
brakets++;
break;
case ')':
brakets--;
if (brakets == 0) {
answer[i+1] = '\0';
return 0;
}
if (brakets < 0)
return -1;
break;
}
}
}
}
示例9: SDLNet_TCP_Recv
void NetClient::handleserver()
{
Uint8 data[512];
int pos, len;
// int used;
/* Has the connection been lost with the server? */
len = SDLNet_TCP_Recv(tcpsock, (char *)data, 512);
if ( len <= 0 ) {
SDLNet_TCP_DelSocket(socketset, tcpsock);
SDLNet_TCP_Close(tcpsock);
tcpsock = NULL;
} else {
pos = 0;
while ( len > 0 ) {
int used = handleserverdata(&data[pos]);
pos += used;
len -= used;
if ( used == 0 ) {
/* We might lose data here.. oh well,
we got a corrupt packet from server
*/
len = 0;
}
}
}
}
示例10: SDLNet_TCP_RecvLine
int SDLNet_TCP_RecvLine(TCPsocket sock, char* data, int maxlen) {
char c;
int status = 0;
int i = 0;
while(1) {
status = SDLNet_TCP_Recv(sock, &c, 1);
if (status == -1) return -1;
if (i == maxlen-1) return -1;
if (status == 0) break;
data[i] = c;
i++;
if (c == '\n') {
data[i] = '\0';
return i;
}
}
if(i > 0) {
data[i] = '\0';
return i;
} else {
return 0;
}
}
示例11: setup_client
/* Setup TCP Client, and attempt to connect
* to the server */
int setup_client(unsigned port) {
#if !defined(PSP) && !defined(EMSCRIPTEN) && !defined(DREAMCAST) && !defined(THREE_DS)
is_client = 1;
log_message(LOG_INFO, "Attempting to connect to server on port %u\n",port);
//SDL_INIT(SDL_INIT_EVERYTHING);
SDLNet_Init();
IPaddress ip;
//TODO, for now always connect to localhost, fix for any specified ip in
//the future
SDLNet_ResolveHost(&ip, "localhost", port);
client = SDLNet_TCP_Open(&ip);
socketset = SDLNet_AllocSocketSet(1);
char buf[100];
int i = SDLNet_TCP_Recv(client, buf, 100);
for (int j = 0; j < i; j++) {
printf("%c",buf[j]);
}
printf("\n");
SDLNet_TCP_AddSocket(socketset, client);
connection_up = 1;
return 1;
#endif
return 0;
}
示例12: SDLNet_TCP_Recv
void mythVirtualServer::HandleClient(int which)
{
int closesocket;
char data[4096] = {0};
//char tmpdata[512] = {0};
int datalength;
int length;
datalength = 0;
for(;;){
length = SDLNet_TCP_Recv(people[which]->sock, data + datalength, 512);
datalength += length;
if(length < 512)
break;
}
/* Has the connection been closed? */
if ( datalength <= 0 ) {
#ifdef DEBUG
fprintf(stderr, "Closing socket %d (was%s active)\n",
which, people[which].active ? "" : " not");
#endif
/* Notify all active clients */
closePeople(people[which]);
} else {
this->ServerDecodeCallBack(people[which],data,datalength);
}
}
示例13: grabPacket
/** read a packet from the given TCPsocket
Returns -1 if some error occured, 0 if there was no data available and 1 if a
packet was successfully read.
Note: the socket has to be in the supplied socketset.
TODO: this function will bomb if a packet arrives in pieces, there is no
inherent guarantee that the next call will be made on the same socket. */
int
grabPacket(TCPsocket s, SDLNet_SocketSet ss, struct NetPacket* pkt)
{
static char buf[NETPKTBUFSIZE];
static int buf_count = 0;
int rc;
if (SDLNet_CheckSockets(ss, 0) <= 0)
return 0;
if(!SDLNet_SocketReady(s))
return 0;
rc = SDLNet_TCP_Recv(s, &buf[buf_count], NETPKTBUFSIZE - buf_count);
if (rc <= 0) {
/* closed connection? */
return -1;
} else if (rc != NETPKTBUFSIZE) {
/* we got a partial packet. Store what we got in the static buffer and
return so that the next call can read the rest. Hopefully. */
buf_count = rc;
return 0;
} else {
buf_count = 0;
bufToPacket(buf, pkt);
return 1;
}
}
示例14: throw
int Server::script_client(void* data) throw(const char*){
Data_scriptClient* _data_cast=static_cast<Data_scriptClient*>(data);
if(_data_cast==NULL){
throw "Impossibile eseguire il casting dei dati nello script client!";
}
int bytes_rcv;
char buffer[MAX_BYTES_BUFFER];
std::string data_store_rcv;
std::string data_store_snd;
int result;
AppProtServer client_prot;
while(_data_cast->exit==false){
bytes_rcv=SDLNet_TCP_Recv(_data_cast->connessione, buffer, MAX_BYTES_BUFFER);
if(bytes_rcv<=0){
_data_cast->exit=true;
}else{
data_store_rcv.append(buffer,bytes_rcv);
result=client_prot.ElaboraRichiesta(data_store_rcv,data_store_snd);
if(result){
int bytes_snd=SDLNet_TCP_Send(_data_cast->connessione, data_store_snd.data(), data_store_snd.size());
if(bytes_snd!=data_store_snd.size()){
_data_cast->exit=true;
}
}
if(result==-1){
_data_cast->exit=true;
}
}
}
return 0;
}
示例15: network_tcp_recv
int network_tcp_recv(TCPsocket tcp, void* data, int maxlen) {
int r = SDLNet_TCP_Recv(tcp, data, maxlen);
if (r <= 0) {
std::cerr << "Failed to recieve data over TCP: " << SDLNet_GetError() << "\n";
}
return r;
}