Changes in uspace/lib/c/generic/net/socket_client.c [b35fea3:228e490] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/net/socket_client.c
rb35fea3 r228e490 220 220 fibril_rwlock_read_lock(&socket_globals.lock); 221 221 222 / * Find the socket */222 // find the socket 223 223 socket = sockets_find(socket_get_sockets(), 224 224 SOCKET_GET_SOCKET_ID(call)); … … 232 232 case NET_SOCKET_RECEIVED: 233 233 fibril_mutex_lock(&socket->receive_lock); 234 / * Push the number of received packet fragments */234 // push the number of received packet fragments 235 235 rc = dyn_fifo_push(&socket->received, 236 236 SOCKET_GET_DATA_FRAGMENTS(call), 237 237 SOCKET_MAX_RECEIVED_SIZE); 238 238 if (rc == EOK) { 239 / * Signal the received packet */239 // signal the received packet 240 240 fibril_condvar_signal(&socket->receive_signal); 241 241 } … … 244 244 245 245 case NET_SOCKET_ACCEPTED: 246 / * Push the new socket identifier */246 // push the new socket identifier 247 247 fibril_mutex_lock(&socket->accept_lock); 248 248 rc = dyn_fifo_push(&socket->accepted, 1, 249 249 SOCKET_MAX_ACCEPTED_SIZE); 250 250 if (rc == EOK) { 251 / * Signal the accepted socket */251 // signal the accepted socket 252 252 fibril_condvar_signal(&socket->accept_signal); 253 253 } … … 264 264 fibril_rwlock_write_lock(&socket->sending_lock); 265 265 266 / * Set the data fragment size */266 // set the data fragment size 267 267 socket->data_fragment_size = 268 268 SOCKET_GET_DATA_FRAGMENT_SIZE(call); … … 342 342 socket_id = 1; 343 343 ++count; 344 / * Only this branch for last_id */344 // only this branch for last_id 345 345 } else { 346 346 if (socket_id < INT_MAX) { … … 408 408 int rc; 409 409 410 / * Find the appropriate service */410 // find the appropriate service 411 411 switch (domain) { 412 412 case PF_INET: … … 457 457 return phone; 458 458 459 / * Create a new socket structure */459 // create a new socket structure 460 460 socket = (socket_t *) malloc(sizeof(socket_t)); 461 461 if (!socket) … … 465 465 fibril_rwlock_write_lock(&socket_globals.lock); 466 466 467 / * Request a new socket */467 // request a new socket 468 468 socket_id = socket_generate_new_id(); 469 469 if (socket_id <= 0) { … … 484 484 socket->header_size = (size_t) header_size; 485 485 486 / * Finish the new socket initialization */486 // finish the new socket initialization 487 487 socket_initialize(socket, socket_id, phone, service); 488 / * Store the new socket */488 // store the new socket 489 489 rc = sockets_add(socket_get_sockets(), socket_id, socket); 490 490 … … 531 531 fibril_rwlock_read_lock(&socket_globals.lock); 532 532 533 / * Find the socket */533 // find the socket 534 534 socket = sockets_find(socket_get_sockets(), socket_id); 535 535 if (!socket) { … … 538 538 } 539 539 540 / * Request the message */540 // request the message 541 541 message_id = async_send_3(socket->phone, message, 542 542 (sysarg_t) socket->socket_id, arg2, socket->service, NULL); 543 / * Send the address */543 // send the address 544 544 async_data_write_start(socket->phone, data, datalength); 545 545 … … 566 566 return EINVAL; 567 567 568 / * Send the address */568 // send the address 569 569 return socket_send_data(socket_id, NET_SOCKET_BIND, 0, my_addr, 570 570 (size_t) addrlen); … … 591 591 fibril_rwlock_read_lock(&socket_globals.lock); 592 592 593 / * Find the socket */593 // find the socket 594 594 socket = sockets_find(socket_get_sockets(), socket_id); 595 595 if (!socket) { … … 598 598 } 599 599 600 / * Request listen backlog change */600 // request listen backlog change 601 601 result = (int) async_req_3_0(socket->phone, NET_SOCKET_LISTEN, 602 602 (sysarg_t) socket->socket_id, (sysarg_t) backlog, socket->service); … … 634 634 fibril_rwlock_write_lock(&socket_globals.lock); 635 635 636 / * Find the socket */636 // find the socket 637 637 socket = sockets_find(socket_get_sockets(), socket_id); 638 638 if (!socket) { … … 643 643 fibril_mutex_lock(&socket->accept_lock); 644 644 645 / * Wait for an accepted socket */645 // wait for an accepted socket 646 646 ++ socket->blocked; 647 647 while (dyn_fifo_value(&socket->accepted) <= 0) { 648 648 fibril_rwlock_write_unlock(&socket_globals.lock); 649 649 fibril_condvar_wait(&socket->accept_signal, &socket->accept_lock); 650 / * Drop the accept lock to avoid deadlock */650 // drop the accept lock to avoid deadlock 651 651 fibril_mutex_unlock(&socket->accept_lock); 652 652 fibril_rwlock_write_lock(&socket_globals.lock); … … 655 655 -- socket->blocked; 656 656 657 / * Create a new socket */657 // create a new scoket 658 658 new_socket = (socket_t *) malloc(sizeof(socket_t)); 659 659 if (!new_socket) { … … 681 681 } 682 682 683 / * Request accept */683 // request accept 684 684 message_id = async_send_5(socket->phone, NET_SOCKET_ACCEPT, 685 685 (sysarg_t) socket->socket_id, 0, socket->service, 0, 686 686 new_socket->socket_id, &answer); 687 687 688 / * Read address */688 // read address 689 689 ipc_data_read_start(socket->phone, cliaddr, *addrlen); 690 690 fibril_rwlock_write_unlock(&socket_globals.lock); … … 695 695 result = EINVAL; 696 696 697 / * Dequeue the accepted socket if successful */697 // dequeue the accepted socket if successful 698 698 dyn_fifo_pop(&socket->accepted); 699 / * Set address length */699 // set address length 700 700 *addrlen = SOCKET_GET_ADDRESS_LENGTH(answer); 701 701 new_socket->data_fragment_size = 702 702 SOCKET_GET_DATA_FRAGMENT_SIZE(answer); 703 703 } else if (result == ENOTSOCK) { 704 / * Empty the queue if no accepted sockets */704 // empty the queue if no accepted sockets 705 705 while (dyn_fifo_pop(&socket->accepted) > 0) 706 706 ; … … 731 731 return EDESTADDRREQ; 732 732 733 / * Send the address */733 // send the address 734 734 return socket_send_data(socket_id, NET_SOCKET_CONNECT, 0, serv_addr, 735 735 addrlen); … … 744 744 int accepted_id; 745 745 746 / * Destroy all accepted sockets */746 // destroy all accepted sockets 747 747 while ((accepted_id = dyn_fifo_pop(&socket->accepted)) >= 0) 748 748 socket_destroy(sockets_find(socket_get_sockets(), accepted_id)); … … 780 780 } 781 781 782 / * Request close */782 // request close 783 783 rc = (int) async_req_3_0(socket->phone, NET_SOCKET_CLOSE, 784 784 (sysarg_t) socket->socket_id, 0, socket->service); … … 787 787 return rc; 788 788 } 789 / * Free the socket structure */789 // free the socket structure 790 790 socket_destroy(socket); 791 791 … … 833 833 fibril_rwlock_read_lock(&socket_globals.lock); 834 834 835 / * Find socket */835 // find socket 836 836 socket = sockets_find(socket_get_sockets(), socket_id); 837 837 if (!socket) { … … 842 842 fibril_rwlock_read_lock(&socket->sending_lock); 843 843 844 / * Compute data fragment count */844 // compute data fragment count 845 845 if (socket->data_fragment_size > 0) { 846 846 fragments = (datalength + socket->header_size) / … … 853 853 } 854 854 855 / * Request send */855 // request send 856 856 message_id = async_send_5(socket->phone, message, 857 857 (sysarg_t) socket->socket_id, … … 859 859 socket->service, (sysarg_t) flags, fragments, &answer); 860 860 861 / * Send the address if given */861 // send the address if given 862 862 if (!toaddr || 863 863 (async_data_write_start(socket->phone, toaddr, addrlen) == EOK)) { 864 864 if (fragments == 1) { 865 / * Send all if only one fragment */865 // send all if only one fragment 866 866 async_data_write_start(socket->phone, data, datalength); 867 867 } else { 868 / * Send the first fragment */868 // send the first fragment 869 869 async_data_write_start(socket->phone, data, 870 870 socket->data_fragment_size - socket->header_size); … … 872 872 socket->data_fragment_size - socket->header_size; 873 873 874 / * Send the middle fragments */874 // send the middle fragments 875 875 while (--fragments > 1) { 876 876 async_data_write_start(socket->phone, data, … … 880 880 } 881 881 882 / * Send the last fragment */882 // send the last fragment 883 883 async_data_write_start(socket->phone, data, 884 884 (datalength + socket->header_size) % … … 892 892 (SOCKET_GET_DATA_FRAGMENT_SIZE(answer) != 893 893 socket->data_fragment_size)) { 894 / * Set the data fragment size */894 // set the data fragment size 895 895 socket->data_fragment_size = 896 896 SOCKET_GET_DATA_FRAGMENT_SIZE(answer); … … 917 917 int send(int socket_id, void *data, size_t datalength, int flags) 918 918 { 919 / * Without the address */919 // without the address 920 920 return sendto_core(NET_SOCKET_SEND, socket_id, data, datalength, flags, 921 921 NULL, 0); … … 950 950 return EDESTADDRREQ; 951 951 952 / * With the address */952 // with the address 953 953 return sendto_core(NET_SOCKET_SENDTO, socket_id, data, datalength, 954 954 flags, toaddr, addrlen); … … 966 966 * read. The actual address length is set. Used only if 967 967 * fromaddr is not NULL. 968 * @return Positive received message size in bytes on success. 969 * @return Zero if no more data (other side closed the connection). 968 * @return EOK on success. 970 969 * @return ENOTSOCK if the socket is not found. 971 970 * @return EBADMEM if the data parameter is NULL. … … 973 972 * @return Other error codes as defined for the spcific message. 974 973 */ 975 static ssize_t974 static int 976 975 recvfrom_core(sysarg_t message, int socket_id, void *data, size_t datalength, 977 976 int flags, struct sockaddr *fromaddr, socklen_t *addrlen) … … 985 984 size_t index; 986 985 ipc_call_t answer; 987 ssize_t retval;988 986 989 987 if (!data) … … 998 996 fibril_rwlock_read_lock(&socket_globals.lock); 999 997 1000 / * Find the socket */998 // find the socket 1001 999 socket = sockets_find(socket_get_sockets(), socket_id); 1002 1000 if (!socket) { … … 1006 1004 1007 1005 fibril_mutex_lock(&socket->receive_lock); 1008 / * Wait for a received packet */1006 // wait for a received packet 1009 1007 ++socket->blocked; 1010 while ((result = dyn_fifo_value(&socket->received)) < 0) {1008 while ((result = dyn_fifo_value(&socket->received)) <= 0) { 1011 1009 fibril_rwlock_read_unlock(&socket_globals.lock); 1012 1010 fibril_condvar_wait(&socket->receive_signal, 1013 1011 &socket->receive_lock); 1014 1012 1015 / * Drop the receive lock to avoid deadlock */1013 // drop the receive lock to avoid deadlock 1016 1014 fibril_mutex_unlock(&socket->receive_lock); 1017 1015 fibril_rwlock_read_lock(&socket_globals.lock); … … 1021 1019 fragments = (size_t) result; 1022 1020 1023 if (fragments == 0) { 1024 /* No more data, other side has closed the connection. */ 1025 fibril_mutex_unlock(&socket->receive_lock); 1026 fibril_rwlock_read_unlock(&socket_globals.lock); 1027 return 0; 1028 } 1029 1030 /* Prepare lengths if more fragments */ 1021 // prepare lengths if more fragments 1031 1022 if (fragments > 1) { 1032 1023 lengths = (size_t *) malloc(sizeof(size_t) * fragments + … … 1038 1029 } 1039 1030 1040 / * Request packet data */1031 // request packet data 1041 1032 message_id = async_send_4(socket->phone, message, 1042 1033 (sysarg_t) socket->socket_id, 0, socket->service, 1043 1034 (sysarg_t) flags, &answer); 1044 1035 1045 / * Read the address if desired */1036 // read the address if desired 1046 1037 if(!fromaddr || 1047 1038 (async_data_read_start(socket->phone, fromaddr, 1048 1039 *addrlen) == EOK)) { 1049 / * Read the fragment lengths */1040 // read the fragment lengths 1050 1041 if (async_data_read_start(socket->phone, lengths, 1051 1042 sizeof(int) * (fragments + 1)) == EOK) { 1052 1043 if (lengths[fragments] <= datalength) { 1053 1044 1054 / * Read all fragments if long enough */1045 // read all fragments if long enough 1055 1046 for (index = 0; index < fragments; 1056 1047 ++index) { … … 1066 1057 1067 1058 free(lengths); 1068 } else { /* fragments == 1 */1069 / * Request packet data */1059 } else { 1060 // request packet data 1070 1061 message_id = async_send_4(socket->phone, message, 1071 1062 (sysarg_t) socket->socket_id, 0, socket->service, 1072 1063 (sysarg_t) flags, &answer); 1073 1064 1074 / * Read the address if desired */1065 // read the address if desired 1075 1066 if (!fromaddr || 1076 1067 (async_data_read_start(socket->phone, fromaddr, 1077 1068 *addrlen) == EOK)) { 1078 / * Read all if only one fragment */1069 // read all if only one fragment 1079 1070 async_data_read_start(socket->phone, data, datalength); 1080 1071 } … … 1084 1075 result = (int) ipc_result; 1085 1076 if (result == EOK) { 1086 / * Dequeue the received packet */1077 // dequeue the received packet 1087 1078 dyn_fifo_pop(&socket->received); 1088 / * Return read data length */1089 re tval= SOCKET_GET_READ_DATA_LENGTH(answer);1090 / * Set address length */1079 // return read data length 1080 result = SOCKET_GET_READ_DATA_LENGTH(answer); 1081 // set address length 1091 1082 if (fromaddr && addrlen) 1092 1083 *addrlen = SOCKET_GET_ADDRESS_LENGTH(answer); 1093 } else {1094 retval = (ssize_t) result;1095 1084 } 1096 1085 1097 1086 fibril_mutex_unlock(&socket->receive_lock); 1098 1087 fibril_rwlock_read_unlock(&socket_globals.lock); 1099 return re tval;1088 return result; 1100 1089 } 1101 1090 … … 1106 1095 * @param[in] datalength The data length. 1107 1096 * @param[in] flags Various receive flags. 1108 * @return Positive received message size in bytes on success. 1109 * @return Zero if no more data (other side closed the connection). 1097 * @return EOK on success. 1110 1098 * @return ENOTSOCK if the socket is not found. 1111 1099 * @return EBADMEM if the data parameter is NULL. … … 1114 1102 * message. 1115 1103 */ 1116 ssize_t recv(int socket_id, void *data, size_t datalength, int flags)1117 { 1118 / * Without the address */1104 int recv(int socket_id, void *data, size_t datalength, int flags) 1105 { 1106 // without the address 1119 1107 return recvfrom_core(NET_SOCKET_RECV, socket_id, data, datalength, 1120 1108 flags, NULL, NULL); … … 1130 1118 * @param[in,out] addrlen The address length. The maximum address length is 1131 1119 * read. The actual address length is set. 1132 * @return Positive received message size in bytes on success. 1133 * @return Zero if no more data (other side closed the connection). 1120 * @return EOK on success. 1134 1121 * @return ENOTSOCK if the socket is not found. 1135 1122 * @return EBADMEM if the data or fromaddr parameter is NULL. … … 1138 1125 * message. 1139 1126 */ 1140 ssize_t1127 int 1141 1128 recvfrom(int socket_id, void *data, size_t datalength, int flags, 1142 1129 struct sockaddr *fromaddr, socklen_t *addrlen) … … 1148 1135 return NO_DATA; 1149 1136 1150 / * With the address */1137 // with the address 1151 1138 return recvfrom_core(NET_SOCKET_RECVFROM, socket_id, data, datalength, 1152 1139 flags, fromaddr, addrlen); … … 1183 1170 fibril_rwlock_read_lock(&socket_globals.lock); 1184 1171 1185 / * Find the socket */1172 // find the socket 1186 1173 socket = sockets_find(socket_get_sockets(), socket_id); 1187 1174 if (!socket) { … … 1190 1177 } 1191 1178 1192 / * Request option value */1179 // request option value 1193 1180 message_id = async_send_3(socket->phone, NET_SOCKET_GETSOCKOPT, 1194 1181 (sysarg_t) socket->socket_id, (sysarg_t) optname, socket->service, 1195 1182 NULL); 1196 1183 1197 / * Read the length */1184 // read the length 1198 1185 if (async_data_read_start(socket->phone, optlen, 1199 1186 sizeof(*optlen)) == EOK) { 1200 / * Read the value */1187 // read the value 1201 1188 async_data_read_start(socket->phone, value, *optlen); 1202 1189 } … … 1225 1212 size_t optlen) 1226 1213 { 1227 / * Send the value */1214 // send the value 1228 1215 return socket_send_data(socket_id, NET_SOCKET_SETSOCKOPT, 1229 1216 (sysarg_t) optname, value, optlen);
Note:
See TracChangeset
for help on using the changeset viewer.