Back to index

im-sdk  12.3.91
Public Member Functions | Private Member Functions | Private Attributes
IMTransListen Class Reference

#include <IMTrans.hh>

List of all members.

Public Member Functions

 IMTransListen (CompoundString im_address, CompoundString protocol_name="", int network=1)
void IMTransListenINET (CompoundString im_address, CompoundString protocol_name="")
 ~IMTransListen ()
IMTransAcceptaccept ()
int connection_id ()

Private Member Functions

int check_fd_resource_limit (int fd)

Private Attributes

int listen_socket
int cur_maxfd
int incr_fd
int fd_resource_limit

Detailed Description

Definition at line 58 of file IMTrans.hh.


Constructor & Destructor Documentation

IMTransListen::IMTransListen ( CompoundString  im_address,
CompoundString  protocol_name = "",
int  network = 1 
)

Definition at line 59 of file IMTransP.cpp.

                   {
  struct rlimit glp;
  char  *err_msg;

  if (getrlimit(RLIMIT_NOFILE, &glp) < 0) {
    err_msg = new char[BUFSIZ];
    snprintf(err_msg, BUFSIZ -1, "Error in getrlimit \n");
    throw err_msg;
  }

  cur_maxfd = glp.rlim_cur; /* initial value */
  incr_fd = glp.rlim_cur;   /* initial value */
  fd_resource_limit = glp.rlim_max; /* initial value */

  if(network) {
    IMTransListenINET(im_address, protocol_name);
  } else {
#ifdef linux
    IMTransListenUNIX(im_address, protocol_name);
#endif /* linux */
  }
}

Here is the call graph for this function:

Definition at line 202 of file IMTransP.cpp.

Here is the call graph for this function:


Member Function Documentation

Definition at line 207 of file IMTransP.cpp.

                      {
  int ss;
  struct sockaddr_in addr_in;
  socklen_t addrlen = sizeof(addr_in);

LOOP:
  if ((ss = ::accept(listen_socket,
                   (struct sockaddr*)&addr_in, &addrlen)) != -1) {
    if ((check_fd_resource_limit(ss)) != -1) {
      IMTransAccept *client = new IMTransAccept(ss);
      return client;
    }
  }
  if (errno != EINTR)
    throw "Can't accept requests";
  else
    goto LOOP;       // has been interrupted. accept again
}

Here is the call graph for this function:

Here is the caller graph for this function:

int IMTransListen::check_fd_resource_limit ( int  fd) [private]

Definition at line 227 of file IMTransP.cpp.

                                             {
  char *err_msg;

  if (fd < (cur_maxfd - 1)) 
    return fd;

  if (fd >= (cur_maxfd - 1)) {
      struct rlimit slp;
      if (cur_maxfd == fd_resource_limit) 
        return -1;
      cur_maxfd += incr_fd;
      if (cur_maxfd > fd_resource_limit) 
        cur_maxfd = fd_resource_limit;
      slp.rlim_cur = cur_maxfd;
      slp.rlim_max = fd_resource_limit;
      if (setrlimit(RLIMIT_NOFILE, (const struct rlimit*)&slp) < 0) {
        err_msg = new char[BUFSIZ];
        snprintf(err_msg, BUFSIZ -1, "Error in setrlimit \n");
        throw err_msg;
      }
  }
  return fd;
}

Here is the caller graph for this function:

int IMTransListen::connection_id ( ) [inline]

Definition at line 69 of file IMTrans.hh.

{ return listen_socket; }
void IMTransListen::IMTransListenINET ( CompoundString  im_address,
CompoundString  protocol_name = "" 
)

Definition at line 149 of file IMTransP.cpp.

                                                                                        {
  struct sockaddr_in sin; // get a listening socket
  struct servent *sp;
  char  *err_msg;
  listen_socket = socket(AF_INET, SOCK_STREAM, 0);
  if (listen_socket == -1) {
    err_msg = new char[BUFSIZ];
    snprintf(err_msg, BUFSIZ -1, "cannot open socket for %s", (char*)protocol_name); 
    throw err_msg;
  }

  char *p;
  char *port_number;

  if (im_address && (p = (char *)index(im_address, ':'))) {
    port_number = ++p;
    sin.sin_port = htons((short)atoi(port_number));
  } else if ((sp = getservbyname("IIIMP", "tcp")) != 0) {
    sin.sin_port = sp->s_port;
  } else {
    sin.sin_port = htons((short)9010);    // default
  }

  sin.sin_family = AF_INET;
  sin.sin_addr.s_addr = htonl(INADDR_ANY);

  const char reuse = 1;
  setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR,
            (const char*)&reuse, sizeof(int));

  if (bind(listen_socket, (struct sockaddr*) &sin, sizeof(sin)) != 0) {
    shutdown(listen_socket, 2);
    err_msg = new char[BUFSIZ];
    snprintf(err_msg, BUFSIZ -1, "Another IIIMP Server might be running.");
    throw err_msg;
  }

  socklen_t len = sizeof sin;
  if (getsockname(listen_socket, (struct sockaddr*) &sin, &len) == -1) {
    err_msg = new char[BUFSIZ];
    snprintf(err_msg, BUFSIZ -1, "Can't get socket name");
    throw err_msg;
  }

  if (listen(listen_socket, 5) < 0) {
    shutdown(listen_socket, 2);
    err_msg = new char[BUFSIZ];
    snprintf(err_msg, BUFSIZ -1, "Can't listen to the socket");
    throw err_msg;
  }
    return;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

int IMTransListen::cur_maxfd [private]

Definition at line 73 of file IMTrans.hh.

Definition at line 75 of file IMTrans.hh.

int IMTransListen::incr_fd [private]

Definition at line 74 of file IMTrans.hh.

Definition at line 72 of file IMTrans.hh.


The documentation for this class was generated from the following files: