INTRODUCTION
Overview
Download and Install
Documentation
Publications

REPOSITORY
Libraries

DEVELOPER
Dev Guide
Dashboard

PEOPLE
Contributors
Users

SourceForge.net Logo
Project
Download
Mailing lists

 

         
tcpport.h
1 /*
2  * GearBox Project: Peer-Reviewed Open-Source Libraries for Robotics
3  * http://gearbox.sf.net/
4  * Copyright (c) 2008 Geoffrey Biggs
5  *
6  * flexiport flexible hardware data communications library.
7  *
8  * This distribution is licensed to you under the terms described in the LICENSE file included in
9  * this distribution.
10  *
11  * This work is a product of the National Institute of Advanced Industrial Science and Technology,
12  * Japan. Registration number: H20PRO-881
13  *
14  * This file is part of flexiport.
15  *
16  * flexiport is free software: you can redistribute it and/or modify it under the terms of the GNU
17  * Lesser General Public License as published by the Free Software Foundation, either version 3 of
18  * the License, or (at your option) any later version.
19  *
20  * flexiport is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
21  * even the implied warranty of 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 License along with flexiport.
25  * If not, see <http://www.gnu.org/licenses/>.
26  */
27 
28 #ifndef __TCPPORT_H
29 #define __TCPPORT_H
30 
31 #include "port.h"
32 
33 #include <map>
34 #include <string>
35 
40 namespace flexiport
41 {
42 
58 class FLEXIPORT_EXPORT TCPPort : public Port
59 {
60  public:
61  TCPPort (std::map<std::string, std::string> options);
62  ~TCPPort ();
63 
68  void Open ();
70  void Close ();
72  ssize_t Read (void * const buffer, size_t count);
74  ssize_t ReadFull (void * const buffer, size_t count);
76  ssize_t BytesAvailable ();
78  ssize_t BytesAvailableWait ();
80  ssize_t Write (const void * const buffer, size_t count);
82  void Flush ();
84  void Drain ();
86  std::string GetStatus () const;
88  void SetTimeout (Timeout timeout);
90  void SetCanRead (bool canRead);
92  void SetCanWrite (bool canWrite);
94  bool IsOpen () const { return _open; }
95 
96  private:
97  int _sock; // Socket connected to wherever the data is coming from.
98  int _listenSock; // Socket to listen on when in listen mode.
99 
100  std::string _ip;
101  unsigned int _port;
102  bool _isListener; // True if this port should listen instead of actively connecting.
103  bool _open;
104 
105  void CheckPort (bool read);
106 
107  bool ProcessOption (const std::string &option, const std::string &value);
108 
109  void Connect ();
110  void WaitForConnection ();
111  typedef enum {TIMED_OUT, DATA_AVAILABLE, CAN_WRITE} WaitStatus;
112  WaitStatus WaitForDataOrTimeout ();
113  bool IsDataAvailable ();
114  WaitStatus WaitForWritableOrTimeout ();
115  void SetSocketBlockingFlag ();
116 };
117 
118 } // namespace flexiport
119 
122 #endif // __TCPPORT_H
void Open()
Open the port.
Definition: tcpport.cpp:141
virtual ssize_t WriteString(const char *const buffer)
Write a string to the port.
Definition: port.cpp:331
bool IsOpen() const
Check if the port is open.
Definition: tcpport.h:94
virtual ssize_t ReadLine(char *const buffer, size_t count)
Read a new-line terminated string of data.
Definition: port.cpp:201
FlexiPort data communications library.
ssize_t BytesAvailableWait()
Get the number of bytes waiting after blocking for the timeout.
Definition: tcpport.cpp:380
std::string GetStatus() const
Get the status of the port (type, device, etc).
Definition: tcpport.cpp:514
virtual void Open()=0
Open the port.
void Drain()
Drain the port's input and output buffers.
Definition: tcpport.cpp:503
ssize_t ReadFull(void *const buffer, size_t count)
Read the requested quantity of data from the port.
Definition: tcpport.cpp:282
virtual ssize_t BytesAvailableWait()=0
Get the number of bytes waiting after blocking for the timeout.
Exception thrown by the Port class and its derivatives.
Definition: flexiport.h:55
void SetCanWrite(bool canWrite)
Set the write permissions of the port.
Definition: tcpport.cpp:537
void SetTimeout(Timeout timeout)
Set the timeout value in milliseconds.
Definition: tcpport.cpp:526
virtual ssize_t ReadStringUntil(std::string &buffer, char terminator)
Read a string until the specified termination character is received.
Definition: port.cpp:160
virtual ssize_t ReadString(std::string &buffer)
Read a string.
Definition: port.cpp:72
An object used to represent timeouts.
Definition: timeout.h:63
TCP implementation of the Port class.
Definition: tcpport.h:59
virtual ssize_t BytesAvailable()=0
Get the number of bytes waiting to be read at the port. Returns immediatly.
void Flush()
Flush the port's input and output buffers, discarding all data.
Definition: tcpport.cpp:472
FLEXIPORT_EXPORT Port * CreatePort(std::string options)
Overloaded factory function. Accepts options as a string.
ssize_t Read(void *const buffer, size_t count)
Read from the port.
Definition: tcpport.cpp:206
virtual ssize_t ReadUntil(void *const buffer, size_t count, uint8_t terminator)
Read data until a specified termination byte is received.
Definition: port.cpp:115
void SetCanRead(bool canRead)
Set the read permissions of the port.
Definition: tcpport.cpp:532
ssize_t BytesAvailable()
Get the number of bytes waiting to be read at the port. Returns immediatly.
Definition: tcpport.cpp:349
virtual ssize_t Read(void *const buffer, size_t count)=0
Read from the port.
void Close()
Close the port.
Definition: tcpport.cpp:168
ssize_t Write(const void *const buffer, size_t count)
Write data to the port.
Definition: tcpport.cpp:427
virtual ssize_t ReadFull(void *const buffer, size_t count)=0
Read the requested quantity of data from the port.
virtual ssize_t Write(const void *const buffer, size_t count)=0
Write data to the port.
Base Port class.
Definition: port.h:80
virtual std::string GetStatus() const
Get the status of the port (type, device, etc).
Definition: port.cpp:352
 

Generated for GearBox by  doxygen 1.4.5