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

Socket.h

Ugrás a fájl dokumentációjához.
00001 /*
00002     mySocket
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 #ifndef SOCKET_H
00020 #define SOCKET_H
00021 
00022 #ifdef _WIN32
00023 #include <windows.h>
00024 #define EINPROGRESS WSAEINPROGRESS
00025 #define socket_t int
00026 #define socklen_t int
00027 #define sockaddr SOCKADDR
00028 #else
00029 #include <fcntl.h>
00030 #include <netdb.h>
00031 #include <sys/types.h>
00032 #include <sys/socket.h>
00033 #include <netinet/in.h>
00034 #include <errno.h>
00035 #include <arpa/inet.h>
00036 #define socket_t int
00037 #endif
00038 #ifdef HAVE_SSL
00039 #include <openssl/ssl.h>
00040 #endif
00041 #include <Locking.h>
00042 #include <Errors.h>
00043 #include <iostream>
00044 
00045 using namespace std;
00046 
00047 const int bufflen = 8192;
00048 
00049 enum SocketType{
00050 
00051   TYPE_TCP=1,
00052   TYPE_UDP,
00053   TYPE_ICMP
00054 
00055   };
00056 
00057 class Socket{ /* Finished, do not inherit this class! */
00058 
00059   friend class SocketHandler;
00060   friend class RealSocket; // needs to be removed
00061   friend class ListenServer; // Close();
00062 #ifdef HAVE_SSL
00063   friend class ConnectSocket; // SSL
00064 #endif
00065 
00066   public:
00067 
00068     Socket();
00069     Socket(socket_t _sock);
00070     virtual ~Socket();
00071 
00072     void MakeSock();
00073     socket_t GetSock();
00074 
00075     int GetPort();
00076     string GetAddr();
00077     string GetLastError();
00078 #ifdef HAVE_SSL
00079     void SetSSL(bool _is_SSL);
00080 #endif
00081 
00082   private:
00083 
00084     void OnReadError();
00085     void OnConnected();
00086     void OnConnClosed();
00087 
00088     virtual void __OnConnClosed() {}
00089     virtual void __OnConnected() {}
00090     virtual void __OnRead() = 0;
00091     virtual void __OnClose() = 0;
00092     virtual void __OnReadError() = 0;
00093     
00094     socket_t sock;
00095 #ifdef HAVE_SSL
00096     SSL_CTX* ctx;
00097     SSL* ssl;
00098 #endif
00099 
00100     bool is_SSL;
00101     CriticalSection cs;
00102 
00103   protected:
00104 
00105     void Close();
00106     SocketType type;
00107     bool connected; // should not be protected
00108     int port;
00109     string addr;
00110 
00111     struct sockaddr_in saddr;
00112 
00113   };
00114 
00115 string Resolv(string _addr);
00116 
00117 #endif

SourceForge.netLogo