Logo ROOT   6.10/09
Reference Guide
TPServerSocket.cxx
Go to the documentation of this file.
1 // @(#)root/net:$Id$
2 // Author: Fons Rademakers 19/1/2001
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2001, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 //////////////////////////////////////////////////////////////////////////
13 // //
14 // TPServerSocket //
15 // //
16 // This class implements parallel server sockets. A parallel server //
17 // socket waits for requests to come in over the network. It performs //
18 // some operation based on that request and then possibly returns a //
19 // full duplex parallel socket to the requester. The actual work is //
20 // done via the TSystem class (either TUnixSystem or TWinNTSystem). //
21 // //
22 //////////////////////////////////////////////////////////////////////////
23 
24 #include "TPServerSocket.h"
25 #include "TPSocket.h"
26 #include "TROOT.h"
27 #include "TVirtualMutex.h"
28 
30 
31 ////////////////////////////////////////////////////////////////////////////////
32 /// Create a parallel server socket object on a specified port. Set reuse
33 /// to true to force reuse of the server socket (i.e. do not wait for the
34 /// time out to pass). Using backlog one can set the desirable queue length
35 /// for pending connections.
36 /// Use tcpwindowsize to specify the size of the receive buffer, it has
37 /// to be specified here to make sure the window scale option is set (for
38 /// tcpwindowsize > 65KB and for platforms supporting window scaling).
39 /// Use IsValid() to check the validity of the
40 /// server socket. In case server socket is not valid use GetErrorCode()
41 /// to obtain the specific error value. These values are:
42 /// 0 = no error (socket is valid)
43 /// -1 = low level socket() call failed
44 /// -2 = low level bind() call failed
45 /// -3 = low level listen() call failed
46 /// Every valid server socket is added to the TROOT sockets list which
47 /// will make sure that any open sockets are properly closed on
48 /// program termination.
49 
51  Int_t tcpwindowsize) :
52  TServerSocket(port, reuse, backlog, tcpwindowsize)
53 {
54  fTcpWindowSize = tcpwindowsize;
55  SetName("PServerSocket");
56 }
57 
58 ////////////////////////////////////////////////////////////////////////////////
59 /// Create a parallel server socket object for a named service. Set reuse
60 /// to true to force reuse of the server socket (i.e. do not wait for the
61 /// time out to pass). Using backlog one can set the desirable queue length
62 /// for pending connections.
63 /// Use tcpwindowsize to specify the size of the receive buffer, it has
64 /// to be specified here to make sure the window scale option is set (for
65 /// tcpwindowsize > 65KB and for platforms supporting window scaling).
66 /// Use IsValid() to check the validity of the
67 /// server socket. In case server socket is not valid use GetErrorCode()
68 /// to obtain the specific error value. These values are:
69 /// 0 = no error (socket is valid)
70 /// -1 = low level socket() call failed
71 /// -2 = low level bind() call failed
72 /// -3 = low level listen() call failed
73 /// Every valid server socket is added to the TROOT sockets list which
74 /// will make sure that any open sockets are properly closed on
75 /// program termination.
76 
77 TPServerSocket::TPServerSocket(const char *service, Bool_t reuse, Int_t backlog,
78  Int_t tcpwindowsize) :
79  TServerSocket(service, reuse, backlog, tcpwindowsize)
80 {
81  fTcpWindowSize = tcpwindowsize;
82  SetName("PServerSocket");
83 }
84 
85 ////////////////////////////////////////////////////////////////////////////////
86 /// Accept a connection on a parallel server socket. Returns a full-duplex
87 /// parallel communication TPSocket object. If no pending connections are
88 /// present on the queue and nonblocking mode has not been enabled
89 /// with SetOption(kNoBlock,1) the call blocks until a connection is
90 /// present. The returned socket must be deleted by the user. The socket
91 /// is also added to the TROOT sockets list which will make sure that
92 /// any open sockets are properly closed on program termination.
93 /// In case of error 0 is returned and in case non-blocking I/O is
94 /// enabled and no connections are available -1 is returned.
95 
97 {
98  TSocket *setupSocket = 0;
99  TSocket **pSockets;
100  TPSocket *newPSocket = 0;
101 
102  Int_t size, port;
103 
104  // wait for the incoming connections to the server and accept them
105  setupSocket = TServerSocket::Accept(Opt);
106 
107  if (setupSocket == 0) return 0;
108 
109  // receive the port number and number of parallel sockets from the
110  // client and establish 'n' connections
111  if (setupSocket->Recv(port, size) < 0) {
112  Error("Accept", "error receiving port number and number of sockets");
113  return 0;
114  }
115 
116  // Check if client is running in single mode
117  if (size == 0) {
118  pSockets = new TSocket*[1];
119 
120  pSockets[0] = setupSocket;
121 
122  // create TPSocket object with the original socket
123  newPSocket = new TPSocket(pSockets, 1);
124 
125  } else {
126  pSockets = new TSocket*[size];
127 
128  for (int i = 0; i < size; i++) {
129  pSockets[i] = new TSocket(setupSocket->GetInetAddress(),
130  port, fTcpWindowSize);
132  gROOT->GetListOfSockets()->Remove(pSockets[i]);
133  }
134 
135  // create TPSocket object with all the accepted sockets
136  newPSocket = new TPSocket(pSockets, size);
137 
138  }
139 
140  // Transmit authentication information, if any
141  if (setupSocket->IsAuthenticated())
142  newPSocket->SetSecContext(setupSocket->GetSecContext());
143 
144  // clean up, if needed
145  if (size > 0)
146  delete setupSocket;
147 
148  // return the TSocket object
149  return newPSocket;
150 }
151 
virtual Int_t Recv(TMessage *&mess)
Receive a TMessage object.
Definition: TSocket.cxx:818
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition: TNamed.cxx:131
TPServerSocket(const TPServerSocket &)
#define gROOT
Definition: TROOT.h:375
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
R__EXTERN TVirtualMutex * gROOTMutex
Definition: TROOT.h:57
void SetSecContext(TSecContext *ctx)
Definition: TSocket.h:166
virtual TSocket * Accept(UChar_t Opt=0)
Accept a connection on a server socket.
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:873
TSocket()
Definition: TSocket.h:98
#define R__LOCKGUARD2(mutex)
#define ClassImp(name)
Definition: Rtypes.h:336
virtual Bool_t IsAuthenticated() const
Definition: TSocket.h:145
TSecContext * GetSecContext() const
Definition: TSocket.h:141
TInetAddress GetInetAddress() const
Definition: TSocket.h:127
unsigned char UChar_t
Definition: RtypesCore.h:34
virtual TSocket * Accept(UChar_t Opt=kSrvNoAuth)
Accept a connection on a parallel server socket.
gr SetName("gr")