Changeset 26e7d6d in mainline for uspace/lib/c/generic/net/socket_client.c
- Timestamp:
- 2011-09-19T16:31:00Z (13 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- a347a11
- Parents:
- 3842a955 (diff), 086290d (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
r3842a955 r26e7d6d 39 39 #include <assert.h> 40 40 #include <async.h> 41 #include <async_obsolete.h>42 41 #include <fibril_synch.h> 43 42 #include <stdint.h> … … 65 64 #define SOCKET_MAX_ACCEPTED_SIZE 0 66 65 67 /** Default timeout for connections in microseconds. */68 #define SOCKET_CONNECT_TIMEOUT (1 * 1000 * 1000)69 70 66 /** 71 67 * Maximum number of random attempts to find a new socket identifier before … … 87 83 /** Socket identifier. */ 88 84 int socket_id; 89 /** Parent module phone. */90 int phone;85 /** Parent module session. */ 86 async_sess_t *sess; 91 87 /** Parent module service. */ 92 88 services_t service; … … 147 143 /** Socket client library global data. */ 148 144 static struct socket_client_globals { 149 /** TCP module phone. */ 150 int tcp_phone; 151 /** UDP module phone. */ 152 int udp_phone; 153 154 // /** The last socket identifier. 155 // */ 156 // int last_id; 145 /** TCP module session. */ 146 async_sess_t *tcp_sess; 147 /** UDP module session. */ 148 async_sess_t *udp_sess; 157 149 158 150 /** Active sockets. */ … … 167 159 fibril_rwlock_t lock; 168 160 } socket_globals = { 169 .tcp_phone = -1, 170 .udp_phone = -1, 171 // .last_id = 0, 161 .tcp_sess = NULL, 162 .udp_sess = NULL, 172 163 .sockets = NULL, 173 164 .lock = FIBRIL_RWLOCK_INITIALIZER(socket_globals.lock) … … 283 274 } 284 275 285 /** Returns the TCP module phone. 286 * 287 * Connects to the TCP module if necessary. 288 * 289 * @return The TCP module phone. 290 * @return Other error codes as defined for the 291 * bind_service_timeout() function. 292 */ 293 static int socket_get_tcp_phone(void) 294 { 295 if (socket_globals.tcp_phone < 0) { 296 socket_globals.tcp_phone = bind_service_timeout(SERVICE_TCP, 297 0, 0, SERVICE_TCP, socket_connection, 298 SOCKET_CONNECT_TIMEOUT); 299 } 300 301 return socket_globals.tcp_phone; 302 } 303 304 /** Returns the UDP module phone. 305 * 306 * Connects to the UDP module if necessary. 307 * 308 * @return The UDP module phone. 309 * @return Other error codes as defined for the 310 * bind_service_timeout() function. 311 */ 312 static int socket_get_udp_phone(void) 313 { 314 if (socket_globals.udp_phone < 0) { 315 socket_globals.udp_phone = bind_service_timeout(SERVICE_UDP, 316 0, 0, SERVICE_UDP, socket_connection, 317 SOCKET_CONNECT_TIMEOUT); 318 } 319 320 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; 321 308 } 322 309 … … 334 321 sockets = socket_get_sockets(); 335 322 count = 0; 336 // socket_id = socket_globals.last_id;337 323 338 324 do { … … 347 333 if (socket_id < INT_MAX) { 348 334 ++socket_id; 349 /* } else if(socket_globals.last_id) { 350 * socket_globals.last_id = 0; 351 * socket_id = 1; 352 */ } else { 335 } else { 353 336 return ELIMIT; 354 337 } 355 338 } 356 339 } while (sockets_find(sockets, socket_id)); 357 358 // last_id = socket_id 340 359 341 return socket_id; 360 342 } … … 363 345 * 364 346 * @param[in,out] socket The socket to be initialized. 365 * @param[in] socket_id The new socket identifier. 366 * @param[in] phone The parent module phone. 367 * @param[in] service The parent module service. 368 */ 369 static void 370 socket_initialize(socket_t *socket, int socket_id, int phone, 371 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) 372 353 { 373 354 socket->socket_id = socket_id; 374 socket-> phone = phone;355 socket->sess = sess; 375 356 socket->service = service; 376 357 dyn_fifo_initialize(&socket->received, SOCKET_INITIAL_RECEIVED_SIZE); … … 397 378 * @return Other error codes as defined for the NET_SOCKET message. 398 379 * @return Other error codes as defined for the 399 * bind_service _timeout() function.380 * bind_service() function. 400 381 */ 401 382 int socket(int domain, int type, int protocol) 402 383 { 403 384 socket_t *socket; 404 int phone;385 async_sess_t *sess; 405 386 int socket_id; 406 387 services_t service; … … 419 400 switch (protocol) { 420 401 case IPPROTO_TCP: 421 phone = socket_get_tcp_phone();402 sess = socket_get_tcp_sess(); 422 403 service = SERVICE_TCP; 423 404 break; … … 434 415 switch (protocol) { 435 416 case IPPROTO_UDP: 436 phone = socket_get_udp_phone();417 sess = socket_get_udp_sess(); 437 418 service = SERVICE_UDP; 438 419 break; … … 455 436 } 456 437 457 if ( phone < 0)458 return phone;438 if (sess == NULL) 439 return ENOENT; 459 440 460 441 /* Create a new socket structure */ … … 473 454 return socket_id; 474 455 } 475 476 rc = (int) async_obsolete_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, 477 459 &fragment_size, &header_size); 460 async_exchange_end(exch); 461 478 462 if (rc != EOK) { 479 463 fibril_rwlock_write_unlock(&socket_globals.lock); … … 486 470 487 471 /* Finish the new socket initialization */ 488 socket_initialize(socket, socket_id, phone, service);472 socket_initialize(socket, socket_id, sess, service); 489 473 /* Store the new socket */ 490 474 rc = sockets_add(socket_get_sockets(), socket_id, socket); … … 495 479 dyn_fifo_destroy(&socket->accepted); 496 480 free(socket); 497 async_obsolete_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, 498 484 service); 485 async_exchange_end(exch); 486 499 487 return rc; 500 488 } … … 540 528 541 529 /* Request the message */ 542 message_id = async_obsolete_send_3(socket->phone, message, 530 async_exch_t *exch = async_exchange_begin(socket->sess); 531 message_id = async_send_3(exch, message, 543 532 (sysarg_t) socket->socket_id, arg2, socket->service, NULL); 544 533 /* Send the address */ 545 async_obsolete_data_write_start(socket->phone, data, datalength); 534 async_data_write_start(exch, data, datalength); 535 async_exchange_end(exch); 546 536 547 537 fibril_rwlock_read_unlock(&socket_globals.lock); … … 600 590 601 591 /* Request listen backlog change */ 602 result = (int) async_obsolete_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, 603 594 (sysarg_t) socket->socket_id, (sysarg_t) backlog, socket->service); 595 async_exchange_end(exch); 604 596 605 597 fibril_rwlock_read_unlock(&socket_globals.lock); … … 671 663 return socket_id; 672 664 } 673 socket_initialize(new_socket, socket_id, socket-> phone,665 socket_initialize(new_socket, socket_id, socket->sess, 674 666 socket->service); 675 667 result = sockets_add(socket_get_sockets(), new_socket->socket_id, … … 683 675 684 676 /* Request accept */ 685 message_id = async_obsolete_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, 686 679 (sysarg_t) socket->socket_id, 0, socket->service, 0, 687 680 new_socket->socket_id, &answer); 688 681 689 682 /* Read address */ 690 async_obsolete_data_read_start(socket->phone, cliaddr, *addrlen); 683 async_data_read_start(exch, cliaddr, *addrlen); 684 async_exchange_end(exch); 685 691 686 fibril_rwlock_write_unlock(&socket_globals.lock); 692 687 async_wait_for(message_id, &ipc_result); … … 782 777 783 778 /* Request close */ 784 rc = (int) async_obsolete_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, 785 781 (sysarg_t) socket->socket_id, 0, socket->service); 782 async_exchange_end(exch); 783 786 784 if (rc != EOK) { 787 785 fibril_rwlock_write_unlock(&socket_globals.lock); … … 855 853 856 854 /* Request send */ 857 message_id = async_obsolete_send_5(socket->phone, message, 855 async_exch_t *exch = async_exchange_begin(socket->sess); 856 857 message_id = async_send_5(exch, message, 858 858 (sysarg_t) socket->socket_id, 859 859 (fragments == 1 ? datalength : socket->data_fragment_size), … … 862 862 /* Send the address if given */ 863 863 if (!toaddr || 864 (async_ obsolete_data_write_start(socket->phone, toaddr, addrlen) == EOK)) {864 (async_data_write_start(exch, toaddr, addrlen) == EOK)) { 865 865 if (fragments == 1) { 866 866 /* Send all if only one fragment */ 867 async_ obsolete_data_write_start(socket->phone, data, datalength);867 async_data_write_start(exch, data, datalength); 868 868 } else { 869 869 /* Send the first fragment */ 870 async_ obsolete_data_write_start(socket->phone, data,870 async_data_write_start(exch, data, 871 871 socket->data_fragment_size - socket->header_size); 872 872 data = ((const uint8_t *) data) + … … 875 875 /* Send the middle fragments */ 876 876 while (--fragments > 1) { 877 async_ obsolete_data_write_start(socket->phone, data,877 async_data_write_start(exch, data, 878 878 socket->data_fragment_size); 879 879 data = ((const uint8_t *) data) + … … 882 882 883 883 /* Send the last fragment */ 884 async_ obsolete_data_write_start(socket->phone, data,884 async_data_write_start(exch, data, 885 885 (datalength + socket->header_size) % 886 886 socket->data_fragment_size); 887 887 } 888 888 } 889 890 async_exchange_end(exch); 889 891 890 892 async_wait_for(message_id, &result); … … 1028 1030 return 0; 1029 1031 } 1032 1033 async_exch_t *exch = async_exchange_begin(socket->sess); 1030 1034 1031 1035 /* Prepare lengths if more fragments */ … … 1040 1044 1041 1045 /* Request packet data */ 1042 message_id = async_ obsolete_send_4(socket->phone, message,1046 message_id = async_send_4(exch, message, 1043 1047 (sysarg_t) socket->socket_id, 0, socket->service, 1044 1048 (sysarg_t) flags, &answer); … … 1046 1050 /* Read the address if desired */ 1047 1051 if(!fromaddr || 1048 (async_ obsolete_data_read_start(socket->phone, fromaddr,1052 (async_data_read_start(exch, fromaddr, 1049 1053 *addrlen) == EOK)) { 1050 1054 /* Read the fragment lengths */ 1051 if (async_ obsolete_data_read_start(socket->phone, lengths,1055 if (async_data_read_start(exch, lengths, 1052 1056 sizeof(int) * (fragments + 1)) == EOK) { 1053 1057 if (lengths[fragments] <= datalength) { … … 1056 1060 for (index = 0; index < fragments; 1057 1061 ++index) { 1058 async_obsolete_data_read_start( 1059 socket->phone, data, 1062 async_data_read_start(exch, data, 1060 1063 lengths[index]); 1061 1064 data = ((uint8_t *) data) + … … 1069 1072 } else { /* fragments == 1 */ 1070 1073 /* Request packet data */ 1071 message_id = async_ obsolete_send_4(socket->phone, message,1074 message_id = async_send_4(exch, message, 1072 1075 (sysarg_t) socket->socket_id, 0, socket->service, 1073 1076 (sysarg_t) flags, &answer); … … 1075 1078 /* Read the address if desired */ 1076 1079 if (!fromaddr || 1077 (async_obsolete_data_read_start(socket->phone, fromaddr, 1078 *addrlen) == EOK)) { 1080 (async_data_read_start(exch, fromaddr, *addrlen) == EOK)) { 1079 1081 /* Read all if only one fragment */ 1080 async_ obsolete_data_read_start(socket->phone, data, datalength);1082 async_data_read_start(exch, data, datalength); 1081 1083 } 1082 1084 } 1085 1086 async_exchange_end(exch); 1083 1087 1084 1088 async_wait_for(message_id, &ipc_result); … … 1192 1196 1193 1197 /* Request option value */ 1194 message_id = async_obsolete_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, 1195 1201 (sysarg_t) socket->socket_id, (sysarg_t) optname, socket->service, 1196 1202 NULL); 1197 1203 1198 1204 /* Read the length */ 1199 if (async_ obsolete_data_read_start(socket->phone, optlen,1205 if (async_data_read_start(exch, optlen, 1200 1206 sizeof(*optlen)) == EOK) { 1201 1207 /* Read the value */ 1202 async_obsolete_data_read_start(socket->phone, value, *optlen); 1203 } 1208 async_data_read_start(exch, value, *optlen); 1209 } 1210 1211 async_exchange_end(exch); 1204 1212 1205 1213 fibril_rwlock_read_unlock(&socket_globals.lock);
Note:
See TracChangeset
for help on using the changeset viewer.