Ignore:
Timestamp:
2011-09-04T11:30:58Z (14 years ago)
Author:
Maurizio Lombardi <m.lombardi85@…>
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.
Message:

Merge mainline changes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/net/socket_client.c

    rd2c67e7 r8ff0bd2  
    3939#include <assert.h>
    4040#include <async.h>
    41 #include <async_obsolete.h>
    4241#include <fibril_synch.h>
    4342#include <stdint.h>
     
    6564#define SOCKET_MAX_ACCEPTED_SIZE        0
    6665
    67 /** Default timeout for connections in microseconds. */
    68 #define SOCKET_CONNECT_TIMEOUT  (1 * 1000 * 1000)
    69 
    7066/**
    7167 * Maximum number of random attempts to find a new socket identifier before
     
    8783        /** Socket identifier. */
    8884        int socket_id;
    89         /** Parent module phone. */
    90         int phone;
     85        /** Parent module session. */
     86        async_sess_t *sess;
    9187        /** Parent module service. */
    9288        services_t service;
     
    147143/** Socket client library global data. */
    148144static 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;
    157149
    158150        /** Active sockets. */
     
    167159        fibril_rwlock_t lock;
    168160} socket_globals = {
    169         .tcp_phone = -1,
    170         .udp_phone = -1,
    171 //      .last_id = 0,
     161        .tcp_sess = NULL,
     162        .udp_sess = NULL,
    172163        .sockets = NULL,
    173164        .lock = FIBRIL_RWLOCK_INITIALIZER(socket_globals.lock)
     
    203194 * @param[in] iid       The initial message identifier.
    204195 * @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 */
     198static void socket_connection(ipc_callid_t iid, ipc_call_t * icall, void *arg)
    207199{
    208200        ipc_callid_t callid;
     
    282274}
    283275
    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 */
     283static 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 */
     300static 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;
    320308}
    321309
     
    333321        sockets = socket_get_sockets();
    334322        count = 0;
    335 //      socket_id = socket_globals.last_id;
    336323
    337324        do {
     
    346333                        if (socket_id < INT_MAX) {
    347334                                ++socket_id;
    348 /*                      } else if(socket_globals.last_id) {
    349  *                              socket_globals.last_id = 0;
    350  *                              socket_id = 1;
    351  */                     } else {
     335                        } else {
    352336                                return ELIMIT;
    353337                        }
    354338                }
    355339        } while (sockets_find(sockets, socket_id));
    356 
    357 //      last_id = socket_id
     340       
    358341        return socket_id;
    359342}
     
    362345 *
    363346 * @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 */
     351static void socket_initialize(socket_t *socket, int socket_id,
     352    async_sess_t *sess, services_t service)
    371353{
    372354        socket->socket_id = socket_id;
    373         socket->phone = phone;
     355        socket->sess = sess;
    374356        socket->service = service;
    375357        dyn_fifo_initialize(&socket->received, SOCKET_INITIAL_RECEIVED_SIZE);
     
    396378 * @return              Other error codes as defined for the NET_SOCKET message.
    397379 * @return              Other error codes as defined for the
    398  *                      bind_service_timeout() function.
     380 *                      bind_service() function.
    399381 */
    400382int socket(int domain, int type, int protocol)
    401383{
    402384        socket_t *socket;
    403         int phone;
     385        async_sess_t *sess;
    404386        int socket_id;
    405387        services_t service;
     
    418400                        switch (protocol) {
    419401                        case IPPROTO_TCP:
    420                                 phone = socket_get_tcp_phone();
     402                                sess = socket_get_tcp_sess();
    421403                                service = SERVICE_TCP;
    422404                                break;
     
    433415                        switch (protocol) {
    434416                        case IPPROTO_UDP:
    435                                 phone = socket_get_udp_phone();
     417                                sess = socket_get_udp_sess();
    436418                                service = SERVICE_UDP;
    437419                                break;
     
    454436        }
    455437
    456         if (phone < 0)
    457                 return phone;
     438        if (sess == NULL)
     439                return ENOENT;
    458440
    459441        /* Create a new socket structure */
     
    472454                return socket_id;
    473455        }
    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,
    476459            &fragment_size, &header_size);
     460        async_exchange_end(exch);
     461       
    477462        if (rc != EOK) {
    478463                fibril_rwlock_write_unlock(&socket_globals.lock);
     
    485470
    486471        /* Finish the new socket initialization */
    487         socket_initialize(socket, socket_id, phone, service);
     472        socket_initialize(socket, socket_id, sess, service);
    488473        /* Store the new socket */
    489474        rc = sockets_add(socket_get_sockets(), socket_id, socket);
     
    494479                dyn_fifo_destroy(&socket->accepted);
    495480                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,
    497484                    service);
     485                async_exchange_end(exch);
     486               
    498487                return rc;
    499488        }
     
    539528
    540529        /* 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,
    542532            (sysarg_t) socket->socket_id, arg2, socket->service, NULL);
    543533        /* 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);
    545536
    546537        fibril_rwlock_read_unlock(&socket_globals.lock);
     
    599590
    600591        /* 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,
    602594            (sysarg_t) socket->socket_id, (sysarg_t) backlog, socket->service);
     595        async_exchange_end(exch);
    603596
    604597        fibril_rwlock_read_unlock(&socket_globals.lock);
     
    670663                return socket_id;
    671664        }
    672         socket_initialize(new_socket, socket_id, socket->phone,
     665        socket_initialize(new_socket, socket_id, socket->sess,
    673666            socket->service);
    674667        result = sockets_add(socket_get_sockets(), new_socket->socket_id,
     
    682675
    683676        /* 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,
    685679            (sysarg_t) socket->socket_id, 0, socket->service, 0,
    686680            new_socket->socket_id, &answer);
    687681
    688682        /* 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       
    690686        fibril_rwlock_write_unlock(&socket_globals.lock);
    691687        async_wait_for(message_id, &ipc_result);
     
    781777
    782778        /* 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,
    784781            (sysarg_t) socket->socket_id, 0, socket->service);
     782        async_exchange_end(exch);
     783       
    785784        if (rc != EOK) {
    786785                fibril_rwlock_write_unlock(&socket_globals.lock);
     
    854853
    855854        /* 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,
    857858            (sysarg_t) socket->socket_id,
    858859            (fragments == 1 ? datalength : socket->data_fragment_size),
     
    861862        /* Send the address if given */
    862863        if (!toaddr ||
    863             (async_obsolete_data_write_start(socket->phone, toaddr, addrlen) == EOK)) {
     864            (async_data_write_start(exch, toaddr, addrlen) == EOK)) {
    864865                if (fragments == 1) {
    865866                        /* Send all if only one fragment */
    866                         async_obsolete_data_write_start(socket->phone, data, datalength);
     867                        async_data_write_start(exch, data, datalength);
    867868                } else {
    868869                        /* Send the first fragment */
    869                         async_obsolete_data_write_start(socket->phone, data,
     870                        async_data_write_start(exch, data,
    870871                            socket->data_fragment_size - socket->header_size);
    871872                        data = ((const uint8_t *) data) +
     
    874875                        /* Send the middle fragments */
    875876                        while (--fragments > 1) {
    876                                 async_obsolete_data_write_start(socket->phone, data,
     877                                async_data_write_start(exch, data,
    877878                                    socket->data_fragment_size);
    878879                                data = ((const uint8_t *) data) +
     
    881882
    882883                        /* Send the last fragment */
    883                         async_obsolete_data_write_start(socket->phone, data,
     884                        async_data_write_start(exch, data,
    884885                            (datalength + socket->header_size) %
    885886                            socket->data_fragment_size);
    886887                }
    887888        }
     889       
     890        async_exchange_end(exch);
    888891
    889892        async_wait_for(message_id, &result);
     
    10271030                return 0;
    10281031        }
     1032       
     1033        async_exch_t *exch = async_exchange_begin(socket->sess);
    10291034
    10301035        /* Prepare lengths if more fragments */
     
    10391044
    10401045                /* Request packet data */
    1041                 message_id = async_obsolete_send_4(socket->phone, message,
     1046                message_id = async_send_4(exch, message,
    10421047                    (sysarg_t) socket->socket_id, 0, socket->service,
    10431048                    (sysarg_t) flags, &answer);
     
    10451050                /* Read the address if desired */
    10461051                if(!fromaddr ||
    1047                     (async_obsolete_data_read_start(socket->phone, fromaddr,
     1052                    (async_data_read_start(exch, fromaddr,
    10481053                    *addrlen) == EOK)) {
    10491054                        /* Read the fragment lengths */
    1050                         if (async_obsolete_data_read_start(socket->phone, lengths,
     1055                        if (async_data_read_start(exch, lengths,
    10511056                            sizeof(int) * (fragments + 1)) == EOK) {
    10521057                                if (lengths[fragments] <= datalength) {
     
    10551060                                        for (index = 0; index < fragments;
    10561061                                            ++index) {
    1057                                                 async_obsolete_data_read_start(
    1058                                                     socket->phone, data,
     1062                                                async_data_read_start(exch, data,
    10591063                                                    lengths[index]);
    10601064                                                data = ((uint8_t *) data) +
     
    10681072        } else { /* fragments == 1 */
    10691073                /* Request packet data */
    1070                 message_id = async_obsolete_send_4(socket->phone, message,
     1074                message_id = async_send_4(exch, message,
    10711075                    (sysarg_t) socket->socket_id, 0, socket->service,
    10721076                    (sysarg_t) flags, &answer);
     
    10741078                /* Read the address if desired */
    10751079                if (!fromaddr ||
    1076                     (async_obsolete_data_read_start(socket->phone, fromaddr,
    1077                         *addrlen) == EOK)) {
     1080                    (async_data_read_start(exch, fromaddr, *addrlen) == EOK)) {
    10781081                        /* Read all if only one fragment */
    1079                         async_obsolete_data_read_start(socket->phone, data, datalength);
     1082                        async_data_read_start(exch, data, datalength);
    10801083                }
    10811084        }
     1085       
     1086        async_exchange_end(exch);
    10821087
    10831088        async_wait_for(message_id, &ipc_result);
     
    11911196
    11921197        /* 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,
    11941201            (sysarg_t) socket->socket_id, (sysarg_t) optname, socket->service,
    11951202            NULL);
    11961203
    11971204        /* Read the length */
    1198         if (async_obsolete_data_read_start(socket->phone, optlen,
     1205        if (async_data_read_start(exch, optlen,
    11991206            sizeof(*optlen)) == EOK) {
    12001207                /* 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);
    12031212
    12041213        fibril_rwlock_read_unlock(&socket_globals.lock);
Note: See TracChangeset for help on using the changeset viewer.