Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
TServerSocket.cxx
Go to the documentation of this file.
1// @(#)root/net:$Id$
2// Author: Fons Rademakers 18/12/96
3
4/*************************************************************************
5 * Copyright (C) 1995-2000, 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\file TServerSocket.cxx
14\class TServerSocket
15\brief This class implements server sockets.
16\note This class deals with sockets: the user is entirely responsible for the security of their usage, for example, but
17not limited to, the management of the connections to said sockets.
18
19This class implements server sockets. A server socket waits for
20requests to come in over the network. It performs some operation
21based on that request and then possibly returns a full duplex socket
22to the requester. The actual work is done via the TSystem class
23(either TUnixSystem or TWinNTSystem).
24
25**/
26
27
28#include "TServerSocket.h"
29#include "TSocket.h"
30#include "TSystem.h"
31#include "TROOT.h"
32#include "TError.h"
33#include <string>
34
35////////////////////////////////////////////////////////////////////////////////
36/// Create a server socket object for a named service. Set reuse to true
37/// to force reuse of the server socket (i.e. do not wait for the time
38/// out to pass). Using backlog one can set the desirable queue length
39/// for pending connections.
40/// Use tcpwindowsize to specify the size of the receive buffer, it has
41/// to be specified here to make sure the window scale option is set (for
42/// tcpwindowsize > 65KB and for platforms supporting window scaling).
43/// The socketBindOption parameter allows to specify how the socket will be
44/// bound. See the documentation of ESocketBindOption for the details.
45/// Use IsValid() to check the validity of the
46/// server socket. In case server socket is not valid use GetErrorCode()
47/// to obtain the specific error value. These values are:
48/// 0 = no error (socket is valid)
49/// -1 = low level socket() call failed
50/// -2 = low level bind() call failed
51/// -3 = low level listen() call failed
52/// Every valid server socket is added to the TROOT sockets list which
53/// will make sure that any open sockets are properly closed on
54/// program termination.
55
56TServerSocket::TServerSocket(const char *service, Bool_t reuse, Int_t backlog,
57 Int_t tcpwindowsize)
58{
61
62 SetName("ServerSocket");
63
64 // If this is a local path, try announcing a UNIX socket service
66 if (service && (!gSystem->AccessPathName(service) ||
67#ifndef WIN32
68 service[0] == '/')) {
69#else
70 service[0] == '/' || (service[1] == ':' && service[2] == '/'))) {
71#endif
73 fService = "unix:";
74 fService += service;
75 fSocket = gSystem->AnnounceUnixService(service, backlog);
76 if (fSocket >= 0) {
78 gROOT->GetListOfSockets()->Add(this);
79 }
80 } else {
81 // TCP / UDP socket
82 fService = service;
83 int port = gSystem->GetServiceByName(service);
84 if (port != -1) {
85 fSocket = gSystem->AnnounceTcpService(port, reuse, backlog, tcpwindowsize, ESocketBindOption::kInaddrLoopback);
86 if (fSocket >= 0) {
88 gROOT->GetListOfSockets()->Add(this);
89 }
90 } else {
91 fSocket = -1;
92 }
93 }
94}
95
96////////////////////////////////////////////////////////////////////////////////
97/// Create a server socket object on a specified port. Set reuse to true
98/// to force reuse of the server socket (i.e. do not wait for the time
99/// out to pass). Using backlog one can set the desirable queue length
100/// for pending connections. If port is 0 a port scan will be done to
101/// find a free port. This option is mutual exlusive with the reuse option.
102/// Use tcpwindowsize to specify the size of the receive buffer, it has
103/// to be specified here to make sure the window scale option is set (for
104/// tcpwindowsize > 65KB and for platforms supporting window scaling).
105/// The socketBindOption parameter allows to specify how the socket will be
106/// bound. See the documentation of ESocketBindOption for the details.
107/// Use IsValid() to check the validity of the
108/// server socket. In case server socket is not valid use GetErrorCode()
109/// to obtain the specific error value. These values are:
110/// 0 = no error (socket is valid)
111/// -1 = low level socket() call failed
112/// -2 = low level bind() call failed
113/// -3 = low level listen() call failed
114/// Every valid server socket is added to the TROOT sockets list which
115/// will make sure that any open sockets are properly closed on
116/// program termination.
117
118TServerSocket::TServerSocket(Int_t port, Bool_t reuse, Int_t backlog, Int_t tcpwindowsize,
119 ESocketBindOption socketBindOption)
120{
123
124 SetName("ServerSocket");
125
126 fService = gSystem->GetServiceByPort(port);
128
129 fSocket = gSystem->AnnounceTcpService(port, reuse, backlog, tcpwindowsize, socketBindOption);
130 if (fSocket >= 0) {
132 gROOT->GetListOfSockets()->Add(this);
133 }
134}
135
136////////////////////////////////////////////////////////////////////////////////
137/// Destructor: close connection
138
143
144////////////////////////////////////////////////////////////////////////////////
145/// Accept a connection on a server socket. Returns a full-duplex
146/// communication TSocket object. If no pending connections are
147/// present on the queue and nonblocking mode has not been enabled
148/// with SetOption(kNoBlock,1) the call blocks until a connection is
149/// present. The returned socket must be deleted by the user. The socket
150/// is also added to the TROOT sockets list which will make sure that
151/// any open sockets are properly closed on program termination.
152/// In case of error 0 is returned and in case non-blocking I/O is
153/// enabled and no connections are available -1 is returned.
154/// Note: opt used to pass authentication options but is currently unused.
155
157{
158 if (fSocket == -1) { return 0; }
159
160 TSocket *socket = new TSocket;
161
162 Int_t soc = gSystem->AcceptConnection(fSocket);
163 if (soc == -1) { delete socket; return 0; }
164 if (soc == -2) { delete socket; return (TSocket*) -1; }
165
166 socket->fSocket = soc;
167 socket->fService = fService;
169 socket->fAddress = gSystem->GetPeerName(socket->fSocket);
170 if (socket->fSocket >= 0) {
172 gROOT->GetListOfSockets()->Add(socket);
173 }
174
175 return socket;
176}
177
178////////////////////////////////////////////////////////////////////////////////
179/// Return internet address of host to which the server socket is bound,
180/// i.e. the local host. In case of error TInetAddress::IsValid() returns
181/// kFALSE.
182
184{
185 if (fSocket != -1) {
186 if (fAddress.GetPort() == -1)
187 fAddress = gSystem->GetSockName(fSocket);
188 return fAddress;
189 }
190 return TInetAddress();
191}
192
193////////////////////////////////////////////////////////////////////////////////
194/// Get port # to which server socket is bound. In case of error returns -1.
195
197{
198 if (fSocket != -1) {
199 if (fAddress.GetPort() == -1)
201 return fAddress.GetPort();
202 }
203 return -1;
204}
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
unsigned char UChar_t
Unsigned Character 1 byte (unsigned char).
Definition RtypesCore.h:52
bool Bool_t
Boolean (0=false, 1=true) (bool).
Definition RtypesCore.h:77
#define R__ASSERT(e)
Checks condition e and reports a fatal error if it's false.
Definition TError.h:125
#define gROOT
Definition TROOT.h:417
externTVirtualMutex * gROOTMutex
Definition TROOT.h:63
ESocketBindOption
Options for binging the sockets created.
Definition TSystem.h:46
@ kInaddrLoopback
Refers to the local host via the loopback device.
Definition TSystem.h:48
externTSystem * gSystem
Definition TSystem.h:582
#define R__LOCKGUARD(mutex)
This class represents an Internet Protocol (IP) address.
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition TNamed.cxx:173
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition TNamed.cxx:149
Bool_t TestBit(UInt_t f) const
Definition TObject.h:204
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:888
void ResetBit(UInt_t f)
Definition TObject.h:203
Int_t GetLocalPort() override
Get port # to which server socket is bound. In case of error returns -1.
virtual ~TServerSocket()
Destructor: close connection.
TServerSocket()=default
virtual TSocket * Accept(UChar_t opt=0)
Accept a connection on a server socket.
TInetAddress GetLocalInetAddress() override
Return internet address of host to which the server socket is bound, i.e.
TInetAddress fAddress
Definition TSocket.h:63
Int_t fSocket
Definition TSocket.h:71
TSocket()
Definition TSocket.h:85
TString fService
Definition TSocket.h:69
@ kIsUnix
Definition TSocket.h:44
virtual void Close(Option_t *opt="")
Close the socket.
Definition TSocket.cxx:378