libaeon.h

Go to the documentation of this file.
00001 /*********************************************************************
00002  * libaeon - A simple, lightweight, cross platform networking library
00003  * Copyright 2006-2007 (c) Elden Armbrust
00004  * This software is licensed under the BSD software license.
00005  *********************************************************************/
00006 #ifndef _LIBAEON_H
00007 #define _LIBAEON_H
00008 
00018 #ifdef _WIN32
00019     #define WIN32_LEAN_AND_MEAN
00020     #define _CRT_SECURE_NO_DEPRECATE 1
00021     #include <winsock2.h>
00022     typedef int socklen_t;
00023 #endif
00024 #ifdef __linux__
00025     #include <sys/socket.h>
00026     #include <netdb.h>
00027     #include <arpa/inet.h>
00028     #include <netinet/in.h>
00029     #include <sys/types.h>
00030     #include <fcntl.h>
00031 #endif
00032 
00033 //multi-platform includes
00034 #include <string>
00035 #include <vector>
00036 
00037 
00038 //generic error, state, and name defines.  *deprecating*
00039 //define states
00040 #define SOCK_RESOLVE 1
00041 #define SOCK_ACCEPT 3
00042 #define SOCK_CONNECT 4
00043 #define SOCK_CREATE 2
00044 //define codes
00045 #define ERR_NONE 0
00046 #define ERR_NOHOST 1
00047 #define ERR_NOSOCKET 2
00048 //various defines
00049 //#define BUF_SIZE 256
00050 #define AEON 1
00051 
00052 //#define DEFAULT_NET AF_INET
00053 
00065 namespace net
00066 {
00075     class CSocket
00076     {
00077     public:
00078         static const int MaxBufferSize = 256;
00079         static const int StreamSocketType = SOCK_STREAM;
00080         static const int DatagramSocketType = SOCK_STREAM;
00081         static const int DefaultSocketType = CSocket::StreamSocketType;
00082 
00083 
00084         static const int DefaultFamilyType = AF_INET;
00085         int SetBlocking(bool flag);
00086         static const int NULLFlag = 0;
00087         int Write(char* data, int size);
00088         int Write(char* data);
00089         int Write(std::string data);
00090         int Read();
00091         int Read(char* buffer, int size);
00092         int ReadLine(char* buffer, int size);
00093         int ReadUntil(char* buffer, int size);
00094         std::string Read(int size);
00095         CSocket();
00096         CSocket(int family_type);
00097         ~CSocket();
00098         bool Close();
00099         int operator<<(char* data);
00100         int operator<<(std::string data);
00101         std::string operator>>(std::string);
00102         int sockfd, n;
00103         struct sockaddr_in remote_addr;
00104         bool connected;
00105     protected:
00106         int flags;
00107         int net_family;
00108         int token_size;
00109         bool blocking;
00110         char inbuffer[CSocket::MaxBufferSize];
00111         char outbuffer[CSocket::MaxBufferSize];
00112         std::string remote_host;
00113         std::string remote_ip;
00114         int connect_code;
00115         int error_code;
00116         int error_state;
00117         int GetState();
00118         int GetError();
00119         void SetError(int error);
00120 
00121         int port;
00122 
00123         void ClearBuffers();
00124         void ClearBuffer(char* buffer, int size);
00125 #ifdef WIN32
00126         int wsaret;
00127         WSADATA wsadata;
00128 #endif
00129     };
00130 
00131 
00140     class CClientSocket : public CSocket
00141     {
00142     public:
00143         bool Connect();
00144         bool Connect(const char* remote, int port);
00145         CClientSocket();
00146         CClientSocket(std::string *remote, int port);
00147         CClientSocket(const char* remote, int port);
00148         ~CClientSocket();
00149     protected:
00150         struct sockaddr_in serv_addr;
00151         struct hostent *server;
00152     };
00153 
00161     class CServerSocket : public CSocket
00162     {
00163     public:
00164         CServerSocket();
00165         ~CServerSocket();
00166         bool Listen();
00167         bool Listen(int port);
00168         CSocket* Accept();
00169     protected:
00170         struct sockaddr_in serv_addr;
00171         struct hostent *server;
00172         int server_socket;
00173     };
00174 
00175 
00176 
00189     class CEventSocket: public CSocket
00190     {
00191     public:
00192         bool OnRead(const char* buffer, int size);
00193         void OnWrite(const char* buffer, int size, int sentsize);
00194         int Write(char* data);
00195         int Write(std::string data);
00196         bool Poll();
00197 
00198     };
00199 
00213     class CEventClientSocket: public CClientSocket
00214         {};
00215 
00216 
00230     class CEventServerSocket: public CServerSocket, public CEventSocket
00231         {};
00232 
00233 
00241     class CSocketSet
00242     {
00243     public:
00244         std::vector<CSocket*> Sockets;
00245         bool Add(CSocket* socket_ref);
00246         bool Add();
00247         bool Remove(int index);
00248         bool Remove(int index, int count);
00249         int Size();
00250     };
00251 
00259     class CEventSocketSet
00260     {
00261     public:
00262         std::vector<CEventSocket*> Sockets;
00263         bool Add(CEventSocket* socket_ref);
00264         bool Add();
00265         bool Remove(int index);
00266         bool Remove(int index, int count);
00267         int Size();
00268         void Poll();
00269         void Cleanup();
00270 
00271     };
00272 
00273     class CSocketUDP : public CSocket
00274     {
00275     public:
00276     CSocketUDP();
00277         int Write(char* data, int size);
00278         int Write(char* data);
00279         int Write(std::string data);
00280         int Read();
00281         int Read(char* buffer, int size);
00282         int ReadUntil(char* buffer, int size);
00283         std::string Read(int size);
00284         static const int DefaultSocketType = SOCK_DGRAM;
00285     };
00286 
00287 
00288 }
00289 #endif

Generated on Mon Jul 2 10:27:20 2007 for libAeon by  doxygen 1.5.1