socket.h

Go to the documentation of this file.
00001 #ifndef _CP_SOCKET_H
00002 #define _CP_SOCKET_H
00003 
00013 #include "common.h"
00014 
00015 #ifdef _WINDOWS
00016 #include <Winsock2.h>
00017 #endif
00018 
00019 __BEGIN_DECLS
00020 
00021 #include "config.h"
00022 
00023 #include "hashlist.h"
00024 #include "vector.h"
00025 #include "thread.h"
00026 
00027 #if defined(unix) || defined(__unix__) || defined(__MACH__)
00028 #include <sys/time.h>
00029 #include <netinet/in.h>
00030 #endif
00031 
00032 #ifdef CP_USE_SSL
00033 #include <openssl/ssl.h>
00034 #endif
00035 
00036 #if defined(sun) || defined(__OpenBSD__)
00037 #include <sys/types.h>
00038 #include <sys/socket.h>
00039 #endif
00040 
00041 #ifdef _WINDOWS
00042 #include "Winsock2.h"
00043 #endif 
00044 
00045 #define CPSOCKET_DEFAULT_BACKLOG                 50
00046 #define CPSOCKET_DEFAULT_DELAY_SEC                1
00047 #define CPSOCKET_DEFAULT_DELAY_USEC               0 
00048 #define CPSOCKET_THREADPOOL_DEFAULT_SIZE_MIN      5
00049 #define CPSOCKET_THREADPOOL_DEFAULT_SIZE_MAX     50
00050 
00052 CPROPS_DLL
00053 void cp_socket_init();
00058 CPROPS_DLL
00059 void cp_socket_stop_all();
00061 CPROPS_DLL
00062 void cp_socket_shutdown();
00063 
00064 #ifdef CP_USE_SSL
00065 CPROPS_DLL
00066 void cp_socket_ssl_init();
00067 CPROPS_DLL
00068 void cp_socket_ssl_shutdown();
00069 
00070 CPROPS_DLL
00071 SSL_CTX *get_ssl_ctx(char *certificate_file, char *key_file);
00072 CPROPS_DLL
00073 SSL_CTX *get_client_ssl_ctx(char *CA_file, char *CA_path);
00074 #endif
00075 
00079 CPROPS_DLL
00080 void cp_tcp_add_shutdown_callback(void (*cb)(void *), void *prm);
00081 
00082 
00086 typedef CPROPS_DLL enum 
00087 { 
00088     CPSOCKET_STRATEGY_CALLBACK,
00089     CPSOCKET_STRATEGY_THREADPOOL
00090 } cp_socket_strategy;
00091 
00092 CPROPS_DLL 
00093 struct _cp_socket; /* fwd, uh, declaration */
00094 
00096 typedef void *(*cp_socket_thread_function)(void *);
00098 typedef int (*cp_socket_callback)(struct _cp_socket *, int fd);
00099 #ifdef CP_USE_SSL
00100 
00101 typedef int (*cp_socket_ssl_callback)(struct _cp_socket *, SSL *ssl);
00102 #endif
00103 
00119 typedef CPROPS_DLL struct _cp_socket
00120 {
00121     int id;                       
00122     int port;                     
00123     int backlog;                  
00124     struct timeval delay;         
00125     int connections_served;       
00126     struct timeval created;       
00127 #if defined(unix) || defined(__unix__) || defined(__MACH__)
00128     int fd;                       
00129 #else
00130 #ifdef _WINDOWS
00131     SOCKET fd;
00132 #endif /* _WINDOWS */
00133 #endif /* unix */
00134     cp_hashlist *fds;             
00135     cp_thread_pool *tpool;        
00136     int poolsize_min;             
00137     int poolsize_max;             
00138     unsigned int fdn;             
00139     cp_socket_strategy strategy;  
00140     union
00141     {
00142         cp_socket_thread_function thread_fn;
00143         cp_socket_callback callback;
00144 #ifdef CP_USE_SSL
00145         cp_socket_ssl_callback ssl_callback;
00146 #endif
00147     } action;                     
00148     int closing;                  
00149     void *owner;
00150 #ifdef CP_USE_SSL
00151     int use_ssl;                  
00152     SSL_CTX *ctx;                 
00153 #endif
00154     cp_vector *shutdown_callback; 
00155 } cp_socket;
00156 
00158 CPROPS_DLL
00159 void cp_socket_set_backlog(cp_socket *socket, int backlog);
00161 CPROPS_DLL
00162 void cp_socket_set_delay(cp_socket *socket, struct timeval delay);
00164 CPROPS_DLL
00165 void cp_socket_set_delay_sec(cp_socket *socket, long sec);
00167 CPROPS_DLL
00168 void cp_socket_set_delay_usec(cp_socket *socket, long usec);
00170 CPROPS_DLL
00171 void cp_socket_set_poolsize_min(cp_socket *socket, int min);
00173 CPROPS_DLL
00174 void cp_socket_set_poolsize_max(cp_socket *socket, int max);
00176 CPROPS_DLL
00177 void cp_socket_set_owner(cp_socket *socket, void *owner);
00178 
00180 CPROPS_DLL
00181 cp_socket *cp_socket_create(int port, cp_socket_strategy strategy, void *fn);
00182 #ifdef CP_USE_SSL
00183 
00184 CPROPS_DLL
00185 cp_socket *cp_socket_create_ssl(int port, 
00186                                 cp_socket_strategy strategy, 
00187                                 void *fn,
00188                                 char *certificate_file,
00189                                 char *key_file,
00190                                 int verification_mode);
00192 CPROPS_DLL
00193 int cp_socket_ssl_connection_close(cp_socket *sock, SSL *ssl);
00194 #endif
00195 
00196 CPROPS_DLL
00197 void cp_socket_delete(cp_socket *sock);
00199 CPROPS_DLL
00200 int cp_socket_listen(cp_socket *sock);
00202 CPROPS_DLL
00203 int cp_socket_select(cp_socket *sock);
00205 CPROPS_DLL
00206 int cp_socket_connection_close(cp_socket *sock, int fd);
00208 CPROPS_DLL
00209 void *cp_socket_add_shutdown_callback(cp_socket *sock, 
00210                                       void (*cb)(void *),
00211                                       void *prm);
00212 
00214 typedef CPROPS_DLL struct _cp_connection_descriptor
00215 {
00216     cp_socket *sock;            
00217     struct sockaddr_in *addr;   
00218 #if defined(unix) || defined(__unix__) || defined(__MACH__)
00219     int fd;                     
00220 #else
00221 #ifdef _WINDOWS
00222     SOCKET fd;
00223 #endif /* _WINDOWS */
00224 #endif /* unix */
00225     void *owner;                
00226 #ifdef CP_USE_SSL
00227     SSL *ssl;                   
00228 #endif
00229     struct timeval created;     
00230     unsigned long bytes_read;   
00231     unsigned long bytes_sent;   
00232 } cp_connection_descriptor;
00233 
00235 CPROPS_DLL
00236 cp_connection_descriptor *cp_connection_descriptor_create(cp_socket *sock, 
00237                                                     struct sockaddr_in *addr, 
00238                                                     int fd);
00239 #ifdef CP_USE_SSL
00240 
00241 CPROPS_DLL
00242 cp_connection_descriptor *cp_connection_descriptor_create_ssl(cp_socket *sock, 
00243                                                     struct sockaddr_in *addr, 
00244                                                     int fd, 
00245                                                     SSL *ssl);
00246 #endif
00247 
00248 CPROPS_DLL
00249 void cp_connection_descriptor_destroy(cp_connection_descriptor *conn_desc);
00250 
00252 #define cp_connection_descriptor_get_socket(cd) ((cd)->sock)
00253 
00254 #define cp_connection_descriptor_get_addr(cd) ((cd)->addr)
00255 
00256 #define cp_connection_descriptor_get_fd(cd) ((cd)->fd)
00257 
00259 CPROPS_DLL
00260 int cp_connection_descrpitor_read(cp_connection_descriptor *desc, 
00261                                   char *buf, int len);
00262 
00264 CPROPS_DLL
00265 int cp_connection_descriptor_write(cp_connection_descriptor *desc, 
00266                                    char *buf, int len);
00267 
00268 __END_DECLS
00269 
00272 #endif /* _CP_SOCKET_H */

Generated on Mon Dec 5 23:00:22 2011 for cprops by  doxygen 1.4.7