Nilorea Library

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Macros Groups
n_network.h
Go to the documentation of this file.
1
7 #ifndef N_NETWORK
8 #define N_NETWORK
9
10 #ifdef __cplusplus
11 extern "C"
12 {
13 #endif
14
20 #include "n_common.h"
21 #include "n_str.h"
22
23 #if defined( LINUX ) || defined( SOLARIS ) || defined( AIX )
24
25 #include <sys/types.h>
26 #include <netinet/in.h>
27 #include <netinet/tcp.h>
28 #include <sys/socket.h>
29 #include <arpa/inet.h>
30 #include <netdb.h>
31
32 #ifndef SOCKET
33
34 typedef int SOCKET ;
35 #endif
36
38 #define closesocket close
39
40 #ifndef SOLARIS
41 #define NETFLAGS MSG_NOSIGNAL /* for program to not quit under linux when a connection is crashing 2 times */
42 #else
43 #define NETFLAGS 0
44 #endif
45
46 #define INVALID_SOCKET -1
47
48 #elif defined WIN32 /* Else it should be WIN32 */
49
50 #include <windows.h>
51 #include <winsock.h>
52 #include <winsock2.h>
53
55 #define NETFLAGS 0 /* no flags needed for microsoft */
56
57 #else
58 #error NO VALID OS DEFINED. PLEASE DEFINE ONE OF THE FOLLOWING: LINUX,WIN,AIX,SOLARIS
59 #endif
60
62 #define NETW_CLIENT 2
63
64 #define NETW_SERVER 4
65
66 #define NETW_RESTART_TIMER 8
67
68 #define NETW_EMPTY_SENDBUF 16
69
70 #define NETW_EMPTY_RECVBUF 32
71
72 #define NETW_RUN -64
73
74 #define NETW_PAUSE -128
75
76 #define NETW_QUIT -256
77
78 #define NETW_STOP -512
79
80 #define NETW_ERROR -1024
81
82 #define NETW_THR_ENGINE_STARTED 2048
83
84 #define NETW_THR_ENGINE_STOPPED 4096
85
87 typedef struct N_SOCKET
88 {
90 int port;
92 SOCKET sock;
94 char *ip;
95
97 struct sockaddr_in haddr ;
99 struct sockaddr_in raddr ;
100
101 } N_SOCKET;
102
104 typedef struct NETWORK
105 {
107 int nb_pending,
109 mode,
111 state,
113 threaded_engine_status ;
114
116 N_SOCKET link;
117
119 LIST *send_buf;
121 LIST *recv_buf;
122
124 pthread_t send_thr;
126 pthread_t recv_thr;
127
129 pthread_mutex_t sendbolt;
131 pthread_mutex_t recvbolt;
133 pthread_mutex_t eventbolt;
134
135 } NETWORK;
136
137 /* Used by Init & Close network */
138 int handle_wsa( int mode , int v1 , int v2 );
139 /* Restart or reset the specified network ability */
140 int netw_set( NETWORK *netw , int flag );
141 /* Set common socket options (disable naggle, send/recv buf, reuse addr) */
142 int netw_setsockopt( SOCKET sock , int disable_naggle , int sock_send_buf , int sock_recv_buf );
143 /* new tcp socket with error checking */
144 int netw_tcp_socket( SOCKET *sock );
145 /* Connecting, extended */
146 int netw_connect_ex( NETWORK **netw , char *host , int port, int disable_naggle , int sock_send_buf , int sock_recv_buf , int send_list_limit , int recv_list_limit );
147 /* Connecting */
148 int netw_connect( NETWORK **netw , char *host , int port );
149 /* Closing */
150 int netw_close( NETWORK **netw );
151 /* Stop a NETWORK connection sending and receing thread */
152 int netw_stop_thr_engine( NETWORK *netw );
153 /* Listening network */
154 int netw_make_listening( NETWORK **netw , int PORT , int nbpending );
155 /* Accepting routine extended */
156 NETWORK *netw_accept_from_ex( NETWORK *from ,int disable_naggle , int sock_send_buf , int sock_recv_buf , int send_list_limit , int recv_list_limit );
157 /* Accepting routine */
158 NETWORK *netw_accept_from( NETWORK *from );
159 /* Add a message to send in aimed NETWORK */
160 int netw_add_msg( NETWORK *netw , N_STR *msg );
161 /* Get a message from aimed NETWORK. Instant return to NULL if no MSG */
162 N_STR *netw_get_msg( NETWORK *netw );
163 /* Wait a message from aimed NETWORK. Recheck each 'refresh' usec until 'timeout' usec */
164 N_STR *netw_wait_msg( NETWORK *netw , int refresh , int timeout );
165 /* Create the sending and receiving thread of a NETWORK */
166 int netw_start_thr_engine( NETWORK *netw );
167 /* Thread Sending management function */
168 void *netw_send_func( void *NET );
169 /* Thread Receiving management function */
170 void *netw_recv_func( void *NET );
171 /* Writting to a socket */
172 int send_data( SOCKET s, char *buf, NSTRBYTE n );
173 /* Reading from a socket */
174 int recv_data( SOCKET s, char *buf, NSTRBYTE n );
175
178 #ifdef __cplusplus
179 }
180 #endif
181
182 #endif /*#ifndef N_NETWORK*/
183