Changeset ede63e4 in mainline for uspace/srv/net/socket/socket_core.c
- Timestamp:
- 2010-01-04T23:25:48Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- eac9722
- Parents:
- 1a0fb3f8
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/socket/socket_core.c
r1a0fb3f8 rede63e4 35 35 */ 36 36 37 #include <limits.h> 38 #include <stdlib.h> 39 37 40 #include "../err.h" 38 41 … … 51 54 52 55 #include "socket_core.h" 56 57 /** Maximum number of random attempts to find a new socket identifier before switching to the sequence. 58 */ 59 #define SOCKET_ID_TRIES 100 53 60 54 61 /** Bound port sockets. … … 94 101 */ 95 102 int socket_port_add_core( socket_port_ref socket_port, socket_core_ref socket, const char * key, size_t key_length ); 103 104 /** Tries to find a new free socket identifier. 105 * @param[in] local_sockets The local sockets to be searched. 106 * @param[in] positive A value indicating whether a positive identifier is requested. A negative identifier is requested if set to false. 107 * @returns The new socket identifier. 108 * @returns ELIMIT if there is no socket identifier available. 109 */ 110 static int socket_generate_new_id( socket_cores_ref local_sockets, int positive ); 96 111 97 112 INT_MAP_IMPLEMENT( socket_cores, socket_core_t ); … … 230 245 } 231 246 247 248 static int socket_generate_new_id( socket_cores_ref local_sockets, int positive ){ 249 int socket_id; 250 int count; 251 252 count = 0; 253 // socket_id = socket_globals.last_id; 254 do{ 255 if( count < SOCKET_ID_TRIES ){ 256 socket_id = rand() % INT_MAX; 257 ++ count; 258 }else if( count == SOCKET_ID_TRIES ){ 259 socket_id = 1; 260 ++ count; 261 // only this branch for last_id 262 }else{ 263 if( socket_id < INT_MAX ){ 264 ++ socket_id; 265 /* }else if( socket_globals.last_id ){ 266 * socket_globals.last_id = 0; 267 * socket_id = 1; 268 */ }else{ 269 return ELIMIT; 270 } 271 } 272 }while( socket_cores_find( local_sockets, (( positive ? 1 : -1 ) * socket_id ))); 273 // last_id = socket_id 274 return socket_id; 275 } 276 232 277 int socket_create( socket_cores_ref local_sockets, int app_phone, void * specific_data, int * socket_id ){ 233 278 ERROR_DECLARE; … … 235 280 socket_core_ref socket; 236 281 int res; 237 238 if( ! socket_id ) return EBADMEM; 282 int positive; 283 284 if( ! socket_id ) return EINVAL; 285 // store the socket 286 if( * socket_id <= 0 ){ 287 positive = ( * socket_id == 0 ); 288 * socket_id = socket_generate_new_id( local_sockets, positive ); 289 if( * socket_id <= 0 ){ 290 return * socket_id; 291 } 292 if( ! positive ){ 293 * socket_id *= -1; 294 } 295 }else if( socket_cores_find( local_sockets, * socket_id )){ 296 return EEXIST; 297 } 239 298 socket = ( socket_core_ref ) malloc( sizeof( * socket )); 240 299 if( ! socket ) return ENOMEM; … … 254 313 return ERROR_CODE; 255 314 } 256 // get a next free socket number 257 socket->socket_id = socket_cores_count( local_sockets ) + 1; 258 // store the socket 315 socket->socket_id = * socket_id; 259 316 res = socket_cores_add( local_sockets, socket->socket_id, socket ); 260 317 if( res < 0 ){ … … 264 321 return res; 265 322 } 266 // return the socket identifier267 * socket_id = socket->socket_id;268 323 return EOK; 269 324 }
Note:
See TracChangeset
for help on using the changeset viewer.