Ignore:
File:
1 edited

Legend:

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

    r6b82009 r9934f7d  
    4040
    4141#include <async.h>
     42#include <async_obsolete.h>
    4243#include <malloc.h>
    4344#include <errno.h>
     
    4647#include <net/modules.h>
    4748#include <ns.h>
     49#include <ns_obsolete.h>
     50
     51/** The time between connect requests in microseconds. */
     52#define MODULE_WAIT_TIME  (10 * 1000)
    4853
    4954/** Answer a call.
     
    9398}
    9499
    95 /** Create bidirectional connection with the needed module service and register
     100/** Create bidirectional connection with the needed module service and registers
    96101 * the message receiver.
    97102 *
    98  * @param[in] need            Needed module service.
    99  * @param[in] arg1            First parameter.
    100  * @param[in] arg2            Second parameter.
    101  * @param[in] arg3            Third parameter.
    102  * @param[in] client_receiver Message receiver.
    103  *
    104  * @return Session to the needed service.
    105  * @return Other error codes as defined for the async_connect_to_me()
    106  *         function.
    107  *
    108  */
    109 async_sess_t *bind_service(services_t need, sysarg_t arg1, sysarg_t arg2,
    110     sysarg_t arg3, async_client_conn_t client_receiver)
     103 * @param[in] need      The needed module service.
     104 * @param[in] arg1      The first parameter.
     105 * @param[in] arg2      The second parameter.
     106 * @param[in] arg3      The third parameter.
     107 * @param[in] client_receiver The message receiver.
     108 *
     109 * @return              The phone of the needed service.
     110 * @return              Other error codes as defined for the ipc_connect_to_me()
     111 *                      function.
     112 */
     113int bind_service(services_t need, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3,
     114    async_client_conn_t client_receiver)
     115{
     116        return bind_service_timeout(need, arg1, arg2, arg3, client_receiver, 0);
     117}
     118
     119/** Create bidirectional connection with the needed module service and registers
     120 * the message receiver.
     121 *
     122 * @param[in] need      The needed module service.
     123 * @param[in] arg1      The first parameter.
     124 * @param[in] arg2      The second parameter.
     125 * @param[in] arg3      The third parameter.
     126 * @param[in] client_receiver The message receiver.
     127 * @param[in] timeout   The connection timeout in microseconds. No timeout if
     128 *                      set to zero (0).
     129 *
     130 * @return              The phone of the needed service.
     131 * @return              ETIMEOUT if the connection timeouted.
     132 * @return              Other error codes as defined for the ipc_connect_to_me()
     133 *                      function.
     134 *
     135 */
     136int bind_service_timeout(services_t need, sysarg_t arg1, sysarg_t arg2,
     137    sysarg_t arg3, async_client_conn_t client_receiver, suseconds_t timeout)
    111138{
    112139        /* Connect to the needed service */
    113         async_sess_t *sess = connect_to_service(need);
    114         if (sess != NULL) {
     140        int phone = connect_to_service_timeout(need, timeout);
     141        if (phone >= 0) {
    115142                /* Request the bidirectional connection */
    116                 async_exch_t *exch = async_exchange_begin(sess);
    117                 int rc = async_connect_to_me(exch, arg1, arg2, arg3,
     143                int rc = async_obsolete_connect_to_me(phone, arg1, arg2, arg3,
    118144                    client_receiver, NULL);
    119                 async_exchange_end(exch);
    120                
    121145                if (rc != EOK) {
    122                         async_hangup(sess);
    123                         errno = rc;
    124                         return NULL;
     146                        async_obsolete_hangup(phone);
     147                        return rc;
    125148                }
    126149        }
    127150       
    128         return sess;
    129 }
    130 
    131 /** Connect to the needed module.
    132  *
    133  * @param[in] need Needed module service.
    134  *
    135  * @return Session to the needed service.
    136  * @return NULL if the connection timeouted.
    137  *
    138  */
    139 async_sess_t *connect_to_service(services_t need)
    140 {
    141         return service_connect_blocking(EXCHANGE_SERIALIZE, need, 0, 0);
    142 }
    143 
    144 /** Reply the data to the other party.
    145  *
    146  * @param[in] data        The data buffer to be sent.
     151        return phone;
     152}
     153
     154/** Connects to the needed module.
     155 *
     156 * @param[in] need      The needed module service.
     157 * @return              The phone of the needed service.
     158 */
     159int connect_to_service(services_t need)
     160{
     161        return connect_to_service_timeout(need, 0);
     162}
     163
     164/** Connects to the needed module.
     165 *
     166 *  @param[in] need     The needed module service.
     167 *  @param[in] timeout  The connection timeout in microseconds. No timeout if
     168 *                      set to zero (0).
     169 *  @return             The phone of the needed service.
     170 *  @return             ETIMEOUT if the connection timeouted.
     171 */
     172int connect_to_service_timeout(services_t need, suseconds_t timeout)
     173{
     174        int phone;
     175
     176        /* If no timeout is set */
     177        if (timeout <= 0)
     178                return service_obsolete_connect_blocking(need, 0, 0);
     179       
     180        while (true) {
     181                phone = service_obsolete_connect(need, 0, 0);
     182                if ((phone >= 0) || (phone != ENOENT))
     183                        return phone;
     184
     185                /* Abort if no time is left */
     186                if (timeout <= 0)
     187                        return ETIMEOUT;
     188
     189                /* Wait the minimum of the module wait time and the timeout */
     190                usleep((timeout <= MODULE_WAIT_TIME) ?
     191                    timeout : MODULE_WAIT_TIME);
     192                timeout -= MODULE_WAIT_TIME;
     193        }
     194}
     195
     196/** Replies the data to the other party.
     197 *
     198 * @param[in] data      The data buffer to be sent.
    147199 * @param[in] data_length The buffer length.
    148  *
    149  * @return EOK on success.
    150  * @return EINVAL if the client does not expect the data.
    151  * @return EOVERFLOW if the client does not expect all the data.
    152  *         Only partial data are transfered.
    153  * @return Other error codes as defined for the
    154  *         async_data_read_finalize() function.
    155  *
     200 * @return              EOK on success.
     201 * @return              EINVAL if the client does not expect the data.
     202 * @return              EOVERFLOW if the client does not expect all the data.
     203 *                      Only partial data are transfered.
     204 * @return              Other error codes as defined for the
     205 *                      async_data_read_finalize() function.
    156206 */
    157207int data_reply(void *data, size_t data_length)
     
    159209        size_t length;
    160210        ipc_callid_t callid;
    161        
     211
    162212        /* Fetch the request */
    163213        if (!async_data_read_receive(&callid, &length))
    164214                return EINVAL;
    165        
     215
    166216        /* Check the requested data size */
    167217        if (length < data_length) {
     
    169219                return EOVERFLOW;
    170220        }
    171        
     221
    172222        /* Send the data */
    173223        return async_data_read_finalize(callid, data, data_length);
Note: See TracChangeset for help on using the changeset viewer.