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

A FileHandle customized with some socket specific functionality and functions. More...

#include <SocketHandle.h>

+ Inheritance diagram for SocketHandle:
+ Collaboration diagram for SocketHandle:

Classes

struct  SendOpts
 

Public Types

typedef FileHandle::ALock ALock
 

Public Member Functions

 SocketHandle ()
 
 SocketHandle (int nfd)
 
void Connect (const SocketAddress &addr)
 Create a new socket and try to connect to the given address. More...
 
void Connect (const SockAddrList &addrs)
 Create a new socket and try to connect to one of the addresses in the address list. More...
 
void ShutdownRead ()
 Shutdown the read end of this socket. This does NOT close the socket! Any future attempt to read from this socket will fail. More...
 
void ShutdownWrite ()
 Shutdown the write end of this socket. Any future attempt to write to this socket will fail. This is how you send an end of file down the socket. More...
 
unsigned Recv (void *ptr, unsigned len, bool block)
 
unsigned Send (const void *ptr, unsigned len, const SendOpts &opts)
 
int GetPendingError ()
 Get and clear any pending error. More...
 
void SetKeepAlive (int ka)
 
int GetKeepAlive ()
 
void SetLingerTimeout (int seconds)
 Set the linger socket options. More...
 
int GetLingerTimeout ()
 
void SetReceiveBufferSize (int size)
 
int GetReceiveBufferSize ()
 
void SetSendBufferSize (int size)
 
int GetSendBufferSize ()
 
void SetReceiveTimeout (double timeout)
 
void SetSendTimeout (double timeout)
 
double GetReceiveTimeout ()
 
double GetSendTimeout ()
 
void SetNoDelay (bool nodelay)
 
bool GetNoDelay ()
 
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 void CreatePair (SocketHandle &sock1, SocketHandle &sock2)
 Create a socket pair. More...
 
static void CreatePair (int fd[2])
 Convenience function that returns actual file descriptors. More...
 
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 Connect (const SocketAddress &addr, int &error)
 

Detailed Description

A FileHandle customized with some socket specific functionality and functions.

Definition at line 34 of file SocketHandle.h.

Member Typedef Documentation

Definition at line 36 of file SocketHandle.h.

Constructor & Destructor Documentation

SocketHandle::SocketHandle ( )
inline

Definition at line 38 of file SocketHandle.h.

38 {}
SocketHandle::SocketHandle ( int  nfd)
inline

Definition at line 39 of file SocketHandle.h.

39 : FileHandle(nfd) {}
FileHandle()
Construct a closed FileHandle.
Definition: FileHandle.cc:91

Member Function Documentation

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
void SocketHandle::Connect ( const SocketAddress addr)

Create a new socket and try to connect to the given address.

Parameters
addrthe address to connect to

Definition at line 48 of file SocketHandle.cc.

Referenced by Connect(), CPN::ConnectionServer::ConnectWriter(), and CPN::RemoteContext::RemoteContext().

48  {
49  int error = 0;
50  if (!Connect(addr, error)) { throw ErrnoException(error); }
51 }
void Connect(const SocketAddress &addr)
Create a new socket and try to connect to the given address.
Definition: SocketHandle.cc:48

+ Here is the caller graph for this function:

void SocketHandle::Connect ( const SockAddrList addrs)

Create a new socket and try to connect to one of the addresses in the address list.

Parameters
addrsthe addresses to try to connect to

Definition at line 53 of file SocketHandle.cc.

References Connect().

53  {
54  int error = 0;
55  bool success = false;
56  for (SockAddrList::const_iterator itr = addrs.begin();
57  itr != addrs.end(); ++itr) {
58  success = Connect(*itr, error);
59  if (success) break;
60  }
61  if (!success) throw ErrnoException(error);
62 }
void Connect(const SocketAddress &addr)
Create a new socket and try to connect to the given address.
Definition: SocketHandle.cc:48

+ Here is the call graph for this function:

bool SocketHandle::Connect ( const SocketAddress addr,
int error 
)
private

Definition at line 64 of file SocketHandle.cc.

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

64  {
65  ASSERT(Closed(), "Already connected!");
66  int nfd = socket(addr.Family(), SOCK_STREAM, 0);
67  if (nfd < 0) {
68  error = errno;
69  return false;
70  }
71  SocketAddress address = addr;
72  bool loop = true;
73  while (loop) {
74  if (connect(nfd, address.GetAddr(), address.GetLen()) < 0) {
75  error = errno;
76  switch (error) {
77  case EINTR:
78  case EAGAIN: // not enough resources
79  break;
80  case EINPROGRESS: // non blocking and connection not completed yet
81  // may want to change things to let non blocking connect.
82  case EALREADY: // previous attempt has not completed
83  case EACCES:
84  case EPERM:
85  case EADDRINUSE:
86  case EAFNOSUPPORT:
87  case EBADF:
88  case ECONNREFUSED:
89  case EFAULT:
90  case EISCONN:
91  case ENETUNREACH:
92  case ENOTSOCK:
93  case ETIMEDOUT:
94  default:
95  close(nfd);
96  nfd = -1;
97  return false;
98  }
99  } else {
100  loop = false;
101  }
102  }
103  FD(nfd);
104  return true;
105 }
An abstraction of a socket address with convenience methods.
Definition: SocketAddress.h:42
socklen_t & GetLen()
sa_family_t & Family()
sockaddr * GetAddr()
bool Closed() const
Definition: FileHandle.h:128
int FD() const
Definition: FileHandle.h:106
#define ASSERT(exp,...)

+ Here is the call graph for this function:

void SocketHandle::CreatePair ( SocketHandle sock1,
SocketHandle sock2 
)
static

Create a socket pair.

Parameters
sock1SocketHandle to fill with one of the created file descriptors
sock2SocketHandle to fill with one of the created file descriptors

Definition at line 33 of file SocketHandle.cc.

References ASSERT, and FileHandle::FD().

33  {
34  ASSERT(sock1.FD() == -1, "sock1 already connected");
35  ASSERT(sock2.FD() == -1, "sock2 already connected");
36  int pair[2];
37  CreatePair(pair);
38  sock1.FD(pair[0]);
39  sock2.FD(pair[1]);
40 }
static void CreatePair(SocketHandle &sock1, SocketHandle &sock2)
Create a socket pair.
Definition: SocketHandle.cc:33
int FD() const
Definition: FileHandle.h:106
#define ASSERT(exp,...)

+ Here is the call graph for this function:

void SocketHandle::CreatePair ( int  fd[2])
static

Convenience function that returns actual file descriptors.

Definition at line 42 of file SocketHandle.cc.

42  {
43  if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd) < 0) {
44  throw ErrnoException(errno);
45  }
46 }
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:

int SocketHandle::GetKeepAlive ( )

Definition at line 219 of file SocketHandle.cc.

References FileHandle::FD().

219  {
220  int ka;
221  socklen_t len = sizeof(ka);
222  if (getsockopt(FD(), SOL_SOCKET, SO_KEEPALIVE, &ka, &len) < 0) {
223  throw ErrnoException();
224  }
225  return ka;
226 }
int FD() const
Definition: FileHandle.h:106

+ Here is the call graph for this function:

int SocketHandle::GetLingerTimeout ( )
Returns
number of seconds socket will linger, negative means off

Definition at line 239 of file SocketHandle.cc.

References FileHandle::FD().

239  {
240  linger l = {0};
241  socklen_t len = sizeof(l);
242  if (getsockopt(FD(), SOL_SOCKET, SO_LINGER, &l, &len) < 0) {
243  throw ErrnoException();
244  }
245  if (l.l_onoff) {
246  return -1;
247  } else {
248  return l.l_linger;
249  }
250 }
int FD() const
Definition: FileHandle.h:106

+ Here is the call graph for this function:

bool SocketHandle::GetNoDelay ( )

Definition at line 326 of file SocketHandle.cc.

References FileHandle::FD().

326  {
327  int flag = 0;
328  socklen_t len = sizeof(flag);
329  if (getsockopt(FD(), IPPROTO_TCP, TCP_NODELAY, &flag, &len) < 0) {
330  throw ErrnoException();
331  }
332  return flag == 0 ? false : true;
333 }
int FD() const
Definition: FileHandle.h:106

+ Here is the call graph for this function:

int SocketHandle::GetPendingError ( )

Get and clear any pending error.

Returns
the pending error or 0 if none

Definition at line 204 of file SocketHandle.cc.

References FileHandle::FD().

204  {
205  int err = 0;
206  socklen_t len = sizeof(err);
207  if (getsockopt(FD(), SOL_SOCKET, SO_ERROR, &err, &len) < 0) {
208  throw ErrnoException();
209  }
210  return err;
211 }
int FD() const
Definition: FileHandle.h:106

+ Here is the call graph for this function:

int SocketHandle::GetReceiveBufferSize ( )

Definition at line 258 of file SocketHandle.cc.

References FileHandle::FD().

258  {
259  int size;
260  socklen_t len = sizeof(size);
261  if (getsockopt(FD(), SOL_SOCKET, SO_RCVBUF, &size, &len) < 0) {
262  throw ErrnoException();
263  }
264  return size;
265 }
int FD() const
Definition: FileHandle.h:106

+ Here is the call graph for this function:

double SocketHandle::GetReceiveTimeout ( )

Definition at line 301 of file SocketHandle.cc.

References FileHandle::FD().

301  {
302  timeval tv = {0};
303  socklen_t len = sizeof(tv);
304  if (getsockopt(FD(), SOL_SOCKET, SO_RCVTIMEO, &tv, &len) < 0) {
305  throw ErrnoException();
306  }
307  return ((double)tv.tv_sec) + (((double)tv.tv_usec)*1e-6);
308 }
int FD() const
Definition: FileHandle.h:106

+ Here is the call graph for this function:

int SocketHandle::GetSendBufferSize ( )

Definition at line 273 of file SocketHandle.cc.

References FileHandle::FD().

273  {
274  int size;
275  socklen_t len = sizeof(size);
276  if (getsockopt(FD(), SOL_SOCKET, SO_SNDBUF, &size, &len) < 0) {
277  throw ErrnoException();
278  }
279  return size;
280 }
int FD() const
Definition: FileHandle.h:106

+ Here is the call graph for this function:

double SocketHandle::GetSendTimeout ( )

Definition at line 310 of file SocketHandle.cc.

References FileHandle::FD().

310  {
311  timeval tv = {0};
312  socklen_t len = sizeof(tv);
313  if (getsockopt(FD(), SOL_SOCKET, SO_SNDTIMEO, &tv, &len) < 0) {
314  throw ErrnoException();
315  }
316  return ((double)tv.tv_sec) + (((double)tv.tv_usec)*1e-6);
317 }
int FD() const
Definition: FileHandle.h:106

+ Here is the call graph for this function:

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 }
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 ServerSocketHandle::Accept(), CPN::RemoteContextDaemon::Client::Client(), FileHandle::OnReadable(), CPN::RemoteContextDaemon::Read(), FileHandle::Read(), FileHandle::Readv(), 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:

unsigned SocketHandle::Recv ( void *  ptr,
unsigned  len,
bool  block 
)
Parameters
ptrpointer to memory to place read data
lenmaximum number of bytes to read
blockIf block true then wait for buffer to be full if block false don't wait for anything
Returns
number of bytes read

Definition at line 119 of file SocketHandle.cc.

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

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

119  {
120  int filed;
121  {
122  ALock al(file_lock);
123  if (eof || fd == -1) { return 0; }
124  filed = fd;
125  }
126  int flags = 0;
127  if (block) {
128  flags |= MSG_WAITALL;
129  } else {
130  flags |= MSG_DONTWAIT;
131  }
132  unsigned bytesread = 0;
133  int num = recv(filed, ptr, len, flags);
134  if (num > 0) {
135  if (unsigned(num) < len) { Readable(false); }
136  bytesread = num;
137  } else if (num == 0 && len != 0) {
138  ALock al(file_lock);
139  eof = true;
140  readable = false;
141  } else if (num < 0) {
142  int error = errno;
143  switch (error) {
144  case EAGAIN: // Ether non blocking or timed out
145  Readable(false);
146  case EINTR: // Interrupted by an interrupt
147  case ENOMEM:
148  // report nothing received
149  break;
150  default:
151  throw ErrnoException(error);
152  }
153  }
154  return bytesread;
155 }
PthreadMutex file_lock
Definition: FileHandle.h:213
FileHandle::ALock ALock
Definition: SocketHandle.h:36
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:

unsigned SocketHandle::Send ( const void *  ptr,
unsigned  len,
const SendOpts opts 
)
Parameters
ptrpointer to bytes to write
lennumber of bytes to write
optsan option object (note that a blank option object this function is exactly the same as Write)
Returns
number of bytes written

Definition at line 177 of file SocketHandle.cc.

References FileHandle::fd, FileHandle::file_lock, SocketHandle::SendOpts::flags, and FileHandle::Writeable().

177  {
178  int filed;
179  {
180  ALock al(file_lock);
181  if (fd == -1) { return 0; }
182  filed = fd;
183  }
184  unsigned written = 0;
185  int num = send(filed, ptr, len, opts.flags);
186  if (num < 0) {
187  int error = errno;
188  switch (error) {
189  case EAGAIN:
190  Writeable(false);
191  case EINTR:
192  case ENOMEM:
193  break;
194  default:
195  throw ErrnoException(error);
196  }
197  } else {
198  if (unsigned(num) < len) { Writeable(false); }
199  written = num;
200  }
201  return written;
202 }
PthreadMutex file_lock
Definition: FileHandle.h:213
FileHandle::ALock ALock
Definition: SocketHandle.h:36
bool Writeable() const
Gives the current writability status of the file.
Definition: FileHandle.h:102

+ Here is the call 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 SocketHandle::SetKeepAlive ( int  ka)

Definition at line 213 of file SocketHandle.cc.

References FileHandle::FD().

213  {
214  if (setsockopt(FD(), SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0) {
215  throw ErrnoException();
216  }
217 }
int FD() const
Definition: FileHandle.h:106

+ Here is the call graph for this function:

void SocketHandle::SetLingerTimeout ( int  seconds)

Set the linger socket options.

Parameters
secondsnumber of seconds to linger, negative to turn off

Definition at line 228 of file SocketHandle.cc.

References FileHandle::FD().

228  {
229  linger l = {0};
230  if (seconds > 0) {
231  l.l_onoff = 1;
232  l.l_linger = seconds;
233  }
234  if (setsockopt(FD(), SOL_SOCKET, SO_LINGER, &l, sizeof(l)) < 0) {
235  throw ErrnoException();
236  }
237 }
int FD() const
Definition: FileHandle.h:106

+ Here is the call graph for this function:

void SocketHandle::SetNoDelay ( bool  nodelay)

Definition at line 319 of file SocketHandle.cc.

References FileHandle::FD().

Referenced by CPN::RemoteContextDaemon::Client::Client(), CPN::RemoteQueue::FileThreadEntryPoint(), and CPN::RemoteContext::RemoteContext().

319  {
320  int flag = nodelay ? 1 : 0;
321  if (setsockopt(FD(), IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(flag)) < 0) {
322  throw ErrnoException();
323  }
324 }
int FD() const
Definition: FileHandle.h:106

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void SocketHandle::SetReceiveBufferSize ( int  size)

Definition at line 252 of file SocketHandle.cc.

References FileHandle::FD().

252  {
253  if (setsockopt(FD(), SOL_SOCKET, SO_RCVBUF, &size, sizeof(size)) < 0) {
254  throw ErrnoException();
255  }
256 }
int FD() const
Definition: FileHandle.h:106

+ Here is the call graph for this function:

void SocketHandle::SetReceiveTimeout ( double  timeout)

Definition at line 283 of file SocketHandle.cc.

References FileHandle::FD().

283  {
284  timeval tv = {0};
285  tv.tv_sec = (int)timeout;
286  tv.tv_usec = (int)((timeout - tv.tv_sec) * 1e6);
287  if (setsockopt(FD(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
288  throw ErrnoException();
289  }
290 }
int FD() const
Definition: FileHandle.h:106

+ Here is the call graph for this function:

void SocketHandle::SetSendBufferSize ( int  size)

Definition at line 267 of file SocketHandle.cc.

References FileHandle::FD().

267  {
268  if (setsockopt(FD(), SOL_SOCKET, SO_SNDBUF, &size, sizeof(size)) < 0) {
269  throw ErrnoException();
270  }
271 }
int FD() const
Definition: FileHandle.h:106

+ Here is the call graph for this function:

void SocketHandle::SetSendTimeout ( double  timeout)

Definition at line 292 of file SocketHandle.cc.

References FileHandle::FD().

292  {
293  timeval tv = {0};
294  tv.tv_sec = (int)timeout;
295  tv.tv_usec = (int)((timeout - tv.tv_sec) * 1e6);
296  if (setsockopt(FD(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0) {
297  throw ErrnoException();
298  }
299 }
int FD() const
Definition: FileHandle.h:106

+ Here is the call graph for this function:

void SocketHandle::ShutdownRead ( )

Shutdown the read end of this socket. This does NOT close the socket! Any future attempt to read from this socket will fail.

Definition at line 107 of file SocketHandle.cc.

References FileHandle::FD().

107  {
108  if (shutdown(FD(), SHUT_RD) != 0) {
109  throw ErrnoException();
110  }
111 }
int FD() const
Definition: FileHandle.h:106

+ Here is the call graph for this function:

void SocketHandle::ShutdownWrite ( )

Shutdown the write end of this socket. Any future attempt to write to this socket will fail. This is how you send an end of file down the socket.

Definition at line 113 of file SocketHandle.cc.

References FileHandle::FD().

Referenced by CPN::RemoteContext::EndWrite(), CPN::RemoteQueue::SendEndOfReadPacket(), and CPN::RemoteQueue::SendEndOfWritePacket().

113  {
114  if (shutdown(FD(), SHUT_WR) != 0) {
115  throw ErrnoException();
116  }
117 }
int FD() const
Definition: FileHandle.h:106

+ Here is the call graph for this function:

+ 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(), 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 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: