Commit 8eaf8844 authored by Daniel Stenberg's avatar Daniel Stenberg
Browse files

multi: introduce sh_getentry() for looking up sockets in the sockhash

Simplify the code by using a single entry that looks for a socket in the
socket hash. As indicated in #712, the code looked for CURL_SOCKET_BAD
at some point and that is ineffective/wrong and this makes it easier to
avoid that.
parent c0717a70
Loading
Loading
Loading
Loading
+20 −22
Original line number Diff line number Diff line
@@ -172,13 +172,22 @@ struct Curl_sh_entry {
#define SH_READ  1
#define SH_WRITE 2

/* look up a given socket in the socket hash, skip invalid sockets */
static struct Curl_sh_entry *sh_getentry(struct curl_hash *sh,
                                         curl_socket_t s)
{
  if(s != CURL_SOCKET_BAD)
    /* only look for proper sockets */
    return Curl_hash_pick(sh, (char *)&s, sizeof(curl_socket_t));
  return NULL;
}

/* make sure this socket is present in the hash for this handle */
static struct Curl_sh_entry *sh_addentry(struct curl_hash *sh,
                                         curl_socket_t s,
                                         struct SessionHandle *data)
{
  struct Curl_sh_entry *there =
    Curl_hash_pick(sh, (char *)&s, sizeof(curl_socket_t));
  struct Curl_sh_entry *there = sh_getentry(sh, s);
  struct Curl_sh_entry *check;

  if(there)
@@ -206,16 +215,10 @@ static struct Curl_sh_entry *sh_addentry(struct curl_hash *sh,
/* delete the given socket + handle from the hash */
static void sh_delentry(struct curl_hash *sh, curl_socket_t s)
{
  struct Curl_sh_entry *there =
    Curl_hash_pick(sh, (char *)&s, sizeof(curl_socket_t));

  if(there) {
    /* this socket is in the hash */
    /* We remove the hash entry. (This'll end up in a call to
       sh_freeentry().) */
  /* We remove the hash entry. This will end up in a call to
     sh_freeentry(). */
  Curl_hash_delete(sh, (char *)&s, sizeof(curl_socket_t));
}
}

/*
 * free a sockhash entry
@@ -2019,7 +2022,7 @@ static void singlesocket(struct Curl_multi *multi,
    s = socks[i];

    /* get it from the hash */
    entry = Curl_hash_pick(&multi->sockhash, (char *)&s, sizeof(s));
    entry = sh_getentry(&multi->sockhash, s);

    if(curraction & GETSOCK_READSOCK(i))
      action |= CURL_POLL_IN;
@@ -2070,7 +2073,7 @@ static void singlesocket(struct Curl_multi *multi,
      /* this socket has been removed. Tell the app to remove it */
      remove_sock_from_hash = TRUE;

      entry = Curl_hash_pick(&multi->sockhash, (char *)&s, sizeof(s));
      entry = sh_getentry(&multi->sockhash, s);
      if(entry) {
        /* check if the socket to be removed serves a connection which has
           other easy-s in a pipeline. In this case the socket should not be
@@ -2151,8 +2154,7 @@ void Curl_multi_closed(struct connectdata *conn, curl_socket_t s)
  if(multi) {
    /* this is set if this connection is part of a handle that is added to
       a multi handle, and only then this is necessary */
    struct Curl_sh_entry *entry =
      Curl_hash_pick(&multi->sockhash, (char *)&s, sizeof(s));
    struct Curl_sh_entry *entry = sh_getentry(&multi->sockhash, s);

    if(entry) {
      if(multi->socket_cb)
@@ -2253,8 +2255,7 @@ static CURLMcode multi_socket(struct Curl_multi *multi,
  }
  else if(s != CURL_SOCKET_TIMEOUT) {

    struct Curl_sh_entry *entry =
      Curl_hash_pick(&multi->sockhash, (char *)&s, sizeof(s));
    struct Curl_sh_entry *entry = sh_getentry(&multi->sockhash, s);

    if(!entry)
      /* Unmatched socket, we can't act on it but we ignore this fact.  In
@@ -2741,9 +2742,7 @@ CURLMcode curl_multi_assign(CURLM *multi_handle,
  struct Curl_sh_entry *there = NULL;
  struct Curl_multi *multi = (struct Curl_multi *)multi_handle;

  if(s != CURL_SOCKET_BAD)
    there = Curl_hash_pick(&multi->sockhash, (char *)&s,
                           sizeof(curl_socket_t));
  there = sh_getentry(&multi->sockhash, s);

  if(!there)
    return CURLM_BAD_SOCKET;
@@ -2821,8 +2820,7 @@ void Curl_multi_dump(const struct Curl_multi *multi_handle)
              statename[data->mstate], data->numsocks);
      for(i=0; i < data->numsocks; i++) {
        curl_socket_t s = data->sockets[i];
        struct Curl_sh_entry *entry =
          Curl_hash_pick(&multi->sockhash, (char *)&s, sizeof(s));
        struct Curl_sh_entry *entry = sh_getentry(&multi->sockhash, s);

        fprintf(stderr, "%d ", (int)s);
        if(!entry) {