Rev 4578 | Rev 4589 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4578 | Rev 4582 | ||
---|---|---|---|
Line 36... | Line 36... | ||
36 | * This is a part of the network application library. |
36 | * This is a part of the network application library. |
37 | */ |
37 | */ |
38 | 38 | ||
39 | #include <assert.h> |
39 | #include <assert.h> |
40 | #include <async.h> |
40 | #include <async.h> |
41 | //#include <fibril_sync.h> |
41 | #include <fibril_sync.h> |
42 | 42 | ||
43 | #include <ipc/services.h> |
43 | #include <ipc/services.h> |
44 | 44 | ||
45 | #include "../err.h" |
45 | #include "../err.h" |
46 | #include "../modules.h" |
46 | #include "../modules.h" |
Line 60... | Line 60... | ||
60 | int socket_id; |
60 | int socket_id; |
61 | int phone; |
61 | int phone; |
62 | services_t service; |
62 | services_t service; |
63 | int max_content; |
63 | int max_content; |
64 | int received; |
64 | int received; |
65 | // fibril_mutex_t receive_lock; |
65 | fibril_mutex_t receive_lock; |
66 | // fibril_condvar_t receive_signal; |
66 | fibril_condvar_t receive_signal; |
67 | int accepted; |
67 | int accepted; |
68 | // fibril_mutex_t accept_lock; |
68 | fibril_mutex_t accept_lock; |
69 | // fibril_condvar_t accept_signal; |
69 | fibril_condvar_t accept_signal; |
70 | }; |
70 | }; |
71 | 71 | ||
72 | INT_MAP_DECLARE( sockets, socket_t ); |
72 | INT_MAP_DECLARE( sockets, socket_t ); |
73 | 73 | ||
74 | static struct{ |
74 | static struct{ |
Line 116... | Line 116... | ||
116 | socket->socket_id = socket_id; |
116 | socket->socket_id = socket_id; |
117 | socket->phone = phone; |
117 | socket->phone = phone; |
118 | socket->service = service; |
118 | socket->service = service; |
119 | socket->received = 0; |
119 | socket->received = 0; |
120 | socket->accepted = 0; |
120 | socket->accepted = 0; |
121 | // fibril_mutex_initialize( & socket->receive_lock ); |
121 | fibril_mutex_initialize( & socket->receive_lock ); |
122 | // fibril_condvar_initialize( & socket->receive_signal ); |
122 | fibril_condvar_initialize( & socket->receive_signal ); |
123 | // fibril_mutex_initialize( & socket->accept_lock ); |
123 | fibril_mutex_initialize( & socket->accept_lock ); |
124 | // fibril_condvar_initialize( & socket->accept_signal ); |
124 | fibril_condvar_initialize( & socket->accept_signal ); |
125 | } |
125 | } |
126 | 126 | ||
127 | void socket_connection( ipc_callid_t iid, ipc_call_t * icall ){ |
127 | void socket_connection( ipc_callid_t iid, ipc_call_t * icall ){ |
128 | ERROR_DECLARE; |
128 | ERROR_DECLARE; |
129 | 129 | ||
Line 140... | Line 140... | ||
140 | socket = sockets_find( socket_get_sockets(), SOCKET_GET_SOCKET_ID( & call )); |
140 | socket = sockets_find( socket_get_sockets(), SOCKET_GET_SOCKET_ID( & call )); |
141 | if( ! socket ){ |
141 | if( ! socket ){ |
142 | ERROR_CODE = ENOTSOCK; |
142 | ERROR_CODE = ENOTSOCK; |
143 | break; |
143 | break; |
144 | } |
144 | } |
145 | // fibril_mutex_lock( & socket->receive_lock ); |
145 | fibril_mutex_lock( & socket->receive_lock ); |
146 | ++ socket->received; |
146 | ++ socket->received; |
147 | // fibril_condvar_signal( & socket->receive_signal ); |
147 | fibril_condvar_signal( & socket->receive_signal ); |
148 | // fibril_mutex_unlock( & socket->receive_lock ); |
148 | fibril_mutex_unlock( & socket->receive_lock ); |
149 | ERROR_CODE = EOK; |
149 | ERROR_CODE = EOK; |
150 | break; |
150 | break; |
151 | case NET_SOCKET_ACCEPTED: |
151 | case NET_SOCKET_ACCEPTED: |
152 | socket = sockets_find( socket_get_sockets(), SOCKET_GET_SOCKET_ID( & call )); |
152 | socket = sockets_find( socket_get_sockets(), SOCKET_GET_SOCKET_ID( & call )); |
153 | if( ! socket ){ |
153 | if( ! socket ){ |
154 | ERROR_CODE = ENOTSOCK; |
154 | ERROR_CODE = ENOTSOCK; |
155 | break; |
155 | break; |
156 | } |
156 | } |
157 | // fibril_mutex_lock( & socket->accept_lock ); |
157 | fibril_mutex_lock( & socket->accept_lock ); |
158 | new_socket = ( socket_ref ) malloc( sizeof( socket_t )); |
158 | new_socket = ( socket_ref ) malloc( sizeof( socket_t )); |
159 | if( ! new_socket ){ |
159 | if( ! new_socket ){ |
160 | ERROR_CODE = ENOMEM; |
160 | ERROR_CODE = ENOMEM; |
161 | break; |
161 | break; |
162 | } |
162 | } |
163 | socket_initialize( new_socket, SOCKET_GET_NEW_SOCKET_ID( & call ), socket->phone, socket->service ); |
163 | socket_initialize( new_socket, SOCKET_GET_NEW_SOCKET_ID( & call ), socket->phone, socket->service ); |
164 | if( ERROR_OCCURRED( sockets_add( socket_get_sockets(), new_socket->socket_id, new_socket ))){ |
164 | if( ERROR_OCCURRED( sockets_add( socket_get_sockets(), new_socket->socket_id, new_socket ))){ |
165 | break; |
165 | break; |
166 | } |
166 | } |
167 | ++ socket->accepted; |
167 | ++ socket->accepted; |
168 | // fibril_condvar_signal( & socket->accept_signal ); |
168 | fibril_condvar_signal( & socket->accept_signal ); |
169 | // fibril_mutex_unlock( & socket->accept_lock ); |
169 | fibril_mutex_unlock( & socket->accept_lock ); |
170 | break; |
170 | break; |
171 | /* case NET_SOCKET_MTU: |
171 | /* case NET_SOCKET_MTU: |
172 | socket = sockets_find( socket_get_sockets(), SOCKET_GET_SOCKET_ID( & call )); |
172 | socket = sockets_find( socket_get_sockets(), SOCKET_GET_SOCKET_ID( & call )); |
173 | if( ! socket ){ |
173 | if( ! socket ){ |
174 | ERROR_CODE = ENOTSOCK; |
174 | ERROR_CODE = ENOTSOCK; |
Line 275... | Line 275... | ||
275 | 275 | ||
276 | if( ! cliaddr ) return EBADMEM; |
276 | if( ! cliaddr ) return EBADMEM; |
277 | if( ! addrlen ) return NO_DATA; |
277 | if( ! addrlen ) return NO_DATA; |
278 | socket = sockets_find( socket_get_sockets(), socket_id ); |
278 | socket = sockets_find( socket_get_sockets(), socket_id ); |
279 | if( ! socket ) return ENOTSOCK; |
279 | if( ! socket ) return ENOTSOCK; |
280 | // fibril_mutex_lock( & socket->accept_lock ); |
280 | fibril_mutex_lock( & socket->accept_lock ); |
281 | while( socket->accepted <= 0 ){ |
281 | while( socket->accepted <= 0 ){ |
282 | // fibril_condvar_wait( & socket->accept_signal, & socket->accept_lock ); |
282 | fibril_condvar_wait( & socket->accept_signal, & socket->accept_lock ); |
283 | } |
283 | } |
284 | message_id = async_send_3( socket->phone, NET_SOCKET_ACCEPT, socket->socket_id, 0, socket->service, NULL ); |
284 | message_id = async_send_3( socket->phone, NET_SOCKET_ACCEPT, socket->socket_id, 0, socket->service, NULL ); |
285 | ipc_data_read_start( socket->phone, cliaddr, * addrlen ); |
285 | ipc_data_read_start( socket->phone, cliaddr, * addrlen ); |
286 | async_wait_for( message_id, & result ); |
286 | async_wait_for( message_id, & result ); |
287 | if( result > 0 ){ |
287 | if( result > 0 ){ |
288 | -- socket->accepted; |
288 | -- socket->accepted; |
289 | } |
289 | } |
290 | // fibril_mutex_unlock( & socket->accept_lock ); |
290 | fibril_mutex_unlock( & socket->accept_lock ); |
291 | return ( int ) result; |
291 | return ( int ) result; |
292 | } |
292 | } |
293 | 293 | ||
294 | int connect( int socket_id, const struct sockaddr * serv_addr, socklen_t addrlen ){ |
294 | int connect( int socket_id, const struct sockaddr * serv_addr, socklen_t addrlen ){ |
295 | return socket_send_data( socket_id, NET_SOCKET_CONNECT, 0, serv_addr, addrlen ); |
295 | return socket_send_data( socket_id, NET_SOCKET_CONNECT, 0, serv_addr, addrlen ); |
Line 333... | Line 333... | ||
333 | 333 | ||
334 | if( ! data ) return EBADMEM; |
334 | if( ! data ) return EBADMEM; |
335 | if( ! datalength ) return NO_DATA; |
335 | if( ! datalength ) return NO_DATA; |
336 | socket = sockets_find( socket_get_sockets(), socket_id ); |
336 | socket = sockets_find( socket_get_sockets(), socket_id ); |
337 | if( ! socket ) return ENOTSOCK; |
337 | if( ! socket ) return ENOTSOCK; |
338 | // fibril_mutex_lock( & socket->receive_lock ); |
338 | fibril_mutex_lock( & socket->receive_lock ); |
339 | while( socket->received <= 0 ){ |
339 | while( socket->received <= 0 ){ |
340 | // fibril_condvar_wait( & socket->receive_signal, & socket->receive_lock ); |
340 | fibril_condvar_wait( & socket->receive_signal, & socket->receive_lock ); |
341 | } |
341 | } |
342 | message_id = async_send_3( socket->phone, NET_SOCKET_RECV, socket->socket_id, flags, socket->service, NULL ); |
342 | message_id = async_send_3( socket->phone, NET_SOCKET_RECV, socket->socket_id, flags, socket->service, NULL ); |
343 | ipc_data_read_start( socket->phone, data, datalength ); |
343 | ipc_data_read_start( socket->phone, data, datalength ); |
344 | async_wait_for( message_id, & result ); |
344 | async_wait_for( message_id, & result ); |
345 | if( result > 0 ){ |
345 | if( result > 0 ){ |
346 | -- socket->received; |
346 | -- socket->received; |
347 | } |
347 | } |
348 | // fibril_mutex_unlock( & socket->receive_lock ); |
348 | fibril_mutex_unlock( & socket->receive_lock ); |
349 | return ( int ) result; |
349 | return ( int ) result; |
350 | } |
350 | } |
351 | 351 | ||
352 | int recvfrom( int socket_id, void * data, size_t datalength, int flags, struct sockaddr * fromaddr, socklen_t * addrlen ){ |
352 | int recvfrom( int socket_id, void * data, size_t datalength, int flags, struct sockaddr * fromaddr, socklen_t * addrlen ){ |
353 | socket_ref socket; |
353 | socket_ref socket; |
Line 358... | Line 358... | ||
358 | if( ! addrlen ) return NO_DATA; |
358 | if( ! addrlen ) return NO_DATA; |
359 | if( ! data ) return EBADMEM; |
359 | if( ! data ) return EBADMEM; |
360 | if( ! datalength ) return NO_DATA; |
360 | if( ! datalength ) return NO_DATA; |
361 | socket = sockets_find( socket_get_sockets(), socket_id ); |
361 | socket = sockets_find( socket_get_sockets(), socket_id ); |
362 | if( ! socket ) return ENOTSOCK; |
362 | if( ! socket ) return ENOTSOCK; |
363 | // fibril_mutex_lock( & socket->receive_lock ); |
363 | fibril_mutex_lock( & socket->receive_lock ); |
364 | while( socket->received <= 0 ){ |
364 | while( socket->received <= 0 ){ |
365 | // fibril_condvar_wait( & socket->receive_signal, & socket->receive_lock ); |
365 | fibril_condvar_wait( & socket->receive_signal, & socket->receive_lock ); |
366 | } |
366 | } |
367 | message_id = async_send_3( socket->phone, NET_SOCKET_RECVFROM, socket->socket_id, flags, socket->service, NULL ); |
367 | message_id = async_send_3( socket->phone, NET_SOCKET_RECVFROM, socket->socket_id, flags, socket->service, NULL ); |
368 | if( ipc_data_read_start( socket->phone, fromaddr, * addrlen ) == EOK ){ |
368 | if( ipc_data_read_start( socket->phone, fromaddr, * addrlen ) == EOK ){ |
369 | ipc_data_read_start( socket->phone, data, datalength ); |
369 | ipc_data_read_start( socket->phone, data, datalength ); |
370 | } |
370 | } |
371 | async_wait_for( message_id, & result ); |
371 | async_wait_for( message_id, & result ); |
372 | if( result > 0 ){ |
372 | if( result > 0 ){ |
373 | -- socket->received; |
373 | -- socket->received; |
374 | } |
374 | } |
375 | // fibril_mutex_unlock( & socket->receive_lock ); |
375 | fibril_mutex_unlock( & socket->receive_lock ); |
376 | return ( int ) result; |
376 | return ( int ) result; |
377 | } |
377 | } |
378 | 378 | ||
379 | int getsockopt( int socket_id, int level, int optname, void * value, size_t * optlen ){ |
379 | int getsockopt( int socket_id, int level, int optname, void * value, size_t * optlen ){ |
380 | socket_ref socket; |
380 | socket_ref socket; |