Main Page   Class Hierarchy   Compound List   File List   Compound Members  

C_Socket.hpp

00001 /*
00002  *  This program is free software; you can redistribute it and/or modify
00003  *  it under the terms of the GNU General Public License as published by
00004  *  the Free Software Foundation; either version 2 of the License, or
00005  *  (at your option) any later version.
00006  *
00007  *  This program is distributed in the hope that it will be useful,
00008  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00009  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010  *  GNU General Public License for more details.
00011  *
00012  *  You should have received a copy of the GNU General Public License
00013  *  along with this program; if not, write to the Free Software
00014  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00015  *
00016  * (c)Copyright 2006 Hewlett-Packard Development Company, LP.
00017  *
00018  */
00019 
00020 #ifndef _CPP_SOCKET_
00021 #define _CPP_SOCKET_
00022 
00023 #include "list_t.hpp"
00024 #include "map_t.hpp"
00025 
00026 #include "S_IpAddr.hpp"
00027 #include <netinet/tcp.h>
00028 #include <netdb.h>
00029 
00030 #include "C_Transport.hpp"
00031 #include "C_TransportEvent.hpp"
00032 #include "C_DataDecode.hpp"
00033 #include "C_ProtocolBinaryFrame.hpp"
00034 
00035 #include "ReceiveMsgContext.h"
00036 
00037 typedef enum _enum_socket_mode {
00038   E_SOCKET_TCP_MODE,
00039   E_SOCKET_UDP_MODE
00040 } T_SocketType, *T_pSocketType ;
00041 
00042 typedef struct _data_rcv {
00043   size_t         m_size ;
00044   struct timeval m_time ;
00045   unsigned char *m_data ;
00046 } T_DataRcv, *T_pDataRcv ;
00047 
00048 typedef enum _socket_state {
00049   E_SOCKET_STATE_NOT_READY = 0,
00050   E_SOCKET_STATE_INPROGESS,
00051   E_SOCKET_STATE_READY
00052 } T_SocketState, *T_pSocketState ;
00053 
00054 typedef list_t<T_pDataRcv> T_DataRcvList, *T_pDataRcvList ;
00055 
00056 class C_Socket {
00057 public:
00058   C_Socket (int P_channel_id) ;
00059   C_Socket (T_SocketType P_type, int P_port, int P_channel_id);
00060   C_Socket (T_SocketType P_type, int P_channel_id);
00061   C_Socket (C_Socket& P_Socket); // Socket with no data setted
00062   virtual ~C_Socket () ;
00063 
00064   virtual int get_prototype () ;
00065   virtual size_t received_buffer (unsigned char  *P_data, 
00066                                   size_t          P_size_buf,
00067                                   struct timeval *P_time) = 0 ;
00068   virtual void      set_fd_set (fd_set *P_rSet, fd_set *P_wSet);
00069   virtual C_Socket* process_fd_set(fd_set           *P_rSet, 
00070                                    fd_set           *P_wSet,
00071                                    C_TransportEvent *P_event) = 0 ;
00072 
00073   virtual size_t send_buffer(unsigned char     *P_data, 
00074                              size_t             P_size) = 0 ;
00075   virtual C_pDataDecode          get_decode () ;
00076   virtual T_pRcvMsgCtxtList      get_list() ;
00077 
00078 
00079   void                   set_properties () ;
00080 
00081   int                    _open (int    P_socket_domain, 
00082                                 size_t P_buffer_size,
00083                                 C_ProtocolBinaryFrame *P_protocol);
00084   int                    get_id () ;
00085   int                    get_channel_id();
00086   void                   _close () ;
00087   void                   set_channel_id(int P_channel_id);
00088   C_ProtocolBinaryFrame* get_protocol() ;
00089 
00090 protected:
00091 
00092   int                 m_socket_id   ; 
00093   int                 m_channel_id     ;
00094   T_SocketType        m_type        ;
00095   int                 m_src_port    ;
00096   T_SocketState       m_state       ;
00097   size_t              m_buffer_size ;
00098 
00099   // decode process temporary
00100   C_ProtocolBinaryFrame  *m_protocol ;
00101 
00102 } ;
00103 
00104 class C_SocketWithData : public C_Socket {
00105 
00106 public:
00107 
00108   C_SocketWithData(int P_channel_id,
00109                    size_t P_read_buf_size,
00110                    size_t P_segm_buf_size);
00111   C_SocketWithData(T_SocketType P_type, 
00112                    T_pIpAddr P_addr, 
00113                    int P_channel_id,
00114                    size_t P_read_buf_size,
00115                    size_t P_segm_buf_size) ;
00116   C_SocketWithData(C_SocketWithData &P_Socket);
00117   virtual ~C_SocketWithData() ;
00118 
00119   virtual C_Socket* process_fd_set(fd_set *P_rSet, fd_set *P_wSet, C_TransportEvent *P_event) ;
00120   virtual C_Socket* process_fd_ready(fd_set *P_rSet, fd_set *P_wSet, C_TransportEvent *P_event);
00121   virtual C_Socket* process_fd_in_progess(fd_set *P_rSet, fd_set *P_wSet, C_TransportEvent *P_event) ;
00122   virtual int _call_read(); 
00123   virtual int _call_write(unsigned char* P_data,
00124                           size_t P_size)  ;
00125   virtual int _write(unsigned char* P_data,
00126                      size_t P_size)  ;
00127   virtual size_t send_buffer(unsigned char     *P_data, 
00128                              size_t             P_size) ;
00129   virtual int _read () = 0 ;
00130   virtual C_pDataDecode          get_decode () ;
00131   virtual T_pRcvMsgCtxtList      get_list() ;
00132   
00133   size_t received_buffer (unsigned char  *P_data, 
00134                           size_t          P_size_buf,
00135                           struct timeval *P_time) ;
00136 protected:
00137 
00138   T_DataRcvList           m_data_queue ;
00139   T_pIpAddr               m_remote_addr_info ;
00140   T_SockAddrStorage       m_accepted_addr ;
00141 
00142   size_t                  m_read_buf_size ;
00143   char                   *m_read_buf      ;
00144 
00145 
00146   T_SockAddrStorage       m_remote_sockaddr     ; 
00147   tool_socklen_t          m_len_remote_sockaddr ;
00148 
00149   T_SockAddrStorage*      m_remote_sockaddr_ptr     ; 
00150   tool_socklen_t*         m_len_remote_sockaddr_ptr ;
00151 
00152   C_pDataDecode           m_decode   ;
00153   T_pRcvMsgCtxtList       m_recv_msg_list ;
00154 
00155 } ;
00156 
00157 class C_SocketListen : public C_Socket {
00158 
00159 public:
00160 
00161   C_SocketListen(T_SocketType  P_type, 
00162                  T_pIpAddr     P_addr, 
00163                  int           P_channel_id,
00164                  size_t        P_read_buf_size,
00165                  size_t        P_segm_buf_size) ;
00166   C_SocketListen(C_SocketListen &P_Socket); // Socket with no data setted
00167   virtual ~C_SocketListen() ;
00168 
00169   virtual C_Socket* process_fd_set(fd_set           *P_rSet, 
00170                                    fd_set           *P_wSet,
00171                                    C_TransportEvent *P_event) ;
00172   virtual size_t send_buffer(unsigned char     *P_data, 
00173                              size_t             P_size);
00174   virtual int _call_listen (int P_max) ;
00175 
00176   int                _open (size_t P_buffer_size, C_ProtocolBinaryFrame *P_protocol) ;
00177   size_t             received_buffer  (unsigned char  *P_data, 
00178                                        size_t          P_size_buf,
00179                                        struct timeval *P_time) ;
00180   T_SockAddrStorage* get_source_address () ;
00181   T_SocketType       get_trans_type() ;
00182   
00183 protected:
00184   
00185   T_pIpAddr           m_source_addr_info ;
00186   size_t              m_read_buf_size ;
00187   size_t              m_segm_buf_size ;
00188 
00189   virtual C_Socket*   create_socket_server(int *P_ret) ;
00190 
00191 } ;
00192 
00193 class C_SocketServer : public C_SocketWithData {
00194 
00195 public:
00196   C_SocketServer(C_SocketListen *P_listen, 
00197                  int             P_channel_id,
00198                  size_t          P_read_buf_size,
00199                  size_t          P_segm_buf_size);
00200   C_SocketServer(T_SocketType  P_type, 
00201                  T_pIpAddr     P_addr, 
00202                  int           P_channel_id,
00203                  size_t        P_read_buf_size,
00204                  size_t        P_segm_buf_size) ;
00205   C_SocketServer (C_SocketServer& P_Socket);
00206   virtual ~C_SocketServer();
00207 
00208   virtual int _open (size_t P_buffer_size, C_ProtocolBinaryFrame *P_protocol) ;
00209   virtual int _read ();
00210 
00211   int _open_udp (size_t P_buffer_size, 
00212                  C_ProtocolBinaryFrame *P_protocol) ;
00213 
00214 private:
00215   C_SocketListen     *m_listen_sock ;
00216   T_pIpAddr           m_source_udp_addr_info ;
00217 
00218 } ;
00219 
00220 
00221 class C_SocketClient : public C_SocketWithData {
00222 public:
00223   C_SocketClient(T_SocketType  P_type, 
00224                  T_pIpAddr     P_addr, 
00225                  int           P_channel_id,
00226                  size_t        P_read_buf_size,
00227                  size_t        P_segm_buf_size) ;
00228   C_SocketClient(C_SocketClient &P_Socket);
00229   virtual ~C_SocketClient() ;
00230 
00231   virtual int _open (T_pOpenStatus P_status,
00232                      size_t        P_buffer_size,
00233                      C_ProtocolBinaryFrame *P_protocol);
00234   int _read ();
00235 
00236 } ;
00237 
00238 typedef map_t<int, C_Socket*> T_SocketMap, *T_pSocketMap ;
00239 
00240 #endif // _CPP_SOCKET_
00241 
00242 
00243 
00244 
00245 

Generated on Wed Mar 7 14:44:58 2007 for Seagull by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002