Changeset 8ff0bd2 in mainline for uspace/lib/c/generic/net/socket_client.c
- Timestamp:
- 2011-09-04T11:30:58Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 03bc76a
- Parents:
- d2c67e7 (diff), deac215e (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
-
uspace/lib/c/generic/net/socket_client.c (modified) (33 diffs)
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/net/socket_client.c
rd2c67e7 r8ff0bd2 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) … … 203 194 * @param[in] iid The initial message identifier. 204 195 * @param[in] icall The initial message call structure. 205 */ 206 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) 207 199 { 208 200 ipc_callid_t callid; … … 282 274 } 283 275 284 /** Returns the TCP module phone. 285 * 286 * Connects to the TCP module if necessary. 287 * 288 * @return The TCP module phone. 289 * @return Other error codes as defined for the 290 * bind_service_timeout() function. 291 */ 292 static int socket_get_tcp_phone(void) 293 { 294 if (socket_globals.tcp_phone < 0) { 295 socket_globals.tcp_phone = bind_service_timeout(SERVICE_TCP, 296 0, 0, SERVICE_TCP, socket_connection, 297 SOCKET_CONNECT_TIMEOUT); 298 } 299 300 return socket_globals.tcp_phone; 301 } 302 303 /** Returns the UDP module phone. 304 * 305 * Connects to the UDP module if necessary. 306 * 307 * @return The UDP module phone. 308 * @return Other error codes as defined for the 309 * bind_service_timeout() function. 310 */ 311 static int socket_get_udp_phone(void) 312 { 313 if (socket_globals.udp_phone < 0) { 314 socket_globals.udp_phone = bind_service_timeout(SERVICE_UDP, 315 0, 0, SERVICE_UDP, socket_connection, 316 SOCKET_CONNECT_TIMEOUT); 317 } 318 319 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; 320 308 } 321 309 … … 333 321 sockets = socket_get_sockets(); 334 322 count = 0; 335 // socket_id = socket_globals.last_id;336 323 337 324 do { … … 346 333 if (socket_id < INT_MAX) { 347 334 ++socket_id; 348 /* } else if(socket_globals.last_id) { 349 * socket_globals.last_id = 0; 350 * socket_id = 1; 351 */ } else { 335 } else { 352 336 return ELIMIT; 353 337 } 354 338 } 355 339 } while (sockets_find(sockets, socket_id)); 356 357 // last_id = socket_id 340 358 341 return socket_id; 359 342 } … … 362 345 * 363 346 * @param[in,out] socket The socket to be initialized. 364 * @param[in] socket_id The new socket identifier. 365 * @param[in] phone The parent module phone. 366 * @param[in] service The parent module service. 367 */ 368 static void 369 socket_initialize(socket_t *socket, int socket_id, int phone, 370 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) 371 353 { 372 354 socket->socket_id = socket_id; 373 socket-> phone = phone;355 socket->sess = sess; 374 356 socket->service = service; 375 357 dyn_fifo_initialize(&socket->received, SOCKET_INITIAL_RECEIVED_SIZE); … … 396 378 * @return Other error codes as defined for the NET_SOCKET message. 397 379 * @return Other error codes as defined for the 398 * bind_service _timeout() function.380 * bind_service() function. 399 381 */ 400 382 int socket(int domain, int type, int protocol) 401 383 { 402 384 socket_t *socket; 403 int phone;385 async_sess_t *sess; 404 386 int socket_id; 405 387 services_t service; … … 418 400 switch (protocol) { 419 401 case IPPROTO_TCP: 420 phone = socket_get_tcp_phone();402 sess = socket_get_tcp_sess(); 421 403 service = SERVICE_TCP; 422 404 break; … … 433 415 switch (protocol) { 434 416 case IPPROTO_UDP: 435 phone = socket_get_udp_phone();417 sess = socket_get_udp_sess(); 436 418 service = SERVICE_UDP; 437 419 break; … … 454 436 } 455 437 456 if ( phone < 0)457 return phone;438 if (sess == NULL) 439 return ENOENT; 458 440 459 441 /* Create a new socket structure */ … … 472 454 return socket_id; 473 455 } 474 475 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, 476 459 &fragment_size, &header_size); 460 async_exchange_end(exch); 461 477 462 if (rc != EOK) { 478 463 fibril_rwlock_write_unlock(&socket_globals.lock); … … 485 470 486 471 /* Finish the new socket initialization */ 487 socket_initialize(socket, socket_id, phone, service);472 socket_initialize(socket, socket_id, sess, service); 488 473 /* Store the new socket */ 489 474 rc = sockets_add(socket_get_sockets(), socket_id, socket); … … 494 479 dyn_fifo_destroy(&socket->accepted); 495 480 free(socket); 496 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, 497 484 service); 485 async_exchange_end(exch); 486 498 487 return rc; 499 488 } … … 539 528 540 529 /* Request the message */ 541 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, 542 532 (sysarg_t) socket->socket_id, arg2, socket->service, NULL); 543 533 /* Send the address */ 544 async_obsolete_data_write_start(socket->phone, data, datalength); 534 async_data_write_start(exch, data, datalength); 535 async_exchange_end(exch); 545 536 546 537 fibril_rwlock_read_unlock(&socket_globals.lock); … … 599 590 600 591 /* Request listen backlog change */ 601 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, 602 594 (sysarg_t) socket->socket_id, (sysarg_t) backlog, socket->service); 595 async_exchange_end(exch); 603 596 604 597 fibril_rwlock_read_unlock(&socket_globals.lock); … … 670 663 return socket_id; 671 664 } 672 socket_initialize(new_socket, socket_id, socket-> phone,665 socket_initialize(new_socket, socket_id, socket->sess, 673 666 socket->service); 674 667 result = sockets_add(socket_get_sockets(), new_socket->socket_id, … … 682 675 683 676 /* Request accept */ 684 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, 685 679 (sysarg_t) socket->socket_id, 0, socket->service, 0, 686 680 new_socket->socket_id, &answer); 687 681 688 682 /* Read address */ 689 async_obsolete_data_read_start(socket->phone, cliaddr, *addrlen); 683 async_data_read_start(exch, cliaddr, *addrlen); 684 async_exchange_end(exch); 685 690 686 fibril_rwlock_write_unlock(&socket_globals.lock); 691 687 async_wait_for(message_id, &ipc_result); … … 781 777 782 778 /* Request close */ 783 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, 784 781 (sysarg_t) socket->socket_id, 0, socket->service); 782 async_exchange_end(exch); 783 785 784 if (rc != EOK) { 786 785 fibril_rwlock_write_unlock(&socket_globals.lock); … … 854 853 855 854 /* Request send */ 856 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, 857 858 (sysarg_t) socket->socket_id, 858 859 (fragments == 1 ? datalength : socket->data_fragment_size), … … 861 862 /* Send the address if given */ 862 863 if (!toaddr || 863 (async_ obsolete_data_write_start(socket->phone, toaddr, addrlen) == EOK)) {864 (async_data_write_start(exch, toaddr, addrlen) == EOK)) { 864 865 if (fragments == 1) { 865 866 /* Send all if only one fragment */ 866 async_ obsolete_data_write_start(socket->phone, data, datalength);867 async_data_write_start(exch, data, datalength); 867 868 } else { 868 869 /* Send the first fragment */ 869 async_ obsolete_data_write_start(socket->phone, data,870 async_data_write_start(exch, data, 870 871 socket->data_fragment_size - socket->header_size); 871 872 data = ((const uint8_t *) data) + … … 874 875 /* Send the middle fragments */ 875 876 while (--fragments > 1) { 876 async_ obsolete_data_write_start(socket->phone, data,877 async_data_write_start(exch, data, 877 878 socket->data_fragment_size); 878 879 data = ((const uint8_t *) data) + … … 881 882 882 883 /* Send the last fragment */ 883 async_ obsolete_data_write_start(socket->phone, data,884 async_data_write_start(exch, data, 884 885 (datalength + socket->header_size) % 885 886 socket->data_fragment_size); 886 887 } 887 888 } 889 890 async_exchange_end(exch); 888 891 889 892 async_wait_for(message_id, &result); … … 1027 1030 return 0; 1028 1031 } 1032 1033 async_exch_t *exch = async_exchange_begin(socket->sess); 1029 1034 1030 1035 /* Prepare lengths if more fragments */ … … 1039 1044 1040 1045 /* Request packet data */ 1041 message_id = async_ obsolete_send_4(socket->phone, message,1046 message_id = async_send_4(exch, message, 1042 1047 (sysarg_t) socket->socket_id, 0, socket->service, 1043 1048 (sysarg_t) flags, &answer); … … 1045 1050 /* Read the address if desired */ 1046 1051 if(!fromaddr || 1047 (async_ obsolete_data_read_start(socket->phone, fromaddr,1052 (async_data_read_start(exch, fromaddr, 1048 1053 *addrlen) == EOK)) { 1049 1054 /* Read the fragment lengths */ 1050 if (async_ obsolete_data_read_start(socket->phone, lengths,1055 if (async_data_read_start(exch, lengths, 1051 1056 sizeof(int) * (fragments + 1)) == EOK) { 1052 1057 if (lengths[fragments] <= datalength) { … … 1055 1060 for (index = 0; index < fragments; 1056 1061 ++index) { 1057 async_obsolete_data_read_start( 1058 socket->phone, data, 1062 async_data_read_start(exch, data, 1059 1063 lengths[index]); 1060 1064 data = ((uint8_t *) data) + … … 1068 1072 } else { /* fragments == 1 */ 1069 1073 /* Request packet data */ 1070 message_id = async_ obsolete_send_4(socket->phone, message,1074 message_id = async_send_4(exch, message, 1071 1075 (sysarg_t) socket->socket_id, 0, socket->service, 1072 1076 (sysarg_t) flags, &answer); … … 1074 1078 /* Read the address if desired */ 1075 1079 if (!fromaddr || 1076 (async_obsolete_data_read_start(socket->phone, fromaddr, 1077 *addrlen) == EOK)) { 1080 (async_data_read_start(exch, fromaddr, *addrlen) == EOK)) { 1078 1081 /* Read all if only one fragment */ 1079 async_ obsolete_data_read_start(socket->phone, data, datalength);1082 async_data_read_start(exch, data, datalength); 1080 1083 } 1081 1084 } 1085 1086 async_exchange_end(exch); 1082 1087 1083 1088 async_wait_for(message_id, &ipc_result); … … 1191 1196 1192 1197 /* Request option value */ 1193 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, 1194 1201 (sysarg_t) socket->socket_id, (sysarg_t) optname, socket->service, 1195 1202 NULL); 1196 1203 1197 1204 /* Read the length */ 1198 if (async_ obsolete_data_read_start(socket->phone, optlen,1205 if (async_data_read_start(exch, optlen, 1199 1206 sizeof(*optlen)) == EOK) { 1200 1207 /* Read the value */ 1201 async_obsolete_data_read_start(socket->phone, value, *optlen); 1202 } 1208 async_data_read_start(exch, value, *optlen); 1209 } 1210 1211 async_exchange_end(exch); 1203 1212 1204 1213 fibril_rwlock_read_unlock(&socket_globals.lock);
Note:
See TracChangeset
for help on using the changeset viewer.
