Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 #ifndef LOG_SOCK_H
00027 #define LOG_SOCK_H
00028 
00029 #include "libts.h"
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 
00038 class LogSock
00039 {
00040 public:
00041   enum Constant
00042   {
00043     LS_CONST_PACKETSIZE = 1024,
00044     LS_CONST_CLUSTER_MAX_MACHINES = 256
00045   };
00046 
00047   enum Err
00048   {
00049     LS_ERROR_UNKNOWN = -1,
00050     LS_ERROR_CONNECT_TABLE_FULL = -3,
00051     LS_ERROR_SOCKET = -4,
00052     LS_ERROR_BIND = -5,
00053     LS_ERROR_CONNECT = -6,
00054     LS_ERROR_ACCEPT = -7,
00055     LS_ERROR_NO_SUCH_HOST = -8,
00056     LS_ERROR_NO_CONNECTION = -9,
00057     LS_ERROR_STATE = -10,
00058     LS_ERROR_WRITE = -11,
00059     LS_ERROR_READ = -12
00060   };
00061 
00062   enum State
00063   {
00064     LS_STATE_UNUSED = 0,
00065     LS_STATE_INCOMING,
00066     LS_STATE_OUTGOING,
00067     LS_N_STATES
00068   };
00069 
00070 public:
00071   LogSock(int max_connects = 1);
00072   ~LogSock();
00073 
00074   bool pending_any(int *cid, int timeout_msec = 0);
00075   bool pending_message_any(int *cid, int timeout_msec = 0);
00076   bool pending_message_on(int cid, int timeout_msec = 0);
00077   bool pending_connect(int timeout_msec = 0);
00078 
00079   int listen(int accept_port, int family = AF_INET);
00080   int accept();
00081   int connect(sockaddr const* ip);
00082 
00083   void close(int cid);          
00084   void close();                 
00085 
00086   int write(int cid, void *buf, int bytes);
00087 
00088   int read(int cid, void *buf, unsigned maxsize);
00089   void *read_alloc(int cid, int *size);
00090 
00091   char *on_host()
00092   {
00093     return ct[0].host;
00094   }
00095 
00096   int on_port()
00097   {
00098     return ct[0].port;
00099   }
00100 
00101   bool is_connected(int cid, bool ping = false) const;
00102   void check_connections();
00103   bool authorized_client(int cid, char *key);
00104   char *connected_host(int cid);
00105   int connected_port(int cid);
00106 
00107 private:
00108   struct ConnectTable
00109   {
00110     char *host;                 
00111     int port;                   
00112     int sd;                     
00113     State state;                
00114   };
00115 
00116   struct MsgHeader
00117   {
00118     int msg_bytes;              
00119   };
00120 
00121 private:
00122   bool pending_data(int *cid, int timeout_msec, bool include_connects);
00123   int new_cid();
00124   void init_cid(int cid, char *host, int port, int sd, State state);
00125   int read_header(int sd, MsgHeader * header);
00126   int read_body(int sd, void *buf, int bytes);
00127 
00128 private:
00129   ConnectTable * ct;            
00130   
00131   bool m_accept_connections;    
00132   int m_max_connections;        
00133 
00134 private:
00135   LogSock(const LogSock &);
00136   LogSock & operator=(const LogSock &);
00137 };
00138 
00139 #endif