• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

LogSock.h

Go to the documentation of this file.
00001 /** @file
00002 
00003   A brief file description
00004 
00005   @section license License
00006 
00007   Licensed to the Apache Software Foundation (ASF) under one
00008   or more contributor license agreements.  See the NOTICE file
00009   distributed with this work for additional information
00010   regarding copyright ownership.  The ASF licenses this file
00011   to you under the Apache License, Version 2.0 (the
00012   "License"); you may not use this file except in compliance
00013   with the License.  You may obtain a copy of the License at
00014 
00015       http://www.apache.org/licenses/LICENSE-2.0
00016 
00017   Unless required by applicable law or agreed to in writing, software
00018   distributed under the License is distributed on an "AS IS" BASIS,
00019   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00020   See the License for the specific language governing permissions and
00021   limitations under the License.
00022  */
00023 
00024 
00025 
00026 #ifndef LOG_SOCK_H
00027 #define LOG_SOCK_H
00028 
00029 #include "libts.h"
00030 
00031 /*-------------------------------------------------------------------------
00032   LogSock
00033 
00034   This class implements a multiplexed socket class that supports both
00035   client and server functionality.
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);          // this connection
00084   void close();                 // all connections
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;                 // hostname for this connection
00111     int port;                   // port number for this connection
00112     int sd;                     // socket descriptor for this connection
00113     State state;                // state of this entry
00114   };
00115 
00116   struct MsgHeader
00117   {
00118     int msg_bytes;              // length of the following message
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;            // list of all connections; index 0 is
00130   // the accept port.
00131   bool m_accept_connections;    // do we accept new connections?
00132   int m_max_connections;        // max size of all tables
00133 
00134 private:
00135   LogSock(const LogSock &);
00136   LogSock & operator=(const LogSock &);
00137 };
00138 
00139 #endif

Generated by  doxygen 1.7.1