XrdNetWork.cc

Go to the documentation of this file.
00001 /******************************************************************************/
00002 /*                                                                            */
00003 /*                         X r d N e t W o r k . c c                          */
00004 /*                                                                            */
00005 /* (c) 2004 by the Board of Trustees of the Leland Stanford, Jr., University  */
00006 /*       All Rights Reserved. See XrdInfo.cc for complete License Terms       */
00007 /*   Produced by Andrew Hanushevsky for Stanford University under contract    */
00008 /*              DE-AC03-76-SFO0515 with the Department of Energy              */
00009 /******************************************************************************/
00010  
00011 //         $Id: XrdNetWork.cc 22437 2008-03-04 14:35:16Z rdm $
00012 
00013 const char *XrdNetWorkCVSID = "$Id: XrdNetWork.cc 22437 2008-03-04 14:35:16Z rdm $";
00014 
00015 #include <errno.h>
00016 #include <fcntl.h>
00017 #ifndef WIN32
00018 #include <poll.h>
00019 #include <stdio.h>
00020 #include <string.h>
00021 #include <unistd.h>
00022 #include <sys/types.h>
00023 #include <sys/socket.h>
00024 #else
00025 #include <stdio.h>
00026 #include <string.h>
00027 #include <Winsock2.h>
00028 #include <io.h>
00029 #endif
00030 
00031 #include "XrdNet/XrdNet.hh"
00032 #include "XrdNet/XrdNetLink.hh"
00033 #include "XrdNet/XrdNetOpts.hh"
00034 #include "XrdNet/XrdNetPeer.hh"
00035 #include "XrdNet/XrdNetWork.hh"
00036 
00037 #include "XrdSys/XrdSysError.hh"
00038 
00039 /******************************************************************************/
00040 /*                                A c c e p t                                 */
00041 /******************************************************************************/
00042 
00043 XrdNetLink *XrdNetWork::Accept(int opts, int timeout)
00044 {
00045    XrdNetPeer myPeer;
00046    XrdNetLink    *lp;
00047    int ismyfd, lnkopts;
00048 
00049 // Perform regular accept
00050 //
00051    if (!XrdNet::Accept(myPeer, opts, timeout)) return (XrdNetLink *)0;
00052    if ((ismyfd = (myPeer.fd == iofd))) lnkopts = XRDNETLINK_NOCLOSE;
00053       else lnkopts = 0;
00054 
00055 // Return a link object
00056 //
00057    if (!(lp = XrdNetLink::Alloc(eDest,(XrdNet *)this,myPeer,BuffQ,lnkopts)))
00058       {if (!ismyfd) close(myPeer.fd);
00059        if (!(opts & XRDNET_NOEMSG))
00060           eDest->Emsg("Connect",ENOMEM,"accept connection from",myPeer.InetName);
00061       } else myPeer.InetBuff = 0; // Keep buffer after object goes away
00062    return lp;
00063 }
00064 
00065 /******************************************************************************/
00066 /*                               C o n n e c t                                */
00067 /******************************************************************************/
00068 
00069 XrdNetLink *XrdNetWork::Connect(const char *host, int port, int opts, int tmo)
00070 {
00071    XrdNetPeer myPeer;
00072    XrdNetLink *lp;
00073 
00074 // Try to do a connect
00075 //
00076    if (!XrdNet::Connect(myPeer, host, port, opts, tmo)) return (XrdNetLink *)0;
00077 
00078 // Return a link object
00079 //
00080    if (!(lp = XrdNetLink::Alloc(eDest, (XrdNet *)this, myPeer, BuffQ)))
00081       {close(myPeer.fd);
00082        if (!(opts & XRDNET_NOEMSG))
00083           eDest->Emsg("Connect", ENOMEM, "connect to", host);
00084       }
00085    return lp;
00086 }
00087 
00088 /******************************************************************************/
00089 /*                                 R e l a y                                  */
00090 /******************************************************************************/
00091   
00092 XrdNetLink *XrdNetWork::Relay(const char *dest, int opts)
00093 {
00094    XrdNetPeer  myPeer;
00095    XrdNetLink *lp;
00096    int         lnkopts;
00097 
00098 // Create a udp socket
00099 //
00100    if (!XrdNet::Connect(myPeer, dest, -1, opts | XRDNET_UDPSOCKET))
00101       return (XrdNetLink *)0;
00102 
00103 // Determine set of options
00104 //
00105    lnkopts = (myPeer.fd == iofd      ? XRDNETLINK_NOCLOSE  : 0)
00106            | (opts & XRDNET_SENDONLY ? XRDNETLINK_NOSTREAM : 0);
00107 
00108 // Associate this socket with a link
00109 //
00110    if (!(lp = XrdNetLink::Alloc(eDest, (XrdNet *)this, myPeer, BuffQ, lnkopts)))
00111       {close(myPeer.fd);
00112        if (!(opts & XRDNET_NOEMSG))
00113           eDest->Emsg("Connect", ENOMEM, "allocate relay to", 
00114                      (dest ? dest : "network"));
00115       }
00116    return lp;
00117 }

Generated on Tue Jul 5 14:46:43 2011 for ROOT_528-00b_version by  doxygen 1.5.1