Flexiport  2.0.0
tcpport.h
Go to the documentation of this file.
1 /* Flexiport
2  *
3  * Header file for the TCP port class.
4  *
5  * Copyright 2008-2011 Geoffrey Biggs geoffrey.biggs@aist.go.jp
6  * RT-Synthesis Research Group
7  * Intelligent Systems Research Institute,
8  * National Institute of Advanced Industrial Science and Technology (AIST),
9  * Japan
10  * All rights reserved.
11  *
12  * This file is part of Flexiport.
13  *
14  * Flexiport is free software; you can redistribute it and/or modify it
15  * under the terms of the GNU Lesser General Public License as published
16  * by the Free Software Foundation; either version 2.1 of the License,
17  * or (at your option) any later version.
18  *
19  * Flexiport is distributed in the hope that it will be useful, but
20  * WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with Flexiport. If not, see
26  * <http://www.gnu.org/licenses/>.
27  */
28 
29 #ifndef __TCPPORT_H
30 #define __TCPPORT_H
31 
32 #include <flexiport/port.h>
33 
34 #include <map>
35 #include <string>
36 
41 namespace flexiport
42 {
43 
62 {
63  public:
64  TCPPort (std::map<std::string, std::string> options);
65  ~TCPPort ();
66 
72  void Open ();
74  void Close ();
76  ssize_t Read (void * const buffer, size_t count);
78  ssize_t ReadFull (void * const buffer, size_t count);
80  ssize_t BytesAvailable ();
82  ssize_t BytesAvailableWait ();
84  ssize_t Write (const void * const buffer, size_t count);
86  void Flush ();
88  void Drain ();
90  std::string GetStatus () const;
92  void SetTimeout (Timeout timeout);
94  void SetCanRead (bool canRead);
96  void SetCanWrite (bool canWrite);
98  bool IsOpen () const { return _open; }
99 
100  private:
101  int _sock; // Socket connected to wherever the data is coming from.
102  int _listenSock; // Socket to listen on when in listen mode.
103 
104  std::string _ip;
105  unsigned int _port;
106  bool _isListener; // True if this port should listen instead of actively connecting.
107  bool _open;
108 
109  void CheckPort (bool read);
110 
111  bool ProcessOption (const std::string &option, const std::string &value);
112 
113  void Connect ();
114  void WaitForConnection ();
115  typedef enum {TIMED_OUT, DATA_AVAILABLE, CAN_WRITE} WaitStatus;
116  WaitStatus WaitForDataOrTimeout ();
117  bool IsDataAvailable ();
118  WaitStatus WaitForWritableOrTimeout ();
119  void SetSocketBlockingFlag ();
120 };
121 
122 } // namespace flexiport
123 
126 #endif // __TCPPORT_H
127 
TCP implementation of the Port class.
Definition: tcpport.h:61
#define FLEXIPORT_EXPORT
Definition: flexiport.h:44
Base Port class.
Definition: port.h:81
bool IsOpen() const
Check if the port is open.
Definition: tcpport.h:98
An object used to represent timeouts.
Definition: timeout.h:63