Changeset a33f0a6 in mainline for uspace/lib/c/generic/net/socket_client.c
- Timestamp:
- 2011-08-03T17:34:57Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 1940326
- Parents:
- 52a79081 (diff), 3fab770 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/net/socket_client.c
r52a79081 ra33f0a6 64 64 #define SOCKET_MAX_ACCEPTED_SIZE 0 65 65 66 /** Default timeout for connections in microseconds. */67 #define SOCKET_CONNECT_TIMEOUT (1 * 1000 * 1000)68 69 66 /** 70 67 * Maximum number of random attempts to find a new socket identifier before … … 86 83 /** Socket identifier. */ 87 84 int socket_id; 88 /** Parent module phone. */89 int phone;85 /** Parent module session. */ 86 async_sess_t *sess; 90 87 /** Parent module service. */ 91 88 services_t service; … … 146 143 /** Socket client library global data. */ 147 144 static struct socket_client_globals { 148 /** TCP module phone. */ 149 int tcp_phone; 150 /** UDP module phone. */ 151 int udp_phone; 152 153 // /** The last socket identifier. 154 // */ 155 // int last_id; 145 /** TCP module session. */ 146 async_sess_t *tcp_sess; 147 /** UDP module session. */ 148 async_sess_t *udp_sess; 156 149 157 150 /** Active sockets. */ … … 166 159 fibril_rwlock_t lock; 167 160 } socket_globals = { 168 .tcp_phone = -1, 169 .udp_phone = -1, 170 // .last_id = 0, 161 .tcp_sess = NULL, 162 .udp_sess = NULL, 171 163 .sockets = NULL, 172 164 .lock = FIBRIL_RWLOCK_INITIALIZER(socket_globals.lock) … … 202 194 * @param[in] iid The initial message identifier. 203 195 * @param[in] icall The initial message call structure. 204 */ 205 static void socket_connection(ipc_callid_t iid, ipc_call_t * icall) 196 * @param[in] arg Local argument. 197 */ 198 static void socket_connection(ipc_callid_t iid, ipc_call_t * icall, void *arg) 206 199 { 207 200 ipc_callid_t callid; … … 281 274 } 282 275 283 /** Returns the TCP module phone. 284 * 285 * Connects to the TCP module if necessary. 286 * 287 * @return The TCP module phone. 288 * @return Other error codes as defined for the 289 * bind_service_timeout() function. 290 */ 291 static int socket_get_tcp_phone(void) 292 { 293 if (socket_globals.tcp_phone < 0) { 294 socket_globals.tcp_phone = bind_service_timeout(SERVICE_TCP, 295 0, 0, SERVICE_TCP, socket_connection, 296 SOCKET_CONNECT_TIMEOUT); 297 } 298 299 return socket_globals.tcp_phone; 300 } 301 302 /** Returns the UDP module phone. 303 * 304 * Connects to the UDP module if necessary. 305 * 306 * @return The UDP module phone. 307 * @return Other error codes as defined for the 308 * bind_service_timeout() function. 309 */ 310 static int socket_get_udp_phone(void) 311 { 312 if (socket_globals.udp_phone < 0) { 313 socket_globals.udp_phone = bind_service_timeout(SERVICE_UDP, 314 0, 0, SERVICE_UDP, socket_connection, 315 SOCKET_CONNECT_TIMEOUT); 316 } 317 318 return socket_globals.udp_phone; 276 /** Return the TCP module session. 277 * 278 * Connect to the TCP module if necessary. 279 * 280 * @return The TCP module session. 281 * 282 */ 283 static async_sess_t *socket_get_tcp_sess(void) 284 { 285 if (socket_globals.tcp_sess == NULL) { 286 socket_globals.tcp_sess = bind_service(SERVICE_TCP, 287 0, 0, SERVICE_TCP, socket_connection); 288 } 289 290 return socket_globals.tcp_sess; 291 } 292 293 /** Return the UDP module session. 294 * 295 * Connect to the UDP module if necessary. 296 * 297 * @return The UDP module session. 298 * 299 */ 300 static async_sess_t *socket_get_udp_sess(void) 301 { 302 if (socket_globals.udp_sess == NULL) { 303 socket_globals.udp_sess = bind_service(SERVICE_UDP, 304 0, 0, SERVICE_UDP, socket_connection); 305 } 306 307 return socket_globals.udp_sess; 319 308 } 320 309 … … 332 321 sockets = socket_get_sockets(); 333 322 count = 0; 334 // socket_id = socket_globals.last_id;335 323 336 324 do { … … 345 333 if (socket_id < INT_MAX) { 346 334 ++socket_id; 347 /* } else if(socket_globals.last_id) { 348 * socket_globals.last_id = 0; 349 * socket_id = 1; 350 */ } else { 335 } else { 351 336 return ELIMIT; 352 337 } 353 338 } 354 339 } while (sockets_find(sockets, socket_id)); 355 356 // last_id = socket_id 340 357 341 return socket_id; 358 342 } … … 361 345 * 362 346 * @param[in,out] socket The socket to be initialized. 363 * @param[in] socket_id The new socket identifier. 364 * @param[in] phone The parent module phone. 365 * @param[in] service The parent module service. 366 */ 367 static void 368 socket_initialize(socket_t *socket, int socket_id, int phone, 369 services_t service) 347 * @param[in] socket_id The new socket identifier. 348 * @param[in] sess The parent module session. 349 * @param[in] service The parent module service. 350 */ 351 static void socket_initialize(socket_t *socket, int socket_id, 352 async_sess_t *sess, services_t service) 370 353 { 371 354 socket->socket_id = socket_id; 372 socket-> phone = phone;355 socket->sess = sess; 373 356 socket->service = service; 374 357 dyn_fifo_initialize(&socket->received, SOCKET_INITIAL_RECEIVED_SIZE); … … 395 378 * @return Other error codes as defined for the NET_SOCKET message. 396 379 * @return Other error codes as defined for the 397 * bind_service _timeout() function.380 * bind_service() function. 398 381 */ 399 382 int socket(int domain, int type, int protocol) 400 383 { 401 384 socket_t *socket; 402 int phone;385 async_sess_t *sess; 403 386 int socket_id; 404 387 services_t service; … … 417 400 switch (protocol) { 418 401 case IPPROTO_TCP: 419 phone = socket_get_tcp_phone();402 sess = socket_get_tcp_sess(); 420 403 service = SERVICE_TCP; 421 404 break; … … 432 415 switch (protocol) { 433 416 case IPPROTO_UDP: 434 phone = socket_get_udp_phone();417 sess = socket_get_udp_sess(); 435 418 service = SERVICE_UDP; 436 419 break; … … 453 436 } 454 437 455 if ( phone < 0)456 return phone;438 if (sess == NULL) 439 return ENOENT; 457 440 458 441 /* Create a new socket structure */ … … 471 454 return socket_id; 472 455 } 473 474 rc = (int) async_req_3_3(phone, NET_SOCKET, socket_id, 0, service, NULL, 456 457 async_exch_t *exch = async_exchange_begin(sess); 458 rc = (int) async_req_3_3(exch, NET_SOCKET, socket_id, 0, service, NULL, 475 459 &fragment_size, &header_size); 460 async_exchange_end(exch); 461 476 462 if (rc != EOK) { 477 463 fibril_rwlock_write_unlock(&socket_globals.lock); … … 484 470 485 471 /* Finish the new socket initialization */ 486 socket_initialize(socket, socket_id, phone, service);472 socket_initialize(socket, socket_id, sess, service); 487 473 /* Store the new socket */ 488 474 rc = sockets_add(socket_get_sockets(), socket_id, socket); … … 493 479 dyn_fifo_destroy(&socket->accepted); 494 480 free(socket); 495 async_msg_3(phone, NET_SOCKET_CLOSE, (sysarg_t) socket_id, 0, 481 482 exch = async_exchange_begin(sess); 483 async_msg_3(exch, NET_SOCKET_CLOSE, (sysarg_t) socket_id, 0, 496 484 service); 485 async_exchange_end(exch); 486 497 487 return rc; 498 488 } … … 538 528 539 529 /* Request the message */ 540 message_id = async_send_3(socket->phone, message, 530 async_exch_t *exch = async_exchange_begin(socket->sess); 531 message_id = async_send_3(exch, message, 541 532 (sysarg_t) socket->socket_id, arg2, socket->service, NULL); 542 533 /* Send the address */ 543 async_data_write_start(socket->phone, data, datalength); 534 async_data_write_start(exch, data, datalength); 535 async_exchange_end(exch); 544 536 545 537 fibril_rwlock_read_unlock(&socket_globals.lock); … … 598 590 599 591 /* Request listen backlog change */ 600 result = (int) async_req_3_0(socket->phone, NET_SOCKET_LISTEN, 592 async_exch_t *exch = async_exchange_begin(socket->sess); 593 result = (int) async_req_3_0(exch, NET_SOCKET_LISTEN, 601 594 (sysarg_t) socket->socket_id, (sysarg_t) backlog, socket->service); 595 async_exchange_end(exch); 602 596 603 597 fibril_rwlock_read_unlock(&socket_globals.lock); … … 669 663 return socket_id; 670 664 } 671 socket_initialize(new_socket, socket_id, socket-> phone,665 socket_initialize(new_socket, socket_id, socket->sess, 672 666 socket->service); 673 667 result = sockets_add(socket_get_sockets(), new_socket->socket_id, … … 681 675 682 676 /* Request accept */ 683 message_id = async_send_5(socket->phone, NET_SOCKET_ACCEPT, 677 async_exch_t *exch = async_exchange_begin(socket->sess); 678 message_id = async_send_5(exch, NET_SOCKET_ACCEPT, 684 679 (sysarg_t) socket->socket_id, 0, socket->service, 0, 685 680 new_socket->socket_id, &answer); 686 681 687 682 /* Read address */ 688 async_data_read_start(socket->phone, cliaddr, *addrlen); 683 async_data_read_start(exch, cliaddr, *addrlen); 684 async_exchange_end(exch); 685 689 686 fibril_rwlock_write_unlock(&socket_globals.lock); 690 687 async_wait_for(message_id, &ipc_result); … … 780 777 781 778 /* Request close */ 782 rc = (int) async_req_3_0(socket->phone, NET_SOCKET_CLOSE, 779 async_exch_t *exch = async_exchange_begin(socket->sess); 780 rc = (int) async_req_3_0(exch, NET_SOCKET_CLOSE, 783 781 (sysarg_t) socket->socket_id, 0, socket->service); 782 async_exchange_end(exch); 783 784 784 if (rc != EOK) { 785 785 fibril_rwlock_write_unlock(&socket_globals.lock); … … 853 853 854 854 /* Request send */ 855 message_id = async_send_5(socket->phone, message, 855 async_exch_t *exch = async_exchange_begin(socket->sess); 856 857 message_id = async_send_5(exch, message, 856 858 (sysarg_t) socket->socket_id, 857 859 (fragments == 1 ? datalength : socket->data_fragment_size), … … 860 862 /* Send the address if given */ 861 863 if (!toaddr || 862 (async_data_write_start( socket->phone, toaddr, addrlen) == EOK)) {864 (async_data_write_start(exch, toaddr, addrlen) == EOK)) { 863 865 if (fragments == 1) { 864 866 /* Send all if only one fragment */ 865 async_data_write_start( socket->phone, data, datalength);867 async_data_write_start(exch, data, datalength); 866 868 } else { 867 869 /* Send the first fragment */ 868 async_data_write_start( socket->phone, data,870 async_data_write_start(exch, data, 869 871 socket->data_fragment_size - socket->header_size); 870 872 data = ((const uint8_t *) data) + … … 873 875 /* Send the middle fragments */ 874 876 while (--fragments > 1) { 875 async_data_write_start( socket->phone, data,877 async_data_write_start(exch, data, 876 878 socket->data_fragment_size); 877 879 data = ((const uint8_t *) data) + … … 880 882 881 883 /* Send the last fragment */ 882 async_data_write_start( socket->phone, data,884 async_data_write_start(exch, data, 883 885 (datalength + socket->header_size) % 884 886 socket->data_fragment_size); 885 887 } 886 888 } 889 890 async_exchange_end(exch); 887 891 888 892 async_wait_for(message_id, &result); … … 1026 1030 return 0; 1027 1031 } 1032 1033 async_exch_t *exch = async_exchange_begin(socket->sess); 1028 1034 1029 1035 /* Prepare lengths if more fragments */ … … 1038 1044 1039 1045 /* Request packet data */ 1040 message_id = async_send_4( socket->phone, message,1046 message_id = async_send_4(exch, message, 1041 1047 (sysarg_t) socket->socket_id, 0, socket->service, 1042 1048 (sysarg_t) flags, &answer); … … 1044 1050 /* Read the address if desired */ 1045 1051 if(!fromaddr || 1046 (async_data_read_start( socket->phone, fromaddr,1052 (async_data_read_start(exch, fromaddr, 1047 1053 *addrlen) == EOK)) { 1048 1054 /* Read the fragment lengths */ 1049 if (async_data_read_start( socket->phone, lengths,1055 if (async_data_read_start(exch, lengths, 1050 1056 sizeof(int) * (fragments + 1)) == EOK) { 1051 1057 if (lengths[fragments] <= datalength) { … … 1054 1060 for (index = 0; index < fragments; 1055 1061 ++index) { 1056 async_data_read_start( 1057 socket->phone, data, 1062 async_data_read_start(exch, data, 1058 1063 lengths[index]); 1059 1064 data = ((uint8_t *) data) + … … 1067 1072 } else { /* fragments == 1 */ 1068 1073 /* Request packet data */ 1069 message_id = async_send_4( socket->phone, message,1074 message_id = async_send_4(exch, message, 1070 1075 (sysarg_t) socket->socket_id, 0, socket->service, 1071 1076 (sysarg_t) flags, &answer); … … 1073 1078 /* Read the address if desired */ 1074 1079 if (!fromaddr || 1075 (async_data_read_start(socket->phone, fromaddr, 1076 *addrlen) == EOK)) { 1080 (async_data_read_start(exch, fromaddr, *addrlen) == EOK)) { 1077 1081 /* Read all if only one fragment */ 1078 async_data_read_start( socket->phone, data, datalength);1082 async_data_read_start(exch, data, datalength); 1079 1083 } 1080 1084 } 1085 1086 async_exchange_end(exch); 1081 1087 1082 1088 async_wait_for(message_id, &ipc_result); … … 1190 1196 1191 1197 /* Request option value */ 1192 message_id = async_send_3(socket->phone, NET_SOCKET_GETSOCKOPT, 1198 async_exch_t *exch = async_exchange_begin(socket->sess); 1199 1200 message_id = async_send_3(exch, NET_SOCKET_GETSOCKOPT, 1193 1201 (sysarg_t) socket->socket_id, (sysarg_t) optname, socket->service, 1194 1202 NULL); 1195 1203 1196 1204 /* Read the length */ 1197 if (async_data_read_start( socket->phone, optlen,1205 if (async_data_read_start(exch, optlen, 1198 1206 sizeof(*optlen)) == EOK) { 1199 1207 /* Read the value */ 1200 async_data_read_start(socket->phone, value, *optlen); 1201 } 1208 async_data_read_start(exch, value, *optlen); 1209 } 1210 1211 async_exchange_end(exch); 1202 1212 1203 1213 fibril_rwlock_read_unlock(&socket_globals.lock);
Note:
See TracChangeset
for help on using the changeset viewer.