socket.cpp

Go to the documentation of this file.
00001 /*
00002 #########################################################################
00003 #
00004 #  This file is part of trustyRC.
00005 #
00006 #  trustyRC, fully modular IRC robot 
00007 #  Copyright (C) 2006-2008 Nicoleau Fabien 
00008 #
00009 #  trustyRC is free software: you can redistribute it and/or modify
00010 #  it under the terms of the GNU General Public License as published by
00011 #  the Free Software Foundation, either version 3 of the License, or
00012 #  (at your option) any later version.
00013 #
00014 #  trustyRC is distributed in the hope that it will be useful,
00015 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 #  GNU General Public License for more details.
00018 #
00019 #  You should have received a copy of the GNU General Public License
00020 #  along with trustyRC.  If not, see <http://www.gnu.org/licenses/>.
00021 #
00022 #########################################################################
00023 */
00024 
00029 #include "socket.h"
00030 
00034 Socket::Socket()
00035 {
00036    this->mySock = -1 ;
00037    this->state = false;
00038    this->queue = "";
00039 }
00040 
00044 Socket::~Socket()
00045 {
00046    this->closeSock();
00047 }
00048 
00057 bool Socket::connectSock(int port, string serverName, string dedicatedIP)
00058 {
00059    struct sockaddr_in sockname;
00060    struct hostent *hostname;
00061    hostname = gethostbyname (serverName.c_str());
00062    if ( hostname == NULL )
00063       return false;
00064    if (hostname->h_addrtype != AF_INET)
00065       return false;
00066         if ((this->mySock=socket(AF_INET, SOCK_STREAM, 0)) <0)
00067         return false;
00068    memset ((char *) &sockname, 0, sizeof (struct sockaddr_in));
00069    memcpy ((char *) &sockname.sin_addr, (char*)hostname->h_addr, hostname->h_length);
00070    sockname.sin_family = AF_INET;
00071    sockname.sin_port = htons ((u_short)port);
00072    if ( dedicatedIP != "" )
00073    {
00074       struct sockaddr_in msim;
00075       unsigned long opt = 1;
00076       msim.sin_addr.s_addr = inet_addr (dedicatedIP.c_str());
00077       msim.sin_family = AF_INET;
00078       msim.sin_port = htons (0);
00079       setsockopt (this->mySock, SOL_SOCKET, SO_REUSEADDR, (char *) &opt,sizeof (opt));
00080       if (bind (this->mySock, (struct sockaddr *) &msim, sizeof (msim)) < 0)
00081       {
00082          cout << "ERROR : unable to bind socket"<<endl;
00083          exit(-1);
00084       }
00085    }
00086    if ((connect (this->mySock, (struct sockaddr *) &sockname, sizeof (struct sockaddr))) < 0)
00087       return false;
00088    this->state = true;
00089    return true;
00090 }
00091 
00096 bool Socket::closeSock()
00097 {
00098    if ( close(this->mySock) < 0 ) {
00099       return false ;
00100    }
00101    else {
00102       this->state = false;
00103       return true ;   
00104    }
00105 }
00106 
00111 bool Socket::getState()
00112 {
00113    return this->state;
00114 }
00115 
00123 string Socket::getOldestMessage()
00124 {
00125    string buff;
00126    string::size_type pos ;
00127    if((pos=this->queue.find("\n")) != string::npos) {
00128       buff = this->queue.substr(0,pos+1);
00129       this->queue.erase(0,buff.length()); 
00130       return buff;
00131    }
00132    this->queue += this->receive();
00133    if((pos=this->queue.find("\n")) != string::npos) {
00134       buff = this->queue.substr(0,pos+1);
00135       this->queue.erase(0,buff.length()); 
00136       return buff;
00137    }
00138    return "";
00139 }
00140 
00145 string Socket::receive()
00146 {
00147   const unsigned int MAXDATAS = 1024;
00148   char buffer[MAXDATAS]; 
00149   int numbytes ;
00150   struct timeval tv;
00151   fd_set readfds;
00152   tv.tv_sec = 0;
00153   tv.tv_usec = 050000; // 0.05 seconds
00154   FD_ZERO(&readfds);
00155   FD_SET(this->mySock, &readfds);
00156   if (select(this->mySock+1, &readfds, NULL, NULL, &tv) >=0 ) {
00157     if (FD_ISSET(this->mySock, &readfds)) {
00158       if((numbytes=recv(this->mySock,buffer,MAXDATAS, 0))<=0) {
00159          this->closeSock();
00160          return "";
00161       }
00162       else {
00163          return ((string)buffer).substr(0,numbytes);
00164       }
00165     }
00166     else {
00167       return "";
00168     }
00169   }
00170   this->state = false;
00171   return "";
00172 }
00173 
00179 bool Socket::sendStr(string strData)
00180 {
00181    if( send(this->mySock, strData.c_str(), strData.length(),MSG_NOSIGNAL) < 0 ) {
00182       return false ;
00183    }
00184    else {
00185       return true ;
00186    }
00187 }

Generated on Sun Apr 19 02:47:18 2009 for trustyRC by  doxygen 1.5.7.1