hsms_socket.h

00001 /*   
00002  *   (c) Copyright 2008 Philipp Skadorov (philipp_s@users.sourceforge.net)
00003  *
00004  *   This file is part of FREESECS.
00005  *
00006  *   FREESECS is free software: you can redistribute it and/or modify
00007  *   it under the terms of the GNU General Public License as published by
00008  *   the Free Software Foundation, either version 3 of the License, or
00009  *   (at your option) any later version.
00010  *
00011  *   FREESECS is distributed in the hope that it will be useful,
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *   GNU General Public License for more details.
00015  *
00016  *   You should have received a copy of the GNU General Public License
00017  *   along with FREESECS, see COPYING.
00018  *   If not, see <http://www.gnu.org/licenses/>.
00019  */
00020 #ifndef hsms_socket_class
00021 #define hsms_socket_class
00022 
00023 #include <sys/types.h>
00024 #include <sys/socket.h>
00025 #include <netinet/in.h>
00026 #include <arpa/inet.h>
00027 #include <netdb.h>
00028 #include <unistd.h>
00029 #include <vector>
00030 #include <string>
00031 #include "async_reception.h"
00032 #include "signals.h"
00033 #include "shared_ptr.h"
00034 
00035 const int MAXHOSTNAME = 200;
00036 const int MAXCONNECTIONS = 5;
00037 
00038 namespace freesecs
00039 {
00045     class hsms_socket_t 
00046     {
00047     public:
00048         typedef std::vector<unsigned char>  data_t;
00049         typedef shared_ptr_t<data_t>   pdata_t;
00050 
00051         typedef enum
00052         {
00053             NOT_CONNECTED,
00054             CONNECTING,
00055             CONNECTED
00056         }cnx_state_et;
00057 
00061         signal_one_arg_t<cnx_state_et>  cnx_state_signal;
00065         signal_no_args_t                cnx_failed_signal;
00066 
00070         hsms_socket_t(const char *name, const int port);
00074         hsms_socket_t(const char *name, const char *host, int port);//active socket ctor
00075         virtual ~hsms_socket_t();
00076 
00077     public:
00081         int             connect         ();
00085         int             disconnect      ();
00089         int             send            (pdata_t);
00093         int             receive         (pdata_t);
00097         cnx_state_et    get_cnx_state   () const;
00098 
00099     protected:
00103         int         connect_tcp        ();
00107         virtual int disconnect_tcp     ();
00111         int         send_tcp           (pdata_t);
00115         virtual int recv_tcp           (pdata_t)=0;
00116 
00117 
00118     protected:
00122         int connect_to      ();
00126         int bind            ();
00130         int listen          ();
00134         int accept  ();
00135 
00136     protected:
00141         typedef enum
00142         {
00143             e_connect,
00144             e_connect_failed,
00145             e_connected,
00146             e_disconnect,
00147             e_send,
00148             e_recv
00149         }sock_cnx_ev_et;
00150 
00155         typedef struct
00156         {
00157             sock_cnx_ev_et  type;
00158             pdata_t         pdata;
00159         } sock_cnx_ev_t;
00160 
00164         int process (sock_cnx_ev_t);
00165 
00166     private:
00167         int z1      (sock_cnx_ev_t);
00168         int z2      (sock_cnx_ev_t);
00169         int z3      (sock_cnx_ev_t);
00170         int z1_0    (sock_cnx_ev_t);
00171         int z2_0    (sock_cnx_ev_t);
00172         int z3_0    (sock_cnx_ev_t);
00173         cnx_state_et    _cnx_state;
00174         static void     *tcp_cnx_thread(void *arg);
00175 
00176     protected:
00177         std::string _name;
00178         std::string _cnx_sm_name;
00179         std::string _remote_host;
00180         int         _port;
00181         int         _sock;
00182         sockaddr_in _addr;
00183 
00184     private:
00185         int create_socket();
00186 
00187     private:
00188         hsms_socket_t();
00189         hsms_socket_t(const hsms_socket_t&);
00190     };
00191 
00192 
00199     class hsms_socket_async_t : public hsms_socket_t
00200     {
00201     public:
00206         typedef enum
00207         {
00208             inactive,
00209             wtg_for_header,
00210             wtg_for_data,
00211         }state_t;
00212 
00216         signal_one_arg_t<pdata_t>     msg_signal;
00221         signal_no_args_t              data_partially_recvd;
00225         signal_no_args_t              recv_error;
00226 
00227     public:
00231         hsms_socket_async_t(const char *name, const int port);
00235         hsms_socket_async_t(const char *name, const char *host, const int port);
00236         virtual ~hsms_socket_async_t();
00237 
00238     protected:
00244         virtual int disconnect_tcp();
00251         virtual int recv_tcp(pdata_t);
00252 
00253         state_t get_state() const {return _state;}
00254     /*sm stuff-->*/
00255     private:
00259         typedef enum
00260         {
00261             e_i_init,
00262             e_i_deinit,
00263             e_i_data_received,
00264             e_i_recv_error
00265         }internal_event_t;
00266 
00271         bool x0(); 
00275         bool x1();
00279         bool x2();
00283         void z0();
00287         void z1();
00291         void z2();
00295         void z3();
00299         void z0_0();
00303         void z1_0();
00308         void z2_0(); 
00309 
00313         void process(internal_event_t);
00314 
00315         state_t _state;
00316     /*sm stuff<--*/
00317     private:
00318         pdata_t  _pmessage;
00319         size_t  _bytes_read;
00320         int     _rx_error;
00321 
00322     private:
00323         int data_rcvd_handler(const size_t&);
00324         int rcv_error_handler(const int&);
00325 
00326         shared_ptr_t<async_reception_t> _ar;
00327 
00328     private:
00329         friend class hsms_passive_cnx_t;
00330     };
00331 
00332 
00333     #ifdef USE_TIMED_SOCK
00334     class hsms_socket_timed_t : public hsms_socket_t
00335     {
00336     public:
00337         void set_non_blocking(const bool);
00338 
00339         int recv(data_t & data, int timeout_ms) const;
00340 
00341     //receive a certain number of bytes
00342     //wait for all data
00343         int recvn(data_t & data, size_t size, int timeout_ms) const;
00344 
00345     protected:
00346         int _recv(data_t & data, size_t size, bool wait_all, bool flash_unwanted,
00347                    int timeout) const;
00348        
00349     //if timeout_ms <= 0, timeout is reset
00350         static void _set_timer(int sock_fd, int timeout_ms);
00351     };
00352 #endif 
00353 }//namespace
00354 #endif  

Generated on Fri Oct 3 15:30:01 2008 for FREESECS hsms by  doxygen 1.5.1