Flexiport 2.0.0
udpport.h
Go to the documentation of this file.
1/* Flexiport
2 *
3 * Header file for the UDP 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 __UDPPORT_H
30#define __UDPPORT_H
31
32#include <flexiport/port.h>
33#include <flexiport/config.h>
34
35#include <map>
36#include <string>
37#if !defined (WIN32)
38 #include <netinet/in.h>
39#endif
40
45namespace flexiport
46{
47
84{
85 public:
86 UDPPort (std::map<std::string, std::string> options);
88
92 void Open ();
94 void Close ();
96 ssize_t Read (void * const buffer, size_t count);
98 ssize_t ReadFull (void * const buffer, size_t count);
100 ssize_t ReadUntil (void * const buffer, size_t count, uint8_t terminator);
102 ssize_t ReadStringUntil (std::string &buffer, char terminator);
104 ssize_t Skip (size_t count);
107 ssize_t SkipUntil (uint8_t terminator, unsigned int count);
109 ssize_t BytesAvailable ();
113 ssize_t Write (const void * const buffer, size_t count);
115 void Flush ();
117 void Drain ();
119 std::string GetStatus () const;
121 void SetTimeout (Timeout timeout);
123 void SetCanRead (bool canRead);
125 void SetCanWrite (bool canWrite);
127 bool IsOpen () const { return _open; }
128
129 private:
130#if !defined (WIN32)
131 #if defined (FLEXIPORT_HAVE_GETADDRINFO)
132 struct sockaddr _destSockAddr;
133 #else
134 struct sockaddr_in _destSockAddr;
135 #endif
136#endif // !defined (WIN32)
137 int _sendSock; // Socket to send data from.
138 int _recvSock; // Socket to receive data on.
139
140 std::string _destIP;
141 unsigned int _destPort;
142 std::string _recvIP;
143 unsigned int _recvPort;
144 bool _open;
145
146 void CheckPort (bool read);
147
148 bool ProcessOption (const std::string &option, const std::string &value);
149
150 void OpenSender ();
151 void CloseSender ();
152 void OpenReceiver ();
153 void CloseReceiver ();
154 typedef enum {TIMED_OUT, DATA_AVAILABLE, CAN_WRITE} WaitStatus;
155 WaitStatus WaitForDataOrTimeout ();
156 bool IsDataAvailable ();
157 WaitStatus WaitForWritableOrTimeout ();
158 void SetSocketBlockingFlag ();
159};
160
161} // namespace flexiport
162
165#endif // __UDPPORT_H
166
Base Port class.
Definition port.h:82
An object used to represent timeouts.
Definition timeout.h:64
UDP implementation of the Port class.
Definition udpport.h:84
void SetCanRead(bool canRead)
Set the read permissions of the port.
std::string GetStatus() const
Get the status of the port (type, device, etc).
ssize_t ReadFull(void *const buffer, size_t count)
Read the requested quantity of data from the port.
ssize_t BytesAvailable()
Get the number of bytes waiting to be read at the port. Returns immediatly.
bool IsOpen() const
Check if the port is open.
Definition udpport.h:127
ssize_t BytesAvailableWait()
Get the number of bytes waiting after blocking for the timeout.
ssize_t ReadUntil(void *const buffer, size_t count, uint8_t terminator)
Read data until a specified termination byte is received.
ssize_t Skip(size_t count)
Dump data until the specified number of bytes have been read.
void Open()
Open the port.
void Close()
Close the port.
UDPPort(std::map< std::string, std::string > options)
void SetTimeout(Timeout timeout)
Set the timeout value in milliseconds.
ssize_t SkipUntil(uint8_t terminator, unsigned int count)
Read and dump data until the specified termination character has been seen count times.
void Flush()
Flush the port's input and output buffers, discarding all data.
void SetCanWrite(bool canWrite)
Set the write permissions of the port.
ssize_t Write(const void *const buffer, size_t count)
Write data to the port.
ssize_t Read(void *const buffer, size_t count)
Read from the port.
void Drain()
Drain the port's input and output buffers.
ssize_t ReadStringUntil(std::string &buffer, char terminator)
Read a string until the specified termination character is received.
#define FLEXIPORT_EXPORT
Definition flexiport.h:44