/common/home/bel/bella/lnx/exports/build/current/all/sys-tools/ix-lib/ix_tools_i.h

00001 #ifndef IX_TOOLS_I_H
00002 #define IX_TOOLS_I_H
00003 
00004 #include <pthread.h>
00005 #include <netinet/in.h>
00006 #include <netdb.h> 
00007 #include <values.h>
00008 #include <limits.h>
00009 #include <sys/time.h>
00010 #include <string.h>
00011 #include <stdlib.h>
00012 #include <stdio.h>
00013 #include <unistd.h>
00014 #include "ix_tools.h"
00015 
00016 #ifdef DEBUG
00017 #define VERBOSEDEBUG
00018 #endif
00019 
00020 #define min(a,b) ((a)<(b)?(a):(b))
00021 #define max(a,b) ((a)>(b)?(a):(b))
00022 
00023 /*-----------------------------------------------------------------
00024  * internal verbosity level
00025  */
00026 typedef enum {     
00027         I_IX_V_LOG    = 0x00,
00028   I_IX_V_ALERT  = 0x01,
00029   I_IX_V_ERROR   = 0x002, 
00030   I_IX_V_DISP    = 0x004,
00031         I_IX_V_TIMER            = 0x008,
00032         I_IX_V_MSG     = 0x010,
00033         I_IX_V_EV      = 0x020,
00034         I_IX_V_THREAD  = 0x040,
00035         I_IX_V_FD      = 0x080,
00036   I_IX_V_SHM      = 0x100,
00037         I_IX_V_SIG      = 0x200,
00038   I_IX_V_ALL     = 0xFFFFFFFF
00039 } i_ix_verbose_e;
00040 
00041 
00042 #define IX_EV_MAX        0xFFFF
00043 
00044 #define IX_VMS_EVCLUSTER_SIZE 32
00045 
00046 // max number of events
00047 #define IX_MAX_EVENTS 288
00048 // user available events
00049 // IX_MAX_USER_EVENTS must be multiple of IX_VMS_EVCLUSTER_SIZE
00050 #define IX_MAX_USER_EVENTS 128
00051 // 
00052 // 0 .. IX_MAX_USER_EVENTS - 1         for user usage
00053 // IX_MAX_USER_EVENTS .. IX_ADM_EVENTS for usage
00054 // IX_MAX_ADM_EVENTS .. IX_MAX_EVENTS for internal usage
00055 #define IX_MAX_ADM_EVENTS 256
00056 
00057 // first internal event
00058 #define IX_SYS_EVENT0  IX_MAX_ADM_EVENTS
00059 // #define IX_SYS_EVENT1 IX_SYS_EVENT0+1
00060 /*
00061  * IX_SYS_EVENT0 : main message queue
00062  * IX_SYS_EVENT1 : ....
00063  */
00064 
00065 #define IX_VMS_EVCLUSTER_MAX (IX_MAX_USER_EVENTS/IX_VMS_EVCLUSTER_SIZE)
00066 
00067 
00068 // max event listener per event
00069 #define IX_MAX_EVENT_LISTENER 256
00070 
00071 // max number of parallel timers
00072 #define MAX_TIMER_POOL 512
00073 
00074 // max shared mem sections
00075 #define IX_MAX_SHM 16
00076 
00077 // range of dynamic ports
00078 #define IX_PORT_START 50000
00079 #define IX_PORT_END   65535
00080 
00081 // max number of connections of tcp-clients to server
00082 #define MAX_TCP_CLIENT_POOL 300
00083 
00084 // max number of tcp-server connections
00085 #define MAX_TCP_SERVER_POOL 256
00086 
00087 // max number of surveyed file descriptors
00088 #define IX_MAX_FD 256
00089 
00090 // ID of ix connections
00091 #define IX_CONN_ID    0x4958 // IX
00092 
00093 // ID of net connection packet
00094 #define IX_NET_CONN_ID 0x434E  // CN
00095 
00096 // ID of data connection packet
00097 #define IX_DATA_CONN_ID 0x4344  // CD
00098 
00099 // maximum queue count of pending connections to be accepted (for listen call)
00100 #define IX_SOCKET_LISTEN_QUEUE 8
00101 
00102 // when connection is refused we try again
00103 //
00104 // retry count to connect 
00105 #define IX_SOCKET_CONNECT_RETRY 3
00106 
00107 // wait time in microseconds before connect again
00108 #define IX_SOCKET_CONNECT_MICROSEC_WAIT 3000
00109 
00110 // timeout for send,reply,receive mechanism per TCP
00111 #define IX_SEND_TIO 10
00112 
00113 // max length of net packet
00114 #define IX_MAX_NET_MSG_LEN (1024*32)
00115 
00116 // highest signal we process
00117 #define IX_MAX_SIGNAL 32
00118 
00119 //#define IX_MAX_APPL_NAME_LEN 256
00120 
00121 // ID of net packets to identify
00122 #define IX_NET_ID 0x5800
00123 
00124 // max number of user hooks
00125 #define IX_MAX_USER_HOOK 16
00126 
00127 // version of net packet
00128 #define IX_NET_V1 0x0001
00129 
00130 // we test for endian type
00131 #define IX_LBE 0xFF00   // test for little or big endian
00132 
00133 //  message types for internal communication through pipe
00134 #define IX_INTERN_ID 0x5849
00135 #define IX_TIMER_NEW 0x4E54
00136 #define IX_TIMER_DEL 0x4454
00137 #define IX_CONNECT 0x4343
00138 #define IX_FD      0x4643
00139 #define IX_SIGNAL  0x4743
00140 #define IX_CLIENT_RM  0x4843
00141 #define IX_SERVER_ACTION  0x4943
00142 
00143 // timer flags
00144 #define IX_TIMER_INIT     0x00
00145 #define IX_TIMER_ACTIVE   0x01
00146 #define IX_TIMER_SYSTEM   0x02
00147 
00148 // intervall in sec of internal ix timer
00149 #define IX_SYSTIMER_SEC 1
00150 
00151 // client inactivity timeout in secs (after that server closes connection)
00152 //#define IX_CLIENT_TIMEOUT 3600
00153 #define IX_CLIENT_TIMEOUT 300
00154 
00155 // timeout for write in secs
00156 #define IX_WRITE_TIMEOUT 4
00157 
00158 // timeout to receive replies in seconds
00159 #define IX_REPLY_TIMEOUT 8
00160 
00161 // timeout to sync server actions with dispatcher
00162 #define IX_SERVER_ACTION_TIMEOUT 2
00163 
00164 //#define IX_MAX_SHM_KEY 6
00165 
00166 // max number of threads
00167 #define MAX_THREAD_POOL 512
00168 
00169 // max number of messages in message queues
00170 #define IX_MSG_MAX 512
00171 
00172 // max size of hostname
00173 #define IX_HOST_NAME_MAX 64
00174 
00175 #define IX_SEM_FN "SEM_GROUP"
00176 
00177 // critical section
00178 struct ix_cs_s
00179 {
00180     pthread_mutex_t mt;
00181 };
00182 int ix_cs_init( ix_cs_t *pcs, ix_cs_e cs_type );
00183 
00184 #define IX_SEM_UNLOCK 0x00
00185 #define IX_SEM_LOCK 0x01
00186 #define IX_SEM_POLLNO 3
00187 // semaphore
00188 struct ix_sem_s
00189 {
00190   int id;        // id of sem
00191   int fd;     // descriptor of lock file
00192   char fn[PATH_MAX]; // name of lock file
00193   int state;  // wait, locked, unlocked
00194   int mode;   // IX_SEM_LOCAL, _GLOBAL, _FD
00195   // not used so far
00196   ix_event_t *pev; // event to trigger when available
00197   int group;     //  group id of sem
00198   ix_cs_t cs;
00199 }; 
00200 
00201 struct ix_fd_s
00202 {
00203   int fd;
00204   ix_event_t *pev;
00205   int flag;
00206   ix_cs_t cs;
00207   IX_FD_FCT cb;
00208   void *userdata;
00209 };
00210 
00211 // event listener
00212 struct ix_evl_s
00213 {
00214   pthread_cond_t cond;
00215   pthread_mutex_t mut;
00216   int id;
00217 };
00218 
00219 // event set
00220 struct ix_evs_s 
00221 {
00222   int no;
00223   ix_event_t *ev[IX_MAX_EVENTS];
00224   int evl_init;
00225   ix_evl_t evl; // to each mask we associate a listener
00226 };
00227 
00228 // event
00229 struct ix_event_s
00230 {
00231     unsigned long id_intern;
00232     int type;    
00233     int set;        // counter
00234     int id;         // set by user
00235     ix_cs_t cs;
00236     ix_list_t *listener;
00237     ix_list_t *appl_listener;
00238     void *userdata;
00239 }; 
00240 
00241 // thread
00242 struct  ix_thread_s
00243 {
00244   pthread_t pth;
00245   pthread_attr_t attr;
00246   char name[64];
00247   IX_START_FCT sfct;
00248   IX_EXIT_FCT efct;
00249   void *parg;
00250   pthread_key_t *pkey;
00251   int exit_flag;
00252   int init;
00253   struct ix_thread_s *pme;
00254 };
00255 
00256 // application
00257 struct ix_appl_s 
00258 {
00259   int socketfd;
00260   int state;
00261   struct in_addr hostaddr;
00262   unsigned short applport;
00263   char hostname[PATH_MAX];
00264   char applname[PATH_MAX];
00265 };
00266 
00267 // timer
00268 struct ix_timer_s 
00269 {
00270   unsigned long  id;         // id of timer to make compare function unique
00271   unsigned long  userid;     // user supplied id
00272   ix_event_t  *pev;      // event to trigger when timer expires
00273   ix_cs_t     cs;
00274   int   flag;            // type and state of timer
00275   struct timeval tv;     // time when timer fires
00276   struct timeval tv_per; // period of periodic timer
00277   void (*pcb) ();        // user supplied call back
00278   void *parg;            // argument for call back function
00279 };
00280 
00281 // message queue
00282 struct ix_msg_queue_s
00283 {
00284   ix_queue_t *pq;
00285   ix_event_t *pev;
00286 };
00287 
00288 struct ix_qio_s
00289 {
00290   int fd;
00291   int ctrl_tag;
00292   ix_qio_ctrl_t ctrl;
00293   IX_QIO_SAVE_FCT save_fct;
00294   void *userarg;
00295   ix_msg_queue_t *pmq;
00296   ix_buffer_t *pbuf;
00297   int len;
00298   ix_cs_t cs;
00299   ix_fd_t *pfd;
00300   int status;
00301 };
00302 
00303 
00304 typedef struct
00305 {
00306   int key;
00307   IX_AST_PACKET_FCT user_hook;
00308 } hook_entry_t;
00309 
00310 // internal receive 
00311 typedef struct
00312 {
00313   IX_AST_PACKET_FCT hook_fct;
00314   ix_cs_t cs;
00315   int blocked;
00316   hook_entry_t hook[IX_MAX_USER_HOOK];
00317   int hook_count;
00318 } ix_receive_t;
00319 
00320 
00321 // net packet
00322 typedef enum 
00323 {
00324   IX_NET_MIN       = 0x00,
00325   IX_NET_EVENT     = 0x01,
00326   IX_NET_MSGUNCONF = 0x02,
00327   IX_NET_SEND      = 0x05,
00328   IX_NET_REPLY     = 0x06,
00329   IX_NET_NOTACK    = 0x07,
00330   IX_NET_ERROR     = 0xFF,
00331   IX_NET_MAX,
00332   IX_NET_VAL = 0xFFFF
00333 } ix_net_type_e;
00334 
00335 typedef struct ix_hdr_s 
00336 {
00337   unsigned short id;     // id in network order
00338   unsigned short lbe;    // 0xFF00  in host order to test endian type 
00339                          // intel: least significant byte first
00340                          // network: most significant byte first
00341                          // receiver checks: 0xFF00 == lbe 
00342                          // if neq: byte order has to be changed in msg
00343   unsigned short type;   // type  in host order
00344   unsigned int   len;    // length in host order
00345   unsigned short port;   // port in network order the packet really comes from
00346                          // filled after packet is received
00347 } ix_hdr_t;
00348 
00349 struct ix_net_s
00350 {
00351   ix_hdr_t hdr;
00352   ix_adr_t from;
00353   ix_adr_t to;
00354   ix_netinfo_t info;
00355   union 
00356   {
00357   IX_SERVER_DISCONNECT_FCT disconn_cb;
00358   long long int align;
00359   };
00360   ix_buffer_t b;
00361   unsigned char msg[IX_MAX_NET_MSG_LEN];
00362 };
00363 
00364 typedef unsigned int ix_cluster_t;
00365 
00366 typedef struct
00367 {
00368   unsigned short ix_id;            // in net order
00369   unsigned short conn_id;         // in net order
00370 } ix_conn_t;
00371 
00372 
00373 #define IX_TCP_INIT          0x00
00374 #define IX_TCP_CONNECTED     0x01
00375 #define IX_NET_CONNECTED     0x02
00376 #define IX_DATA_CONNECTED    0x04
00377 // #define IX_TCP_DATA          0x02
00378 
00379 //typedef struct ix_tcp_client_s
00380 struct ix_tcp_client_s
00381 {
00382   int fd;               // file descriptor of connected client socket
00383   ix_adr_t adr;         // host adress and host port the request comes from
00384   int state;
00385   ix_net_t *pnet;       // dynamic actual buffer
00386   time_t access_time;   // last acces time from client
00387   int timeout;          // if gt 0, client is removed after timeout sec of inactivity
00388   ix_cs_t cs;           // critical section to allow synchronized use of userdata
00389   void *userdata;       // this is for userdata available between calls of callback
00390 
00391   int header_f;         // flag that header in reply is read
00392   int len;              // length of buffer filled
00393   char *buffer;         // buffer to store reply in
00394 };
00395 
00396 
00397 
00398 typedef int( * IX_SERVER_READ_FCT)( void *ps );
00399 
00400 typedef enum
00401 {
00402   IX_SERVER_INIT = 0x00,
00403   IX_SERVER_ERROR = 0x01,
00404   IX_SERVER_SENT = 0x02
00405 
00406 } ix_server_state_e;
00407 
00408 typedef struct ix_tcp_server_s
00409 {
00410   int fd;               // file descriptor of connected server socket
00411   ix_adr_t adr;         // host adress and host port we connect to
00412   struct ix_cs_s cs;         // critical section for access server
00413   struct ix_cs_s socket_cs;  // critical section for using socket
00414   ix_net_t *pnet;       //  buffer to write reply to
00415   int shared;
00416   int state;
00417   ix_cond_t sync;       // the condition to trigger when reply received
00418   IX_SERVER_DISCONNECT_FCT disconn_cb; // callback if server disonnects
00419 
00420   int header_f;         // flag that header in reply is read
00421   int len;              // length of buffer filled
00422   char *buffer;         // buffer to store reply in
00423 } ix_tcp_server_t;
00424 
00425 struct ix_signal_s 
00426 {
00427   int signal;
00428   ix_event_t *pev;
00429   void *userdata;
00430   IX_SIGNAL_FCT cb;
00431 };
00432 
00433 int rm_tcp_server( ix_tcp_server_t *ps );
00434 
00435 // global init structure
00436 typedef struct ix_init_s
00437 {
00438   int done;
00439   sigset_t sigmask_block;
00440   char proc_name[PATH_MAX]; // for informational usage: user supplied
00441                             // process-name
00442   int node;                 // for informational usage: user supplied id
00443                             // for host identification
00444   ix_list_t *ptp;     // Thread pool
00445   ix_list_t *pti;     // active timer list
00446   ix_cs_t ev_cs;      // critical section for dealing wiht event[]
00447   ix_cs_t server_cs; // .. .for dealing with tcpserver:
00448   ix_cs_t fdcs;   // ...for dealing with filedescriptors
00449   ix_cs_t initcs;   // ...to avoid ix_init() concurrency faults
00450   ix_event_t *event[IX_MAX_EVENTS];  // array of max events 
00451   ix_cluster_t ev_cluster[IX_VMS_EVCLUSTER_MAX];
00452   int pipe[2]  ;      // internal pipe
00453   int udp_out;     // udp socket for client output 
00454   char udp_host[IX_HOST_NAME_MAX]; // our udp host name
00455   int udp_port;       // our udp port
00456   int udp_fd;         // udp socket for server input
00457   char tcp_host[IX_HOST_NAME_MAX]; // our tcp host name
00458   int tcp_port;       // our tcp port
00459   int tcp_fd;         // tcp socket for server input
00460   ix_list_t *ptcpcl;    // list with connected tcp-clients
00461   ix_list_t *ptcpsl;    // list with connected tcp-servers
00462   ix_list_t *pfdl;      // list with surveyed fd-descriptors
00463   fd_set rfds;              // read file descriptor result set
00464   fd_set wfds;              // write ...
00465   fd_set efds;              // error ...
00466   fd_set active_rfds;       // ...........          active set
00467   fd_set active_wfds;       // ...........          active set
00468   fd_set active_efds;       // ...........          active set
00469   int nfds;                 // highest number of fd
00470   int active_nfds;          // highest number of fd to set
00471   ix_cs_t fd_cs;      // critical section for dealing with fdsets
00472   ix_msg_queue_t msg; // main message queue for udp and tcp input messages
00473   ix_receive_t recv;  // this is for rebuild a special message receive
00474   int client_timeout; // timeout for idle client in sec
00475   ix_list_t *pshml;   // list with shared memory sections
00476   IX_CLIENT_CONNECT_FCT    client_connect_cb; 
00477   IX_CLIENT_REPLY_FCT      client_reply_cb; 
00478   IX_CLIENT_DATA_FCT       client_data_cb; 
00479   IX_CLIENT_DISCONNECT_FCT client_disconnect_cb; 
00480   IX_SERVER_DISCONNECT_FCT server_disconnect_cb; 
00481   struct ix_signal_s * signal[IX_MAX_SIGNAL];
00482   IX_EVENT_HOOK_FCT         event_hook_fct;
00483 } ix_init_t;
00484 
00485 void i_ix_log_level( unsigned long flag );
00486 void i_ix_log( i_ix_verbose_e flags, char *text, ... );
00487 void  _ix_evl_signal( ix_evl_t *pevl  );
00488 int  _ix_evl_wait( ix_evl_t *pevl, ix_evs_t *mask, ix_evs_t *set,
00489     int msec );
00490 inline ix_event_t *_ix_evs_add( ix_evs_t *pevs, ix_event_t *pev );
00491 void  ix_evl_init( ix_evl_t *pevl  );
00492 int start_dispatcher(void);
00493 int snd_timer(void );
00494 int snd_fd( void );
00495 void *dispatch_fct( void  *arg);
00496 int receive_init(void );
00497 int ix_udp_send( ix_net_t *pnet );
00498 int ix_tcp_send( ix_net_t *pnet, ix_net_t *preply );
00499 int snd_connect( void  );
00500 ix_event_t *_ix_event_new( ix_cs_t *pcs, int id, int flags, void *userdata );
00501 int add_selrfd( int fd );
00502 /*
00503 static int add_selwfd( int fd );
00504 static int add_selefd( int fd );
00505 */
00506 int add_dyn_selrfd( int fd );
00507 int add_dyn_selwfd( int fd );
00508 int add_dyn_selefd( int fd );
00509 int rm_dyn_selrfd( int fd );
00510 int rm_dyn_selwfd( int fd );
00511 int rm_dyn_selefd( int fd );
00512 int init_selfd(void );
00513 int init_fds(void);
00514 //ix_buffer_t *ix_buffer_new( unsigned char *pbuf, size_t len );
00515 int ix_buffer_init( ix_buffer_t *pb, unsigned char *pbuf, size_t len );
00516 //int ix_buffer_resize( ix_buffer_t *pb, size_t len );
00517 //void ix_buffer_free( ix_buffer_t *pb );
00518 int ix_msg_queue_init( ix_msg_queue_t *pmq, ix_event_t *pev );
00519 int ix_msg_queue_size_init( ix_msg_queue_t *pmq, ix_event_t *pev, size_t size );
00520 int fd_comp_fct( const void *pth1, const void *pth2 );
00521 int shm_comp_fct( const void *pshm1, const void *pshm2 ); 
00522 
00523 
00524 
00525 #endif
00526 

Generated on 4 Mar 2014 for ixtools by  doxygen 1.4.7