CPN
Computational Process Networks
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes | List of all members
ServerSocketHandle Class Reference

#include <ServerSocketHandle.h>

+ Inheritance diagram for ServerSocketHandle:
+ Collaboration diagram for ServerSocketHandle:

Public Types

typedef AutoLock< PthreadMutexALock
 

Public Member Functions

 ServerSocketHandle ()
 
 ServerSocketHandle (int nfd)
 
 ServerSocketHandle (const SocketAddress &addr, int queuelength=256)
 
 ServerSocketHandle (const SockAddrList &addrs, int queuelength=256)
 
void Listen (const SocketAddress &addr, int queuelength=256)
 
void Listen (const SockAddrList &addrs, int queuelength=256)
 
int Accept (SocketAddress &addr)
 
int Accept ()
 
void SetReuseAddr (bool reuse=true)
 Turn on reuse of the address. Only takes effect if set before Listen. More...
 
bool GetReuseAddr ()
 
int Poll (double timeout)
 
bool Readable (bool r)
 Set that the file is currently readable or not. More...
 
bool Readable () const
 Gives the current readability status of the file. More...
 
bool Writeable (bool w)
 Set that this file is currently writeable or not. More...
 
bool Writeable () const
 Gives the current writability status of the file. More...
 
int FD () const
 
int FD (int filed)
 Set the current file descriptor. More...
 
bool Eof () const
 
bool Eof (bool e)
 Set/reset the end of file condition. More...
 
bool Good () const
 Convenience method for testing if the file this FileHandle has is open and not at end of file. More...
 
bool Closed () const
 
void SetBlocking (bool blocking)
 Manipulate how the current file handles blocking. More...
 
bool IsBlocking () const
 Test if the current file is in blocking or non blocking mode. More...
 
void Reset ()
 Clear all internal state including the file descriptor! WARNING does not close the file! More...
 
void Close ()
 Close the file and reset the internal state. More...
 
unsigned Read (void *ptr, unsigned len)
 Read data from the file descriptor. More...
 
unsigned Readv (const iovec *iov, int iovcnt)
 scatter gather io version of Read More...
 
unsigned Write (const void *ptr, unsigned len)
 Write data to the file descriptor. More...
 
unsigned Writev (const iovec *iov, int iovcnt)
 scatter gather io version of Write More...
 
void Flush ()
 Tell the OS to flush any buffers it has. May not be supported for all file types. More...
 

Static Public Member Functions

static int Poll (IteratorRef< FileHandle * > begin, IteratorRef< FileHandle * > end, double timeout)
 poll a list of FileHandles for any activity and call the appropriate On method. More...
 
static void SetBlocking (int fd, bool blocking)
 a convenience method that allows one to set the same blocking parameters for a file descriptor without setting it inside a FileHandle. More...
 
static bool IsBlocking (int fd)
 Test the given file descriptor if it is in blocking mode. More...
 

Protected Member Functions

virtual void OnReadable ()
 Called by Poll when it detects that the file is readable. More...
 
virtual void OnWriteable ()
 Called by Poll when it detects that the file is writeable. More...
 

Protected Attributes

PthreadMutex file_lock
 
int fd
 
bool readable
 
bool writeable
 
bool eof
 

Private Member Functions

bool Listen (const SocketAddress &addr, int queuelength, int &error)
 
int Accept (SocketAddress *addr)
 

Private Attributes

bool reuse_addr
 

Detailed Description

Provides functionality for a server socket in a FileHandle

Definition at line 33 of file ServerSocketHandle.h.

Member Typedef Documentation

Definition at line 43 of file FileHandle.h.

Constructor & Destructor Documentation

ServerSocketHandle::ServerSocketHandle ( )
inline

Definition at line 36 of file ServerSocketHandle.h.

36 : reuse_addr(false) {}
ServerSocketHandle::ServerSocketHandle ( int  nfd)
inline

Definition at line 37 of file ServerSocketHandle.h.

37 : FileHandle(nfd), reuse_addr(false) {}
FileHandle()
Construct a closed FileHandle.
Definition: FileHandle.cc:91
ServerSocketHandle::ServerSocketHandle ( const SocketAddress addr,
int  queuelength = 256 
)
inline

Definition at line 38 of file ServerSocketHandle.h.

References Listen().

38  : reuse_addr(false)
39  { Listen(addr, queuelength); }
void Listen(const SocketAddress &addr, int queuelength=256)

+ Here is the call graph for this function:

ServerSocketHandle::ServerSocketHandle ( const SockAddrList addrs,
int  queuelength = 256 
)
inline

Definition at line 40 of file ServerSocketHandle.h.

References Listen().

40  : reuse_addr(false)
41  { Listen(addrs, queuelength); }
void Listen(const SocketAddress &addr, int queuelength=256)

+ Here is the call graph for this function:

Member Function Documentation

int ServerSocketHandle::Accept ( SocketAddress addr)

Accept an incoming connection.

Parameters
addra SocketAddress object to fill with the address of the connecting peer.
Returns
-1 if no connection >=0 on success
Exceptions
ErrnoExceptionfor errors

Definition at line 46 of file ServerSocketHandle.cc.

References Accept().

Referenced by CPN::ConnectionServer::Poll().

46  {
47  return Accept(&addr);
48 }

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int ServerSocketHandle::Accept ( )

Accept an incoming connection

Definition at line 50 of file ServerSocketHandle.cc.

Referenced by Accept(), and CPN::RemoteContextDaemon::Read().

50  {
51  return Accept(0);
52 }

+ Here is the caller graph for this function:

int ServerSocketHandle::Accept ( SocketAddress addr)
private

Definition at line 53 of file ServerSocketHandle.cc.

References FileHandle::FD(), SocketAddress::GetAddr(), SocketAddress::GetLen(), and FileHandle::Readable().

53  {
54  bool loop = true;
55  int nfd = -1;
56  while (loop) {
57  if (addr) {
58  addr->GetLen() = sizeof(sockaddr_storage);
59  nfd = accept(FD(), addr->GetAddr(), &addr->GetLen());
60  } else {
61  nfd = accept(FD(), 0, 0);
62  }
63  if (nfd < 0) {
64  int error = errno;
65  switch (error) {
66  case EAGAIN:
67  //case EWOULDBLOCK: // Duplicate
68  Readable(false);
69  case ECONNABORTED:
70  case EINTR:
71  case ENETDOWN:
72  case EPROTO:
73  case ENOPROTOOPT:
74  case EHOSTDOWN:
75  //case ENONET:
76  case EHOSTUNREACH:
77  case ENETUNREACH:
78  case ENOBUFS:
79  case ENOMEM:
80  case EPERM:
81  case EPIPE:
82  case ETIMEDOUT:
83  nfd = -1;
84  loop = false;
85  break;
86  case EOPNOTSUPP:
87  case EBADF:
88  case EINVAL:
89  case ENFILE:
90  case EMFILE:
91  case ENOTSOCK:
92  case EFAULT:
93  default:
94  throw ErrnoException(error);
95  }
96  } else {
97  loop = false;
98  }
99  }
100  return nfd;
101 }
socklen_t & GetLen()
sockaddr * GetAddr()
int FD() const
Definition: FileHandle.h:106
bool Readable() const
Gives the current readability status of the file.
Definition: FileHandle.h:91

+ Here is the call graph for this function:

void FileHandle::Close ( )
inherited

Close the file and reset the internal state.

Definition at line 146 of file FileHandle.cc.

References FileHandle::eof, FileHandle::fd, FileHandle::file_lock, FileHandle::readable, and FileHandle::writeable.

Referenced by CPN::ConnectionServer::Close(), CPN::RemoteContext::EntryPoint(), CPN::RemoteQueue::HandleError(), CPN::RemoteQueue::InternalCheckStatus(), and CPN::RemoteContextDaemon::Terminate().

146  {
147  ALock al(file_lock);
148  if (fd != -1) {
149  if (close(fd) != 0) {
150  throw ErrnoException();
151  }
152  fd = -1;
153  }
154  readable = false;
155  writeable = false;
156  eof = false;
157 }
bool writeable
Definition: FileHandle.h:216
PthreadMutex file_lock
Definition: FileHandle.h:213
AutoLock< PthreadMutex > ALock
Definition: FileHandle.h:43
bool readable
Definition: FileHandle.h:215

+ Here is the caller graph for this function:

bool FileHandle::Closed ( ) const
inlineinherited
bool FileHandle::Eof ( ) const
inlineinherited
Returns
the current end of file condition

Definition at line 115 of file FileHandle.h.

References FileHandle::eof, and FileHandle::file_lock.

Referenced by CPN::RemoteQueue::EnqueuePacket(), CPN::RemoteContext::EntryPoint(), CPN::RemoteQueue::InternalCheckStatus(), CPN::RemoteContextDaemon::Client::Read(), and CPN::RemoteQueue::Read().

115 { ALock al(file_lock); return eof; }
PthreadMutex file_lock
Definition: FileHandle.h:213
AutoLock< PthreadMutex > ALock
Definition: FileHandle.h:43

+ Here is the caller graph for this function:

bool FileHandle::Eof ( bool  e)
inlineinherited

Set/reset the end of file condition.

Parameters
ethe new end of file condition
Returns
the new end of file condition

Definition at line 120 of file FileHandle.h.

References FileHandle::eof, and FileHandle::file_lock.

120 { ALock al(file_lock); return eof = e; }
PthreadMutex file_lock
Definition: FileHandle.h:213
AutoLock< PthreadMutex > ALock
Definition: FileHandle.h:43
int FileHandle::FD ( ) const
inlineinherited
int FileHandle::FD ( int  filed)
inlineinherited

Set the current file descriptor.

Parameters
filedthe new file descriptor
Returns
the new file descriptor

Definition at line 111 of file FileHandle.h.

References FileHandle::fd, and FileHandle::file_lock.

111 { ALock al(file_lock); return fd = filed; }
PthreadMutex file_lock
Definition: FileHandle.h:213
AutoLock< PthreadMutex > ALock
Definition: FileHandle.h:43
void FileHandle::Flush ( )
inherited

Tell the OS to flush any buffers it has. May not be supported for all file types.

Definition at line 293 of file FileHandle.cc.

References FileHandle::FD().

293  {
294  if (fsync(FD()) != 0) {
295  throw ErrnoException();
296  }
297 }
int FD() const
Definition: FileHandle.h:106

+ Here is the call graph for this function:

bool ServerSocketHandle::GetReuseAddr ( )
inline

Definition at line 71 of file ServerSocketHandle.h.

References reuse_addr.

71 { return reuse_addr; }
bool FileHandle::Good ( ) const
inlineinherited

Convenience method for testing if the file this FileHandle has is open and not at end of file.

Returns
true if open and not at end of file otherwise false

Definition at line 125 of file FileHandle.h.

References FileHandle::eof, FileHandle::fd, and FileHandle::file_lock.

Referenced by CPN::RemoteContext::EntryPoint(), and CPN::RemoteContextDaemon::Client::Read().

125 { ALock al(file_lock); return !(eof || fd == -1); }
PthreadMutex file_lock
Definition: FileHandle.h:213
AutoLock< PthreadMutex > ALock
Definition: FileHandle.h:43

+ Here is the caller graph for this function:

bool FileHandle::IsBlocking ( ) const
inherited

Test if the current file is in blocking or non blocking mode.

Returns
true if blocking or false if non blocking

Definition at line 128 of file FileHandle.cc.

References FileHandle::FD().

128  {
129  return IsBlocking(FD());
130 }
bool IsBlocking() const
Test if the current file is in blocking or non blocking mode.
Definition: FileHandle.cc:128
int FD() const
Definition: FileHandle.h:106

+ Here is the call graph for this function:

bool FileHandle::IsBlocking ( int  fd)
staticinherited

Test the given file descriptor if it is in blocking mode.

Parameters
fdthe file descriptor to test
Returns
true if blocking or false if non blocking

Definition at line 132 of file FileHandle.cc.

132  {
133  int flags = fcntl(fd, F_GETFL, 0);
134  if (-1 == flags) { throw ErrnoException(); }
135  return !(flags & O_NONBLOCK);
136 }
void ServerSocketHandle::Listen ( const SocketAddress addr,
int  queuelength = 256 
)

Open a new socket and try to listen on the given SocketAddress.

Definition at line 30 of file ServerSocketHandle.cc.

Referenced by CPN::ConnectionServer::ConnectionServer(), Listen(), CPN::RemoteContextDaemon::RemoteContextDaemon(), and ServerSocketHandle().

30  {
31  int error = 0;
32  if (!Listen(addr, queuelength, error)) { throw ErrnoException(error); }
33 }
void Listen(const SocketAddress &addr, int queuelength=256)

+ Here is the caller graph for this function:

void ServerSocketHandle::Listen ( const SockAddrList addrs,
int  queuelength = 256 
)

Open a new socket and try to listen on one of the addresses in the given list.

Definition at line 35 of file ServerSocketHandle.cc.

References Listen().

35  {
36  int error = 0;
37  bool success = false;
38  for (SockAddrList::const_iterator itr = addrs.begin();
39  itr != addrs.end(); ++itr) {
40  success = Listen(*itr, queuelength, error);
41  if (success) break;
42  }
43  if (!success) throw ErrnoException(error);
44 }
void Listen(const SocketAddress &addr, int queuelength=256)

+ Here is the call graph for this function:

bool ServerSocketHandle::Listen ( const SocketAddress addr,
int  queuelength,
int error 
)
private

Definition at line 103 of file ServerSocketHandle.cc.

References SocketAddress::Family(), FileHandle::FD(), SocketAddress::GetAddr(), SocketAddress::GetLen(), and reuse_addr.

103  {
104  SocketAddress address = addr;
105  int nfd = socket(address.Family(), SOCK_STREAM, 0);
106  if (nfd < 0) {
107  error = errno;
108  return false;
109  }
110  if (reuse_addr) {
111  int opt = 1;
112  if (setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
113  throw ErrnoException();
114  }
115  }
116  if (bind(nfd, address.GetAddr(), address.GetLen()) < 0) {
117  error = errno;
118  return false;
119  }
120  if (listen(nfd, queuelength) < 0) {
121  error = errno;
122  return false;
123  }
124  FD(nfd);
125  return true;
126 }
An abstraction of a socket address with convenience methods.
Definition: SocketAddress.h:42
socklen_t & GetLen()
sa_family_t & Family()
sockaddr * GetAddr()
int FD() const
Definition: FileHandle.h:106

+ Here is the call graph for this function:

virtual void FileHandle::OnReadable ( )
inlineprotectedvirtualinherited

Called by Poll when it detects that the file is readable.

Reimplemented in WakeupHandle.

Definition at line 210 of file FileHandle.h.

References FileHandle::Readable().

Referenced by FileHandle::Poll().

210 { Readable(true); }
bool Readable() const
Gives the current readability status of the file.
Definition: FileHandle.h:91

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

virtual void FileHandle::OnWriteable ( )
inlineprotectedvirtualinherited

Called by Poll when it detects that the file is writeable.

Definition at line 212 of file FileHandle.h.

References FileHandle::Writeable().

Referenced by FileHandle::Poll().

212 { Writeable(true); }
bool Writeable() const
Gives the current writability status of the file.
Definition: FileHandle.h:102

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int FileHandle::Poll ( IteratorRef< FileHandle * >  begin,
IteratorRef< FileHandle * >  end,
double  timeout 
)
staticinherited

poll a list of FileHandles for any activity and call the appropriate On method.

Definition at line 34 of file FileHandle.cc.

References FileHandle::FD(), FileHandle::fd, FileHandle::OnReadable(), FileHandle::OnWriteable(), FileHandle::Readable(), and FileHandle::Writeable().

Referenced by CPN::RemoteContext::EntryPoint(), CPN::RemoteQueue::FileThreadEntryPoint(), CPN::ConnectionServer::Poll(), FileHandle::Poll(), and CPN::RemoteContextDaemon::Run().

34  {
35  fd_set rfd;
36  fd_set wfd;
37  FD_ZERO(&rfd);
38  FD_ZERO(&wfd);
39  int maxfd = 0;
40  IteratorRef<FileHandle*> itr = begin;
41  while (itr != end) {
42  FileHandle *han = *itr;
43  int fd = han->FD();
44  if (fd < 0) {
45  return -1;
46  } else {
47  bool set = false;
48  if (!han->Readable()) {
49  FD_SET(fd, &rfd);
50  set = true;
51  }
52  if (!han->Writeable()) {
53  FD_SET(fd, &wfd);
54  set = true;
55  }
56  if (set) { maxfd = std::max(maxfd, fd); }
57  }
58  ++itr;
59  }
60  timeval tv;
61  timeval *ptv = 0;
62  if (timeout >= 0) {
63  tv.tv_sec = (int)timeout;
64  tv.tv_usec = (int)((timeout - tv.tv_sec) * 1e6);
65  ptv = &tv;
66  }
67  int ret = select(maxfd + 1, &rfd, &wfd, 0, ptv);
68  if (ret < 0) {
69  if (errno == EINTR) {
70  return 0;
71  }
72  throw ErrnoException();
73  }
74  itr = begin;
75  while (itr != end) {
76  FileHandle *han = *itr;
77  int fd = han->FD();
78  if (fd >= 0) {
79  if (FD_ISSET(fd, &rfd)) {
80  han->OnReadable();
81  }
82  if (FD_ISSET(fd, &wfd)) {
83  han->OnWriteable();
84  }
85  }
86  ++itr;
87  }
88  return ret;
89 }
virtual void OnWriteable()
Called by Poll when it detects that the file is writeable.
Definition: FileHandle.h:212
bool Writeable(bool w)
Set that this file is currently writeable or not.
Definition: FileHandle.h:97
A reference to an iterator.
Definition: IteratorRef.h:46
int FD() const
Definition: FileHandle.h:106
bool Readable(bool r)
Set that the file is currently readable or not.
Definition: FileHandle.h:86
Generic file handle could be a file, or a socket or a device.
Definition: FileHandle.h:41
virtual void OnReadable()
Called by Poll when it detects that the file is readable.
Definition: FileHandle.h:210

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int FileHandle::Poll ( double  timeout)
inherited

Poll the current file descriptor for activity specified by the current readable or writeable status, if false poll that value, and call one of OnWriteable or OnReadable whos default action is to set Readable or Writeable to true.

Parameters
timeout-1 to wait forever for activity 0 to poll and return immediately or a time to wait in seconds.
Returns
zero if timed out, positive if an event occurred or negative if an error occurred.

Definition at line 106 of file FileHandle.cc.

References FileHandle::Poll().

106  {
107  FileHandle *fh = this;
108  return Poll(&fh, &fh + 1, timeout);
109 }
static int Poll(IteratorRef< FileHandle * > begin, IteratorRef< FileHandle * > end, double timeout)
poll a list of FileHandles for any activity and call the appropriate On method.
Definition: FileHandle.cc:34
Generic file handle could be a file, or a socket or a device.
Definition: FileHandle.h:41

+ Here is the call graph for this function:

unsigned FileHandle::Read ( void *  ptr,
unsigned  len 
)
inherited

Read data from the file descriptor.

Will set the end of file condition if read detects end of file.

Note
All the read functions will set readable to false if they read less than the requested amount or we are non blocking and a would block condition happened.
Parameters
ptrpointer to write data to
lenthe maximum number of bytes to write to ptr
Returns
0 if no bytes read (check Eof) or the number of bytes read

Definition at line 159 of file FileHandle.cc.

References FileHandle::eof, FileHandle::fd, FileHandle::file_lock, FileHandle::Readable(), and FileHandle::readable.

Referenced by CPN::RemoteQueue::D4RTagPacket(), and WakeupHandle::Read().

159  {
160  int filed;
161  {
162  ALock al(file_lock);
163  if (eof || fd == -1) { return 0; }
164  filed = fd;
165  }
166  unsigned bytesread = 0;
167  int num = read(filed, ptr, len);
168  if (num < 0) {
169  int error = errno;
170  switch (error) {
171  case EAGAIN:
172  Readable(false);
173  case EINTR:
174  case ENOMEM:
175  break;
176  default:
177  throw ErrnoException(error);
178  }
179  } else if (num == 0 && len != 0) {
180  ALock al(file_lock);
181  eof = true;
182  readable = false;
183  } else {
184  if (unsigned(num) < len) { Readable(false); }
185  bytesread = num;
186  }
187  return bytesread;
188 }
PthreadMutex file_lock
Definition: FileHandle.h:213
AutoLock< PthreadMutex > ALock
Definition: FileHandle.h:43
bool readable
Definition: FileHandle.h:215
bool Readable() const
Gives the current readability status of the file.
Definition: FileHandle.h:91

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool FileHandle::Readable ( bool  r)
inlineinherited

Set that the file is currently readable or not.

Parameters
rtrue or false
Returns
the new readable status

Definition at line 86 of file FileHandle.h.

References FileHandle::file_lock, and FileHandle::readable.

Referenced by CPN::RemoteContext::EntryPoint(), FileHandle::Poll(), CPN::ConnectionServer::Poll(), and CPN::RemoteQueue::Read().

86 { ALock al(file_lock); return readable = r; }
PthreadMutex file_lock
Definition: FileHandle.h:213
AutoLock< PthreadMutex > ALock
Definition: FileHandle.h:43
bool readable
Definition: FileHandle.h:215

+ Here is the caller graph for this function:

bool FileHandle::Readable ( ) const
inlineinherited

Gives the current readability status of the file.

Returns
true if it is known that a read will not block

Definition at line 91 of file FileHandle.h.

References FileHandle::file_lock, and FileHandle::readable.

Referenced by Accept(), CPN::RemoteContextDaemon::Client::Client(), FileHandle::OnReadable(), CPN::RemoteContextDaemon::Read(), FileHandle::Read(), FileHandle::Readv(), SocketHandle::Recv(), CPN::RemoteContextDaemon::Run(), and WakeupHandle::WakeupHandle().

91 { ALock al(file_lock); return readable; }
PthreadMutex file_lock
Definition: FileHandle.h:213
AutoLock< PthreadMutex > ALock
Definition: FileHandle.h:43
bool readable
Definition: FileHandle.h:215

+ Here is the caller graph for this function:

unsigned FileHandle::Readv ( const iovec *  iov,
int  iovcnt 
)
inherited

scatter gather io version of Read

Returns
see Read

Definition at line 190 of file FileHandle.cc.

References FileHandle::eof, FileHandle::fd, FileHandle::file_lock, FileHandle::Readable(), and FileHandle::readable.

Referenced by CPN::RemoteQueue::EnqueuePacket().

190  {
191  int filed;
192  {
193  ALock al(file_lock);
194  if (eof || fd == -1) { return 0; }
195  filed = fd;
196  }
197  unsigned len = 0;
198  for (int i = 0; i < iovcnt; ++i) {
199  len += iov[i].iov_len;
200  }
201  unsigned bytesread = 0;
202  int num = readv(filed, iov, iovcnt);
203  if (num < 0) {
204  int error = errno;
205  switch (error) {
206  case EAGAIN:
207  Readable(false);
208  case EINTR:
209  case ENOMEM:
210  break;
211  default:
212  throw ErrnoException(error);
213  }
214  } else if (num == 0 && len != 0) {
215  ALock al(file_lock);
216  eof = true;
217  readable = false;
218  } else {
219  if (unsigned(num) < len) { Readable(false); }
220  bytesread = num;
221  }
222  return bytesread;
223 }
PthreadMutex file_lock
Definition: FileHandle.h:213
AutoLock< PthreadMutex > ALock
Definition: FileHandle.h:43
bool readable
Definition: FileHandle.h:215
bool Readable() const
Gives the current readability status of the file.
Definition: FileHandle.h:91

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void FileHandle::Reset ( )
inherited

Clear all internal state including the file descriptor! WARNING does not close the file!

Definition at line 138 of file FileHandle.cc.

References FileHandle::eof, FileHandle::fd, FileHandle::file_lock, FileHandle::readable, and FileHandle::writeable.

Referenced by CPN::ConnectionServer::ConnectWriter(), CPN::RemoteQueue::FileThreadEntryPoint(), and FileHandle::~FileHandle().

138  {
139  ALock al(file_lock);
140  readable = false;
141  writeable = false;
142  eof = false;
143  fd = -1;
144 }
bool writeable
Definition: FileHandle.h:216
PthreadMutex file_lock
Definition: FileHandle.h:213
AutoLock< PthreadMutex > ALock
Definition: FileHandle.h:43
bool readable
Definition: FileHandle.h:215

+ Here is the caller graph for this function:

void FileHandle::SetBlocking ( bool  blocking)
inherited

Manipulate how the current file handles blocking.

Parameters
blockingtrue to set to blocking mode (default) false to set to non blocking mode.

In non blocking mode all reads and writes will not block. Use Poll to block.

Definition at line 111 of file FileHandle.cc.

References FileHandle::FD().

Referenced by WakeupHandle::WakeupHandle().

111  {
112  SetBlocking(FD(), blocking);
113 }
void SetBlocking(bool blocking)
Manipulate how the current file handles blocking.
Definition: FileHandle.cc:111
int FD() const
Definition: FileHandle.h:106

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void FileHandle::SetBlocking ( int  fd,
bool  blocking 
)
staticinherited

a convenience method that allows one to set the same blocking parameters for a file descriptor without setting it inside a FileHandle.

Parameters
fdthe file descriptor
blockingtrue or false see SetBlock

Definition at line 115 of file FileHandle.cc.

115  {
116  int flags = fcntl(fd, F_GETFL, 0);
117  if (-1 == flags) { throw ErrnoException(); }
118  if (blocking) {
119  flags &= ~O_NONBLOCK;
120  } else {
121  flags |= O_NONBLOCK;
122  }
123  if (fcntl(fd, F_SETFL, flags) != 0) {
124  throw ErrnoException();
125  }
126 }
void ServerSocketHandle::SetReuseAddr ( bool  reuse = true)
inline

Turn on reuse of the address. Only takes effect if set before Listen.

Definition at line 70 of file ServerSocketHandle.h.

References reuse_addr.

Referenced by CPN::RemoteContextDaemon::RemoteContextDaemon().

70 { reuse_addr = true; }

+ Here is the caller graph for this function:

unsigned FileHandle::Write ( const void *  ptr,
unsigned  len 
)
inherited

Write data to the file descriptor.

Note
All write functions will set Writeable to false if they write less than the amount requested or if the file is in non blocking mode and the write returned would block.
Parameters
ptrpointer to beginning of data to write
lenlength of data to write
Returns
number of bytes written

Definition at line 225 of file FileHandle.cc.

References FileHandle::fd, FileHandle::file_lock, and FileHandle::Writeable().

Referenced by CPN::ConnectionServer::ConnectWriter(), CPN::RemoteContextDaemon::Client::Send(), CPN::RemoteContext::SendMessage(), and CPN::RemoteQueue::WriteBytes().

225  {
226  int filed;
227  {
228  ALock al(file_lock);
229  if (fd == -1) { return 0; }
230  filed = fd;
231  }
232  unsigned written = 0;
233  int num = write(filed, ptr, len);
234  if (num < 0) {
235  int error = errno;
236  switch (error) {
237  case EAGAIN: // Would block
238  //case EWOULDBLOCK:
239  Writeable(false);
240  case EINTR: // Returned because of signal
241  case ENOMEM:
242  case ENOBUFS: // Buffers are full
243  break;
244  case EPIPE:
245  case EBADF:
246  case EFAULT:
247  case EFBIG:
248  case EINVAL:
249  case EIO:
250  case ENOSPC:
251  default:
252  throw ErrnoException(error);
253  }
254  } else {
255  if (unsigned(num) < len) { Writeable(false); }
256  written = num;
257  }
258  return written;
259 }
PthreadMutex file_lock
Definition: FileHandle.h:213
AutoLock< PthreadMutex > ALock
Definition: FileHandle.h:43
bool Writeable() const
Gives the current writability status of the file.
Definition: FileHandle.h:102

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool FileHandle::Writeable ( bool  w)
inlineinherited

Set that this file is currently writeable or not.

Parameters
wtrue or false
Returns
the new writeable status

Definition at line 97 of file FileHandle.h.

References FileHandle::file_lock, and FileHandle::writeable.

Referenced by FileHandle::Poll().

97 { ALock al(file_lock); return writeable = w; }
bool writeable
Definition: FileHandle.h:216
PthreadMutex file_lock
Definition: FileHandle.h:213
AutoLock< PthreadMutex > ALock
Definition: FileHandle.h:43

+ Here is the caller graph for this function:

bool FileHandle::Writeable ( ) const
inlineinherited

Gives the current writability status of the file.

Returns
true if it is known that a write will not block

Definition at line 102 of file FileHandle.h.

References FileHandle::file_lock, and FileHandle::writeable.

Referenced by FileHandle::OnWriteable(), SocketHandle::Send(), WakeupHandle::WakeupHandle(), FileHandle::Write(), and FileHandle::Writev().

102 { ALock al(file_lock); return writeable; }
bool writeable
Definition: FileHandle.h:216
PthreadMutex file_lock
Definition: FileHandle.h:213
AutoLock< PthreadMutex > ALock
Definition: FileHandle.h:43

+ Here is the caller graph for this function:

unsigned FileHandle::Writev ( const iovec *  iov,
int  iovcnt 
)
inherited

scatter gather io version of Write

Returns
see Write

Definition at line 261 of file FileHandle.cc.

References FileHandle::fd, FileHandle::file_lock, and FileHandle::Writeable().

Referenced by CPN::RemoteQueue::WriteBytes().

261  {
262  int filed;
263  {
264  ALock al(file_lock);
265  if (fd == -1) { return 0; }
266  filed = fd;
267  }
268  unsigned written = 0;
269  int num = writev(filed, iov, iovcnt);
270  if (num < 0) {
271  int error = errno;
272  switch (error) {
273  case EAGAIN:
274  Writeable(false);
275  case EINTR:
276  case ENOMEM:
277  case ENOBUFS:
278  break;
279  default:
280  throw ErrnoException(error);
281  }
282  } else {
283  unsigned len = 0;
284  for (int i = 0; i < iovcnt; ++i) {
285  len += iov[i].iov_len;
286  }
287  if (unsigned(num) < len) { Writeable(false); }
288  written = num;
289  }
290  return written;
291 }
PthreadMutex file_lock
Definition: FileHandle.h:213
AutoLock< PthreadMutex > ALock
Definition: FileHandle.h:43
bool Writeable() const
Gives the current writability status of the file.
Definition: FileHandle.h:102

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Member Data Documentation

bool FileHandle::eof
protectedinherited
int FileHandle::fd
protectedinherited
PthreadMutex FileHandle::file_lock
mutableprotectedinherited
bool FileHandle::readable
protectedinherited
bool ServerSocketHandle::reuse_addr
private

Definition at line 75 of file ServerSocketHandle.h.

Referenced by GetReuseAddr(), Listen(), and SetReuseAddr().

bool FileHandle::writeable
protectedinherited

Definition at line 216 of file FileHandle.h.

Referenced by FileHandle::Close(), FileHandle::Reset(), and FileHandle::Writeable().


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