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

RealSocket.cpp

Ugrás a fájl dokumentációjához.
00001 /*
00002     mySocket, RealSocket
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 <RealSocket.h>
00020 #include <StringLib.h>
00021 #include <iostream>
00022 
00023 using namespace StringLib;
00024 using namespace std;
00025 
00026 RealSocket::RealSocket(socket_t _sock) : Socket(_sock) {
00027 
00028   ReadBuf = "";
00029   ReadLen = 0;
00030   WriteBuf = "";
00031   WriteLen = 0;
00032   if(_sock == -1) connected = false;
00033     else connected = true;
00034 
00035   }
00036 
00037 
00038 // Retrun current read buffer
00039 string RealSocket::GetReadBuf(){
00040 
00041   return ReadBuf;
00042 
00043   }
00044 
00045 
00046 // Return read buffer length
00047 int RealSocket::GetReadLen(){
00048 
00049   return ReadBuf.size();
00050 
00051   }
00052 
00053 // Retrun current write buffer
00054 string RealSocket::GetWriteBuf(){
00055 
00056   return WriteBuf;
00057 
00058   }
00059 
00060 // Return write buffer length
00061 int RealSocket::GetWriteLen(){
00062 
00063   return WriteBuf.size();
00064 
00065   }
00066 
00067 
00068 void RealSocket::Write(string str){
00069 
00070   WriteBuf += str;
00071   WriteLen = WriteBuf.size();
00072 
00073   }
00074 
00075 
00076 // Physical write to a socket
00077 int RealSocket::WriteBuffer(string writeval){
00078 
00079   return WriteBuffer((void*)writeval.c_str(), writeval.size());
00080 
00081   }
00082 
00083 
00084 // Physical write to a socket
00085 int RealSocket::WriteBuffer(void* writeval, int writelen){
00086 
00087   if(writeval == NULL or writelen < 0){
00088 //    throw SocketException(SERR_INVALID_PARAMETERS);
00089     return -1;
00090     }
00091 
00092   if(!IsConnected()) throw SocketException(SOCKET_CLOSED);
00093 
00094   if(writelen == 0) return 0;
00095 
00096 
00097   int ret;
00098 
00099 
00100   if(is_SSL){
00101 #ifdef HAVE_SSL
00102 
00103     ret = SSL_write(ssl, writeval, writelen);
00104 
00105 #else
00106 
00107     throw SocketException(NO_SSL);
00108 
00109 #endif
00110 
00111     }else{
00112 
00113 #ifdef _WIN32
00114     ret = send(GetSock(), (char*)writeval, writelen, 0);
00115 #else
00116     ret = send(GetSock(), writeval, writelen, 0);
00117 #endif
00118 
00119     }
00120 /*
00121   if(ret < writelen){
00122     throw -1;
00123     }
00124 */
00125   if(ret == -1) throw SocketException(SOCKET_DISCONNECTED);
00126 
00127   return ret;
00128 
00129   }
00130 
00131 // Physical write to a socket
00132 int RealSocket::WriteBuffer(char* writeval){ // deprecated function
00133 
00134 // THAT'S WHY I DONT REALLY LIKE CHAR*'S
00135 
00136   int i = strlen(writeval);
00137   char* tmp = (char*) malloc(i+2);
00138   memcpy(tmp, writeval, i);
00139   tmp[i] = '\r';
00140   tmp[i+1] = '\n';
00141   int ret = WriteBuffer((void*)tmp, i+2);
00142   free(tmp);
00143   return ret;
00144 
00145   }
00146 
00147 
00148 
00149 // Read from a socket
00150 int RealSocket::Read(string& readval){
00151 
00152   int ret;
00153   char _readval[bufflen];
00154 
00155   ret = Read(_readval, bufflen);
00156   readval.append(_readval, ret);
00157 
00158   return ret; // Received ret bytes
00159   }
00160 
00161 // Read from a socket
00162 int RealSocket::Read(void* readval, int readlen){
00163 
00164   if(readval == NULL or readlen < 0){
00165 //    throw SocketException(INVALID_PARAMETERS);
00166     }
00167 
00168   if(!IsConnected()) throw SocketException(SOCKET_CLOSED);
00169 
00170   if(readlen == 0){
00171 
00172     return 0;
00173 
00174     }
00175 
00176   int ret;
00177 
00178   if(is_SSL){
00179 #ifdef HAVE_SSL
00180 
00181     ret = SSL_read(ssl, readval, readlen);
00182 //    if(ret<0) error(SSL_get_error(ssl, ret));
00183 
00184 #else
00185 
00186     throw SocketException(NO_SSL);
00187 
00188 #endif
00189 
00190     }else{
00191 
00192 #ifdef _WIN32
00193 
00194     ret = recv(GetSock(), (char*) readval, readlen, 0);
00195 
00196 #else
00197 
00198     ret = recv(GetSock(), readval, readlen, 0);
00199 
00200 #endif
00201 
00202     }
00203 
00204   if(ret <= 0){
00205 
00206     OnConnClosed();
00207 
00208     }
00209 
00210   return ret; // Received ret bytes
00211 
00212   }
00213 
00214 
00215 /* deprecated
00216 int RealSocket::readfrom( void* buf, int bufLen, struct sockaddr* from, socklen_t* fromLen ) {
00217   if ( ( buf == NULL ) || ( bufLen < 0 ) || ( from == NULL ) || ( fromLen == NULL ) ) {
00218 //    throw SocketException( SERR_INVALID_PARAMETERS );
00219   }
00220 
00221   if ( bufLen == 0 ) {
00222     return 0;
00223   }
00224 #ifdef _WIN32
00225   int ans = ::recvfrom( sock, (char*)buf, bufLen, 0, from, fromLen );
00226 #else
00227   int ans = ::recvfrom( sock, buf, bufLen, 0, from, fromLen );
00228 #endif
00229   if ( ans <= 0 ) {
00230 //    throw SocketException( SERR_STREAM_CLOSED );
00231   }
00232 
00233   return ans;
00234 }
00235 */
00236 
00237 // Called on recv()
00238 void RealSocket::OnRead() {
00239 
00240   string readstr;
00241   int ret = Read(readstr);
00242 
00243   if(ret == -1) throw SocketException(READ_FAILED);
00244 
00245   // store read results for user
00246   ReadBuf = readstr;
00247   ReadLen = ret;
00248 
00249   if(ret > 0)  __OnRead();
00250 
00251   }
00252 
00253 // Called when it's safe to write
00254 void RealSocket::OnWrite() {
00255 
00256 //  if(WriteBufLen == 0) throw SocketException(INVALID_PARAMETERS);
00257 
00258   int ret = WriteBuffer(GetWriteBuf());
00259 
00260   // setting buffer to the remaining part
00261   WriteBuf = WriteBuf.substr(ret);
00262   WriteLen = WriteBuf.size();
00263   }
00264 
00265 
00266 void RealSocket::OnConnectionError(){
00267 
00268   Close();
00269 
00270   }
00271 
00272 bool RealSocket::IsConnected(){
00273 
00274   return connected;
00275 
00276   }
00277 
00278 RealSocket* operator<<(RealSocket* _socket, string str){
00279 
00280   _socket -> Write(str);
00281 
00282   }
00283 
00284 RealSocket* operator+=(RealSocket* _socket, string str){
00285 
00286   _socket << str;
00287 
00288   }
00289 

SourceForge.netLogo