/* Copyright (C) 2007 Bradley Arsenault This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "NetConnection.h" #include #include #include "StreamBackend.h" #include "BinaryStream.h" using namespace GAGCore; NetConnection::NetConnection(const std::string& address, Uint16 port) { connected = false; set=SDLNet_AllocSocketSet(1); openConnection(address, port); } NetConnection::NetConnection() { set=SDLNet_AllocSocketSet(1); connected = false; } NetConnection::~NetConnection() { closeConnection(); SDLNet_FreeSocketSet(set); } void NetConnection::openConnection(const std::string& connectaddress, Uint16 port) { if(!connected) { //Resolve the address if(SDLNet_ResolveHost(&address, connectaddress.c_str(), port) == -1) { std::cout<<"NetConnection::openConnection: "< NetConnection::getMessage() { //Poll the SDL_net socket for more messages while (true) { int numReady = SDLNet_CheckSockets(set, 0); //This checks if there are any active sockets. //SDLNet_CheckSockets is used because it is non-blocking if(numReady==-1) { std::cout<<"NetConnection::getMessage: " << SDLNet_GetError() << std::endl; //most of the time this is a system error, so use perror perror("SDLNet_CheckSockets"); } else if(numReady) { //Read and interpret the length of the message Uint8* lengthData = new Uint8[2]; int amount = SDLNet_TCP_Recv(socket, lengthData, 2); if(amount <= 0) { std::cout<<"NetConnection::getMessage: " << SDLNet_GetError() << std::endl; closeConnection(); } else { Uint16 length = SDLNet_Read16(lengthData); //Read in the data. Uint8* data = new Uint8[length]; for(int i=0; iseekFromStart(0); BinaryInputStream* bis = new BinaryInputStream(msb); //Now interpret the message from the data, and add it to the queue shared_ptr message = NetMessage::getNetMessage(bis); recieved.push(message); //std::cout<<"Recieved: "<format()< message = recieved.front(); recieved.pop(); return message; } else { return shared_ptr(); } } void NetConnection::sendMessage(shared_ptr message) { if(connected) { //std::cout<<"Sending: "<format()<writeUint8(message->getMessageType(), "messageType"); message->encodeData(bos); msb->seekFromEnd(0); Uint32 length = msb->getPosition(); msb->seekFromStart(0); Uint8* newData = new Uint8[length+2]; SDLNet_Write16(length, newData); msb->read(newData+2, length); Uint32 result=SDLNet_TCP_Send(socket, newData, length+2); if(result<(length+2)) { std::cout<<"NetConnection::sendMessage: "<