Changeset 6b82009 in mainline for uspace/lib/c/generic/net/socket_client.c
- Timestamp:
- 2011-06-22T20:41:41Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- ef09a7a
- Parents:
- 55091847
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/net/socket_client.c
r55091847 r6b82009 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> … … 84 83 /** Socket identifier. */ 85 84 int socket_id; 86 /** Parent module phone. */87 int phone;85 /** Parent module session. */ 86 async_sess_t *sess; 88 87 /** Parent module service. */ 89 88 services_t service; … … 144 143 /** Socket client library global data. */ 145 144 static struct socket_client_globals { 146 /** TCP module phone. */ 147 int tcp_phone; 148 /** UDP module phone. */ 149 int udp_phone; 150 151 // /** The last socket identifier. 152 // */ 153 // int last_id; 145 /** TCP module session. */ 146 async_sess_t *tcp_sess; 147 /** UDP module session. */ 148 async_sess_t *udp_sess; 154 149 155 150 /** Active sockets. */ … … 164 159 fibril_rwlock_t lock; 165 160 } socket_globals = { 166 .tcp_phone = -1, 167 .udp_phone = -1, 168 // .last_id = 0, 161 .tcp_sess = NULL, 162 .udp_sess = NULL, 169 163 .sockets = NULL, 170 164 .lock = FIBRIL_RWLOCK_INITIALIZER(socket_globals.lock) … … 280 274 } 281 275 282 /** Returns the TCP module phone. 283 * 284 * Connects to the TCP module if necessary. 285 * 286 * @return The TCP module phone. 287 * @return Other error codes as defined for the 288 * bind_service() function. 289 */ 290 static int socket_get_tcp_phone(void) 291 { 292 if (socket_globals.tcp_phone < 0) { 293 socket_globals.tcp_phone = bind_service(SERVICE_TCP, 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, 294 287 0, 0, SERVICE_TCP, socket_connection); 295 288 } 296 289 297 return socket_globals.tcp_phone; 298 } 299 300 /** Returns the UDP module phone. 301 * 302 * Connects to the UDP module if necessary. 303 * 304 * @return The UDP module phone. 305 * @return Other error codes as defined for the 306 * bind_service() function. 307 */ 308 static int socket_get_udp_phone(void) 309 { 310 if (socket_globals.udp_phone < 0) { 311 socket_globals.udp_phone = bind_service(SERVICE_UDP, 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, 312 304 0, 0, SERVICE_UDP, socket_connection); 313 305 } 314 306 315 return socket_globals.udp_ phone;307 return socket_globals.udp_sess; 316 308 } 317 309 … … 329 321 sockets = socket_get_sockets(); 330 322 count = 0; 331 // socket_id = socket_globals.last_id;332 323 333 324 do { … … 342 333 if (socket_id < INT_MAX) { 343 334 ++socket_id; 344 /* } else if(socket_globals.last_id) { 345 * socket_globals.last_id = 0; 346 * socket_id = 1; 347 */ } else { 335 } else { 348 336 return ELIMIT; 349 337 } 350 338 } 351 339 } while (sockets_find(sockets, socket_id)); 352 353 // last_id = socket_id 340 354 341 return socket_id; 355 342 } … … 358 345 * 359 346 * @param[in,out] socket The socket to be initialized. 360 * @param[in] socket_id The new socket identifier. 361 * @param[in] phone The parent module phone. 362 * @param[in] service The parent module service. 363 */ 364 static void 365 socket_initialize(socket_t *socket, int socket_id, int phone, 366 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) 367 353 { 368 354 socket->socket_id = socket_id; 369 socket-> phone = phone;355 socket->sess = sess; 370 356 socket->service = service; 371 357 dyn_fifo_initialize(&socket->received, SOCKET_INITIAL_RECEIVED_SIZE); … … 397 383 { 398 384 socket_t *socket; 399 int phone;385 async_sess_t *sess; 400 386 int socket_id; 401 387 services_t service; … … 414 400 switch (protocol) { 415 401 case IPPROTO_TCP: 416 phone = socket_get_tcp_phone();402 sess = socket_get_tcp_sess(); 417 403 service = SERVICE_TCP; 418 404 break; … … 429 415 switch (protocol) { 430 416 case IPPROTO_UDP: 431 phone = socket_get_udp_phone();417 sess = socket_get_udp_sess(); 432 418 service = SERVICE_UDP; 433 419 break; … … 450 436 } 451 437 452 if ( phone < 0)453 return phone;438 if (sess == NULL) 439 return ENOENT; 454 440 455 441 /* Create a new socket structure */ … … 468 454 return socket_id; 469 455 } 470 471 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, 472 459 &fragment_size, &header_size); 460 async_exchange_end(exch); 461 473 462 if (rc != EOK) { 474 463 fibril_rwlock_write_unlock(&socket_globals.lock); … … 481 470 482 471 /* Finish the new socket initialization */ 483 socket_initialize(socket, socket_id, phone, service);472 socket_initialize(socket, socket_id, sess, service); 484 473 /* Store the new socket */ 485 474 rc = sockets_add(socket_get_sockets(), socket_id, socket); … … 490 479 dyn_fifo_destroy(&socket->accepted); 491 480 free(socket); 492 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, 493 484 service); 485 async_exchange_end(exch); 486 494 487 return rc; 495 488 } … … 535 528 536 529 /* Request the message */ 537 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, 538 532 (sysarg_t) socket->socket_id, arg2, socket->service, NULL); 539 533 /* Send the address */ 540 async_obsolete_data_write_start(socket->phone, data, datalength); 534 async_data_write_start(exch, data, datalength); 535 async_exchange_end(exch); 541 536 542 537 fibril_rwlock_read_unlock(&socket_globals.lock); … … 595 590 596 591 /* Request listen backlog change */ 597 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, 598 594 (sysarg_t) socket->socket_id, (sysarg_t) backlog, socket->service); 595 async_exchange_end(exch); 599 596 600 597 fibril_rwlock_read_unlock(&socket_globals.lock); … … 666 663 return socket_id; 667 664 } 668 socket_initialize(new_socket, socket_id, socket-> phone,665 socket_initialize(new_socket, socket_id, socket->sess, 669 666 socket->service); 670 667 result = sockets_add(socket_get_sockets(), new_socket->socket_id, … … 678 675 679 676 /* Request accept */ 680 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, 681 679 (sysarg_t) socket->socket_id, 0, socket->service, 0, 682 680 new_socket->socket_id, &answer); 683 681 684 682 /* Read address */ 685 async_obsolete_data_read_start(socket->phone, cliaddr, *addrlen); 683 async_data_read_start(exch, cliaddr, *addrlen); 684 async_exchange_end(exch); 685 686 686 fibril_rwlock_write_unlock(&socket_globals.lock); 687 687 async_wait_for(message_id, &ipc_result); … … 777 777 778 778 /* Request close */ 779 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, 780 781 (sysarg_t) socket->socket_id, 0, socket->service); 782 async_exchange_end(exch); 783 781 784 if (rc != EOK) { 782 785 fibril_rwlock_write_unlock(&socket_globals.lock); … … 850 853 851 854 /* Request send */ 852 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, 853 858 (sysarg_t) socket->socket_id, 854 859 (fragments == 1 ? datalength : socket->data_fragment_size), … … 857 862 /* Send the address if given */ 858 863 if (!toaddr || 859 (async_ obsolete_data_write_start(socket->phone, toaddr, addrlen) == EOK)) {864 (async_data_write_start(exch, toaddr, addrlen) == EOK)) { 860 865 if (fragments == 1) { 861 866 /* Send all if only one fragment */ 862 async_ obsolete_data_write_start(socket->phone, data, datalength);867 async_data_write_start(exch, data, datalength); 863 868 } else { 864 869 /* Send the first fragment */ 865 async_ obsolete_data_write_start(socket->phone, data,870 async_data_write_start(exch, data, 866 871 socket->data_fragment_size - socket->header_size); 867 872 data = ((const uint8_t *) data) + … … 870 875 /* Send the middle fragments */ 871 876 while (--fragments > 1) { 872 async_ obsolete_data_write_start(socket->phone, data,877 async_data_write_start(exch, data, 873 878 socket->data_fragment_size); 874 879 data = ((const uint8_t *) data) + … … 877 882 878 883 /* Send the last fragment */ 879 async_ obsolete_data_write_start(socket->phone, data,884 async_data_write_start(exch, data, 880 885 (datalength + socket->header_size) % 881 886 socket->data_fragment_size); 882 887 } 883 888 } 889 890 async_exchange_end(exch); 884 891 885 892 async_wait_for(message_id, &result); … … 1023 1030 return 0; 1024 1031 } 1032 1033 async_exch_t *exch = async_exchange_begin(socket->sess); 1025 1034 1026 1035 /* Prepare lengths if more fragments */ … … 1035 1044 1036 1045 /* Request packet data */ 1037 message_id = async_ obsolete_send_4(socket->phone, message,1046 message_id = async_send_4(exch, message, 1038 1047 (sysarg_t) socket->socket_id, 0, socket->service, 1039 1048 (sysarg_t) flags, &answer); … … 1041 1050 /* Read the address if desired */ 1042 1051 if(!fromaddr || 1043 (async_ obsolete_data_read_start(socket->phone, fromaddr,1052 (async_data_read_start(exch, fromaddr, 1044 1053 *addrlen) == EOK)) { 1045 1054 /* Read the fragment lengths */ 1046 if (async_ obsolete_data_read_start(socket->phone, lengths,1055 if (async_data_read_start(exch, lengths, 1047 1056 sizeof(int) * (fragments + 1)) == EOK) { 1048 1057 if (lengths[fragments] <= datalength) { … … 1051 1060 for (index = 0; index < fragments; 1052 1061 ++index) { 1053 async_obsolete_data_read_start( 1054 socket->phone, data, 1062 async_data_read_start(exch, data, 1055 1063 lengths[index]); 1056 1064 data = ((uint8_t *) data) + … … 1064 1072 } else { /* fragments == 1 */ 1065 1073 /* Request packet data */ 1066 message_id = async_ obsolete_send_4(socket->phone, message,1074 message_id = async_send_4(exch, message, 1067 1075 (sysarg_t) socket->socket_id, 0, socket->service, 1068 1076 (sysarg_t) flags, &answer); … … 1070 1078 /* Read the address if desired */ 1071 1079 if (!fromaddr || 1072 (async_obsolete_data_read_start(socket->phone, fromaddr, 1073 *addrlen) == EOK)) { 1080 (async_data_read_start(exch, fromaddr, *addrlen) == EOK)) { 1074 1081 /* Read all if only one fragment */ 1075 async_ obsolete_data_read_start(socket->phone, data, datalength);1082 async_data_read_start(exch, data, datalength); 1076 1083 } 1077 1084 } 1085 1086 async_exchange_end(exch); 1078 1087 1079 1088 async_wait_for(message_id, &ipc_result); … … 1187 1196 1188 1197 /* Request option value */ 1189 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, 1190 1201 (sysarg_t) socket->socket_id, (sysarg_t) optname, socket->service, 1191 1202 NULL); 1192 1203 1193 1204 /* Read the length */ 1194 if (async_ obsolete_data_read_start(socket->phone, optlen,1205 if (async_data_read_start(exch, optlen, 1195 1206 sizeof(*optlen)) == EOK) { 1196 1207 /* Read the value */ 1197 async_obsolete_data_read_start(socket->phone, value, *optlen); 1198 } 1208 async_data_read_start(exch, value, *optlen); 1209 } 1210 1211 async_exchange_end(exch); 1199 1212 1200 1213 fibril_rwlock_read_unlock(&socket_globals.lock);
Note:
See TracChangeset
for help on using the changeset viewer.