CPN
Computational Process Networks
SocketHandle.h
Go to the documentation of this file.
1 //=============================================================================
2 // Computational Process Networks class library
3 // Copyright (C) 1997-2006 Gregory E. Allen and The University of Texas
4 //
5 // This library is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Library General Public License as published
7 // by the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Library General Public License for more details.
14 //
15 // The GNU Public License is available in the file LICENSE, or you
16 // can write to the Free Software Foundation, Inc., 59 Temple Place -
17 // Suite 330, Boston, MA 02111-1307, USA, or you can find it on the
18 // World Wide Web at http://www.fsf.org.
19 //=============================================================================
23 #ifndef SOCKETHANDLE_H
24 #define SOCKETHANDLE_H
25 #pragma once
26 #include <cpn/common.h>
27 #include <cpn/io/FileHandle.h>
28 #include <cpn/io/SocketAddress.h>
29 
34 class SocketHandle : public FileHandle {
35 public:
37 
39  SocketHandle(int nfd) : FileHandle(nfd) {}
40 
45  static void CreatePair(SocketHandle &sock1, SocketHandle &sock2);
48  static void CreatePair(int fd[2]);
49 
54  void Connect(const SocketAddress &addr);
55 
61  void Connect(const SockAddrList &addrs);
62 
68  void ShutdownRead();
74  void ShutdownWrite();
75 
83  unsigned Recv(void *ptr, unsigned len, bool block);
84 
91  struct SendOpts {
92  SendOpts() : flags(0) {}
93  SendOpts(int f) : flags(f) {}
94 #if !defined(__APPLE__)
95  SendOpts &Block(bool block);
96  SendOpts &NoSignal(bool sig);
97  SendOpts &More(bool more);
98 #endif
99  int flags;
100  };
101 
108  unsigned Send(const void *ptr, unsigned len, const SendOpts &opts);
109 
113  int GetPendingError();
114 
115  void SetKeepAlive(int ka);
116  int GetKeepAlive();
120  void SetLingerTimeout(int seconds);
123  int GetLingerTimeout();
124 
125  void SetReceiveBufferSize(int size);
126  int GetReceiveBufferSize();
127  void SetSendBufferSize(int size);
128  int GetSendBufferSize();
129 
130  void SetReceiveTimeout(double timeout);
131  void SetSendTimeout(double timeout);
132  double GetReceiveTimeout();
133  double GetSendTimeout();
134 
135  void SetNoDelay(bool nodelay);
136  bool GetNoDelay();
137 
138 private:
139  bool Connect(const SocketAddress &addr, int &error);
140 };
141 #endif
int GetReceiveBufferSize()
void SetSendTimeout(double timeout)
SendOpts & Block(bool block)
void ShutdownRead()
Shutdown the read end of this socket. This does NOT close the socket! Any future attempt to read from...
void ShutdownWrite()
Shutdown the write end of this socket. Any future attempt to write to this socket will fail...
void SetSendBufferSize(int size)
An abstraction of a socket address with convenience methods.
Definition: SocketAddress.h:42
A FileHandle customized with some socket specific functionality and functions.
Definition: SocketHandle.h:34
void SetKeepAlive(int ka)
void SetLingerTimeout(int seconds)
Set the linger socket options.
unsigned Recv(void *ptr, unsigned len, bool block)
static void CreatePair(SocketHandle &sock1, SocketHandle &sock2)
Create a socket pair.
Definition: SocketHandle.cc:33
SendOpts & More(bool more)
int GetKeepAlive()
double GetReceiveTimeout()
void SetNoDelay(bool nodelay)
SendOpts & NoSignal(bool sig)
void SetReceiveBufferSize(int size)
int GetLingerTimeout()
int GetPendingError()
Get and clear any pending error.
A class to make it easy to deal with file descriptors.
FileHandle::ALock ALock
Definition: SocketHandle.h:36
SocketHandle(int nfd)
Definition: SocketHandle.h:39
int GetSendBufferSize()
unsigned Send(const void *ptr, unsigned len, const SendOpts &opts)
std::vector< SocketAddress > SockAddrList
Definition: SocketAddress.h:35
bool GetNoDelay()
void SetReceiveTimeout(double timeout)
double GetSendTimeout()
Generic file handle could be a file, or a socket or a device.
Definition: FileHandle.h:41
void Connect(const SocketAddress &addr)
Create a new socket and try to connect to the given address.
Definition: SocketHandle.cc:48