예제 #1
0
int
ACE_TLI_Connector::complete (ACE_TLI_Stream &new_stream,
                             ACE_Addr *remote_sap,
                             ACE_Time_Value *tv)
{
  ACE_TRACE ("ACE_TLI_Connector::complete");
#if defined (ACE_WIN32)
   if (WaitForSingleObject (new_stream.get_handle(), tv->msec()) == WAIT_OBJECT_0)
    {
      if (ACE_OS::t_look (new_stream.get_handle()) == T_CONNECT)
        return t_rcvconnect (new_stream.get_handle(), 0);
      else
        return -1;
    }
  else
    return -1;
#else
  ACE_HANDLE h = ACE::handle_timed_complete (new_stream.get_handle (),
                                             tv,
                                             1);
  if (h == ACE_INVALID_HANDLE)
    {
      new_stream.close ();
      return -1;
    }
  else    // We've successfully connected!
    {
      if (remote_sap != 0)
        {
#if defined (ACE_HAS_XTI) || defined (ACE_HAS_SVR4_TLI)
          struct netbuf name;

          name.maxlen = remote_sap->get_size ();
          name.buf    = (char *) remote_sap->get_addr ();

          if (ACE_OS::t_getname (new_stream.get_handle (),
                                 &name,
                                 REMOTENAME) == -1)
#else /* SunOS4 */
          if (0)
#endif /* ACE_HAS_XTI || ACE_HAS_SVR4_TLI */
            {
              new_stream.close ();
              return -1;
            }
        }

      // Start out with non-blocking disabled on the <new_stream>.
      new_stream.disable (ACE_NONBLOCK);

      return 0;
    }
#endif /* ACE_WIN32 */
}
예제 #2
0
파일: ftp-client.cpp 프로젝트: CCJY/ACE
int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{
  if (argc < 2)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("Usage: %s filename ")
                       ACE_TEXT ("[server-host port-number]\n"),
                       argv[0]),
                      1);

  const ACE_TCHAR *filename  = argv[1];
  const ACE_TCHAR *host_name = argc < 3 ? ACE_DEFAULT_SERVER_HOST : argv[2];
  u_short port = argc < 4 ? ACE_DEFAULT_SERVER_PORT : ACE_OS::atoi (argv[3]);

  ACE_TLI_Stream client;
  ACE_TLI_Connector con;
  int fd;
  char buf[BUFSIZ];

  if (con.connect (client, ACE_INET_Addr (port, host_name)) == -1)
    ACE_OS::t_error ((char *) host_name), ACE_OS::exit (1);

  if ((fd = ACE_OS::open (filename, O_RDONLY)) == -1)
    ACE_OS::perror (filename), ACE_OS::exit (1);

  for (int n; (n = ACE_OS::read (fd, buf, sizeof buf)) > 0; )
    if (client.send_n (buf, n) != n)
      ACE_OS::t_error ("client.send error");

  if (client.close () == -1)
    ACE_OS::t_error ("cli_close"), ACE_OS::exit (1);

  return 0;
}
예제 #3
0
void *
read_file (void *fd)
{
  ACE_TLI_Stream stream;
  char buf[BUFSIZ];
  int flags = 0;
  ssize_t n;

  // Cast the arg to a long, first, because a pointer is the same size
  // as a long on all current ACE platforms.
  stream.set_handle ((int) (long) fd);

  ACE_DEBUG((LM_DEBUG, "start (tid = %t, fd = %d)\n",
             stream.get_handle ()));

  while ((n = stream.recv (buf, sizeof buf, &flags)) > 0)
    continue;

  ACE_UNUSED_ARG (n);

  ACE_DEBUG ((LM_DEBUG,"finish (tid = %t, fd = %d)\n",
              stream.get_handle ()));

  if (stream.close () == -1)
    ACE_OS::t_error ("stream.close error");

  return 0;
}
int
ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{
  parse_args (argc, argv);
  void *cp;
  char buf[BUFSIZ];
  ACE_TLI_Stream sc;
  ACE_TLI_Connector con;

  if (con.connect (sc,
                   ACE_INET_Addr (port_number,
                                  host_name)) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "open"),
                      -1);

  ACE_Mem_Map mmap (file_name, PROT_READ);

  if (mmap (cp) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",

                       "mmap"), -1);

  // Next, send the file's contents.

  if (sc.send_n (cp, mmap.size ()) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",

                       "send_n"), -1);

  if (sc.sndrel () == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "close_writer"),
                      -1);

  for (int n; (n = sc.recv (buf, sizeof buf)) > 0; )
    if (ACE_OS::write (ACE_STDOUT, buf, n) != n)
      ACE_ERROR_RETURN ((LM_ERROR,
                         "%p\n",
                         "write"),
                        -1);

  if (sc.close () == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "close"),
                      -1);
  return 0;
}
예제 #5
0
int
ACE_TLI_Connector::connect (ACE_TLI_Stream &new_stream,
                            const ACE_Addr &remote_sap,
                            ACE_Time_Value *timeout,
                            const ACE_Addr &local_sap,
                            int reuse_addr,
                            int flags,
                            int /* perms */,
                            const char device[],
                            struct t_info *info,
                            int rwf,
                            struct netbuf *udata,
                            struct netbuf *opt)
{
  ACE_TRACE ("ACE_TLI_Connector::connect");
  int result = 0;

  // Only open a new endpoint if we don't already have a valid handle.

  if (new_stream.get_handle () == ACE_INVALID_HANDLE
      && new_stream.open (device, flags, info) == ACE_INVALID_HANDLE)
    return -1;

  if (local_sap != ACE_Addr::sap_any)
    {
      // Bind the local endpoint to a specific addr.

      struct t_bind *localaddr;

      localaddr = (struct t_bind *)
        ACE_OS::t_alloc (new_stream.get_handle (), T_BIND, T_ADDR);

      if (localaddr == 0)
        result = -1;
      else
        {
          int one = 1;
#if !defined (ACE_HAS_FORE_ATM_XTI)
          // Reusing the address causes problems with FORE's API. The
          // issue may be that t_optmgmt isn't fully supported by
          // FORE. t_errno is TBADOPT after the t_optmgmt call so
          // maybe options are configured differently for XTI than for
          // TLI (at least for FORE's implementation - XTI is supposed
          // to be a superset of TLI).
          if (reuse_addr
              && new_stream.set_option (SOL_SOCKET,
                                        SO_REUSEADDR,
                                        &one,
                                        sizeof one) == -1)
            result = -1;
          else
#endif /* ACE_HAS_FORE_ATM_XTI */
            {
              void *addr_buf = local_sap.get_addr ();
              localaddr->addr.len = local_sap.get_size ();
              ACE_OS::memcpy(localaddr->addr.buf,
                             addr_buf,
                             localaddr->addr.len);

              if (ACE_OS::t_bind (new_stream.get_handle (),
                                  localaddr,
                                  localaddr) == -1)
                result = -1;

              ACE_OS::t_free ((char *) localaddr,
                              T_BIND);
            }
        }

      if (result == -1)
        {
          new_stream.close ();
          return -1;
        }
    }
  // Let TLI select the local endpoint addr.
  else if (ACE_OS::t_bind (new_stream.get_handle (), 0, 0) == -1)
    return -1;

  struct t_call *callptr = 0;

  callptr = (struct t_call *)
    ACE_OS::t_alloc (new_stream.get_handle (), T_CALL, T_ADDR);

  if (callptr == 0)
    {
      new_stream.close ();
      return -1;
    }

  void *addr_buf = remote_sap.get_addr ();
  callptr->addr.len = remote_sap.get_size ();
  ACE_OS::memcpy (callptr->addr.buf,
                  addr_buf,
                  callptr->addr.len);
  //callptr->addr.buf = (char *) remote_sap.get_addr ();

  if (udata != 0)
    ACE_OS::memcpy ((void *) &callptr->udata, (void *) udata, sizeof *udata);
  if (opt != 0)
    ACE_OS::memcpy ((void *) &callptr->opt, (void *) opt, sizeof *opt);

  // Connect to remote endpoint.
#if defined (ACE_HAS_FORE_ATM_XTI)
  // FORE's XTI/ATM driver has problems with ioctl/fcntl calls so (at least
  // for now) always have blocking calls.
  timeout = 0;
#endif /* ACE_HAS_FORE_ATM_XTI */

  if (timeout != 0)   // Enable non-blocking, if required.
    {
      if (new_stream.enable (ACE_NONBLOCK) == -1)
        result = -1;

      // Do a non-blocking connect.
      if (ACE_OS::t_connect (new_stream.get_handle (), callptr, 0) == -1)
        {
          result = -1;

          // Check to see if we simply haven't connected yet on a
          // non-blocking handle or whether there's really an error.
          if (t_errno == TNODATA)
            {
              if (*timeout == ACE_Time_Value::zero)
                errno = EWOULDBLOCK;
              else
                result = this->complete (new_stream, 0, timeout);
            }
          else if (t_errno == TLOOK && new_stream.look () == T_DISCONNECT)
            new_stream.rcvdis ();
        }
    }
  // Do a blocking connect to the server.
  else if (ACE_OS::t_connect (new_stream.get_handle (), callptr, 0) == -1)
    result = -1;

  if (result != -1)
    {
      new_stream.set_rwflag (rwf);
#if defined (I_PUSH) && !defined (ACE_HAS_FORE_ATM_XTI)
      if (new_stream.get_rwflag ())
        result = ACE_OS::ioctl (new_stream.get_handle (),
                                I_PUSH,
                                const_cast<char *> ("tirdwr"));
#endif /* I_PUSH */
    }
  else if (!(errno == EWOULDBLOCK || errno == ETIME))
    {
      // If things have gone wrong, close down and return an error.
      new_stream.close ();
      new_stream.set_handle (ACE_INVALID_HANDLE);
    }

  if (ACE_OS::t_free ((char *) callptr, T_CALL) == -1)
    return -1;
  return result;
}
예제 #6
0
void *
lookup_name (ACE_HANDLE handle)
{
  enum
  {
    MAXLINE = 255,
    EMPNAMELEN = 512
  };

  static struct
  {
    int emp_id;
    const char *emp_name;
  } employee_db[] =
    {
      {123, "John Wayne Bobbit"},
      {124, "Woody Allen"},
      {125, "O. J. Simpson"},
      {126, "Bill Clinton"},
      {127, "Rush Limbaugh"},
      {128, "Michael Jackson"},
      {129, "Kenneth Starr"},
      {130, "Paula Jones"},
      {131, "Monica Lewinsky"},
      {132, "Marv Albert"},
      {0, ""}
    };

  int flags;
  int employee_id;
  int index;
  int found;
  ACE_TLI_Stream stream;
  char recvline[MAXLINE];
  char sendline[MAXLINE];

  ACE_DEBUG ((LM_DEBUG,
              "stream handle = %d, thread id = %t\n",
              handle));
  stream.set_handle (handle);

  ssize_t n = stream.recv (recvline, MAXLINE, &flags);

  if (n == -1)
    ACE_OS::t_error ("stream.recv error");

  employee_id = ACE_OS::atoi (recvline);
  found = 0;

  for (index = 0; found == 0 && employee_db[index].emp_id; index++)
    if (employee_id == employee_db[index].emp_id)
      {
        found = 1;
        n = ACE_OS::sprintf (sendline,
                             "%s", employee_db[index].emp_name);
      }

  if (found == 0)
    n = ACE_OS::sprintf (sendline, "%s", "ERROR");

  if (stream.send (sendline, n + 1, 0) == -1)
    ACE_OS::t_error ("stream.send error");

  if (stream.sndrel () == -1)
    ACE_OS::t_error ("stream.send error");

  if (stream.close () == -1)
    ACE_OS::t_error ("stream.close error");

  return 0;
}