socket.cpp

Go to the documentation of this file.
00001 
00002 //            Copyright (C) 2004-2007 by The Allacrost Project
00003 //                         All Rights Reserved
00004 //
00005 // This code is licensed under the GNU GPL version 2. It is free software 
00006 // and you may modify it and/or redistribute it under the terms of this license.
00007 // See http://www.gnu.org/copyleft/gpl.html for details.
00009 
00016 #include "socket.h"
00017 
00018 static uint32 num_sockets = 0;
00019 
00020 namespace hoa_socket {
00021 
00022 Socket::Socket ()
00023 {
00024   if (num_sockets == 0)
00025     SDLNet_Init();
00026   num_sockets++;
00027   sock = NULL;
00028   set = NULL;
00029   connected = false;
00030 }
00031 
00032 Socket::~Socket ()
00033 {
00034   if (IsConnected())
00035     Disconnect();
00036   num_sockets--;
00037   if (num_sockets == 0)
00038     SDLNet_Quit();
00039 }
00040 
00041 bool Socket::IsConnected ()
00042 {
00043   return connected;
00044 }
00045 
00046 void Socket::Connect ( const std::string& host, uint16 port )
00047 {
00048   IPaddress ipa;
00049   SDLNet_ResolveHost(&ipa, (char*)host.c_str(), port);
00050   sock = SDLNet_TCP_Open ( &ipa );
00051   if (sock != NULL)
00052   {
00053     set = SDLNet_AllocSocketSet(1);
00054     if (!set)
00055       SDLNet_TCP_Close(sock);
00056     else
00057     {
00058       SDLNet_TCP_AddSocket(set, sock);
00059       connected = true;
00060     }
00061   }
00062 }
00063 
00064 void Socket::Disconnect ()
00065 {
00066   if (!IsConnected())
00067     return;
00068   SDLNet_TCP_Close(sock);
00069   SDLNet_FreeSocketSet(set);
00070   connected = false;
00071 }
00072 
00073 bool Socket::IsQueued(uint32 wait_time) {
00074   if (!IsConnected())
00075     return false;
00076   return SDLNet_CheckSockets ( set, wait_time );
00077 }
00078 
00079 uint32 Socket::SendBinary(void* data, uint32 len) {
00080   if (!IsConnected())
00081     return 0;
00082   return SDLNet_TCP_Send(sock, data, len);
00083 }
00084 
00085 uint32 Socket::RecvBinary(void* location, uint32 len) {
00086   if (!IsConnected())
00087     return 0;
00088   return SDLNet_TCP_Recv ( sock, location, len );
00089 }
00090 
00091 void Socket::Write (const char* fmt, ... ) {
00092   /*va_list va;
00093   va_start ( va, fmt );
00094   
00095   char* buffer;
00096   vasprintf ( &buffer, fmt, va );
00097   
00098   SendBinary ( buffer, strlen(buffer) );
00099   
00100   free ( (void*)buffer );
00101   
00102   va_end ( va );*/
00103 }
00104 
00105 std::string Socket::ReadLine() {
00106   std::string ret;
00107   char* res = (char*)malloc(1);
00108 
00109   while (RecvBinary((void*)res, 1)) {
00110     if (*res == '\n')
00111       break;
00112     ret.append ( 1, *res );
00113   }
00114 
00115   std::string::size_type pos;
00116   if ((pos = ret.find_last_of('\r')) != std::string::npos) {
00117     ret.erase(pos, 1);
00118   }
00119   free((void*)res);
00120   return ret;
00121 }
00122 
00123 void Socket::ScanLine ( const char* format, ... ) {
00124   va_list va;
00125   va_start(va, format);
00126   
00127   std::string line = ReadLine ();
00128   
00129   //vsscanf ( line.c_str(), format, va );
00130   
00131   va_end(va);
00132 }
00133 
00134 } // namespace hoa_socket

Generated on Fri Jul 6 23:11:23 2007 for Hero of Allacrost by  doxygen 1.5.1