Főoldal | Névtérlista | Osztályhierarchia | Betűrendes lista | Adatszerkezetek | Könyvtárak | Fájllista | Névtértagok | Adatmezők | Globális elemek

ConnectSocket.cpp

Ugrás a fájl dokumentációjához.
00001 /*
00002     mySocket, ConnectSocket
00003     Copyright (C) 2006  Kornel Csernai <csko@csko.hu>
00004 
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 */
00019 #include <ConnectSocket.h>
00020 #include <SocketHandler.h>
00021 #include <StringLib.h>
00022 
00023 using namespace StringLib;
00024 
00025 // Constructor
00026 ConnectSocket::ConnectSocket() : RealSocket(-1){
00027 
00028   addr = "";
00029   port = -1;
00030   connected = false;
00031   memset(&saddr, 0, sizeof(saddr));
00032 
00033   }
00034 
00035 // Destructor
00036 ConnectSocket::~ConnectSocket(){
00037 
00038 //  OnClose();
00039 
00040   }
00041 
00042 
00043 // Connect with sockaddr_in
00044 void ConnectSocket::Connect(sockaddr_in _saddr, SocketType _type, bool _blocking){
00045 
00046   Connect( (string) inet_ntoa(_saddr.sin_addr), ntohs(_saddr.sin_port), _type, _blocking);
00047 
00048   }
00049 
00050 
00051 // Connect to host, port 
00052 void ConnectSocket::Connect(string _addr, int _port, SocketType _type, bool _blocking){
00053 
00054   if(IsConnected() or _addr == "" or _port <= 0) return; // throw needed
00055 
00056   type = _type;
00057   blocking = _blocking;
00058 
00059   if(GetSock() < 0) MakeSock();
00060   
00061   debug("Socket created.");
00062   
00063   string resolved = Resolv(_addr);
00064 
00065   if(resolved != ""){
00066 
00067     debug(_addr + " resolved to " + resolved);
00068 
00069     }else{
00070 
00071     debug("Could not resolve " + _addr);
00072     throw SocketException(RESOLV_FAILED);
00073 
00074     }
00075 
00076   saddr.sin_family = AF_INET;
00077   saddr.sin_addr.s_addr = inet_addr(resolved.c_str());
00078   saddr.sin_port = htons(_port);
00079 
00080   if(!blocking){
00081 #ifdef _WIN32
00082     u_long b = 1;
00083     ioctlsocket(GetSock(), FIONBIO, &b);
00084 #else
00085     int flags = fcntl(GetSock(), F_GETFL, 0);
00086     if (fcntl(GetSock(), F_SETFL, flags | O_NONBLOCK ) == -1 ) throw SocketException(NONBLOCK_FAILED);
00087 #endif
00088     }
00089 
00090   debug("Connecting to " + _addr + ":" + itos(_port) + " ...");
00091 
00092   int ret;
00093 
00094 
00095 #ifdef _WIN32
00096   while((ret = connect(GetSock(), (SOCKADDR*) &saddr, sizeof(saddr))) == -1 and (errno == 115 or errno == 114)){
00097     Sleep(15000);
00098 #else
00099   while((ret = connect(GetSock(), (const struct sockaddr*) &saddr, sizeof(saddr))) == -1 and (errno == 115 or errno == 114)){
00100     usleep(15000);
00101 #endif
00102     }
00103 
00104   if(ret == -1){
00105 
00106     debug("Failed to connect: " + GetLastError());
00107     throw SocketException(CONNECT_FAILED);
00108 
00109     }
00110 
00111   SocketHandler* handler = SocketHandler::getInstance();
00112   if(!handler) throw SocketException(SOCKETHANDLING_FAILED);
00113 
00114   debug("Connection successful.");
00115   __OnConnected();
00116   handler -> Add(this);
00117 
00118 
00119 #ifdef HAVE_SSL
00120 
00121   if(is_SSL){
00122 
00123     if ((ret = SSL_connect(ssl)) != 1) {
00124       char err[300];
00125       snprintf(err, 300, "SSL_connect failed: %s", strerror(errno));
00126       error(err);
00127       throw SocketException(SSL_CONNECT_FAILED);
00128 
00129       }
00130           
00131     }
00132 
00133 #endif
00134 
00135   addr = _addr;
00136   port = _port;
00137   connected = true;
00138 
00139   }
00140 
00141 // Disconnect
00142 void ConnectSocket::Disconnect(){
00143 
00144   if(!IsConnected()) return;
00145 
00146   Close();
00147   addr = "";
00148   port = -1;
00149   memset(&saddr, 0, sizeof(saddr));  
00150   connected = false;
00151 
00152   }

SourceForge.netLogo