Flexiport 2.0.0

udpport.h

Go to the documentation of this file.
00001 /* Flexiport
00002  *
00003  * Header file for the UDP port class.
00004  *
00005  * Copyright 2008-2011 Geoffrey Biggs geoffrey.biggs@aist.go.jp
00006  *     RT-Synthesis Research Group
00007  *     Intelligent Systems Research Institute,
00008  *     National Institute of Advanced Industrial Science and Technology (AIST),
00009  *     Japan
00010  *     All rights reserved.
00011  *
00012  * This file is part of Flexiport.
00013  *
00014  * Flexiport is free software; you can redistribute it and/or modify it
00015  * under the terms of the GNU Lesser General Public License as published
00016  * by the Free Software Foundation; either version 2.1 of the License,
00017  * or (at your option) any later version.
00018  *
00019  * Flexiport is distributed in the hope that it will be useful, but
00020  * WITHOUT ANY WARRANTY; without even the implied warranty of
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00022  * Lesser General Public License for more details.
00023  *
00024  * You should have received a copy of the GNU Lesser General Public
00025  * License along with Flexiport. If not, see
00026  * <http://www.gnu.org/licenses/>.
00027  */
00028 
00029 #ifndef __UDPPORT_H
00030 #define __UDPPORT_H
00031 
00032 #include <flexiport/port.h>
00033 #include <flexiport/config.h>
00034 
00035 #include <map>
00036 #include <string>
00037 #if !defined (WIN32)
00038     #include <netinet/in.h>
00039 #endif
00040 
00045 namespace flexiport
00046 {
00047 
00083 class FLEXIPORT_EXPORT UDPPort : public Port
00084 {
00085     public:
00086         UDPPort (std::map<std::string, std::string> options);
00087         ~UDPPort ();
00088 
00092         void Open ();
00094         void Close ();
00096         ssize_t Read (void * const buffer, size_t count);
00098         ssize_t ReadFull (void * const buffer, size_t count);
00100         ssize_t ReadUntil (void * const buffer, size_t count, uint8_t terminator);
00102         ssize_t ReadStringUntil (std::string &buffer, char terminator);
00104         ssize_t Skip (size_t count);
00107         ssize_t SkipUntil (uint8_t terminator, unsigned int count);
00109         ssize_t BytesAvailable ();
00111         ssize_t BytesAvailableWait ();
00113         ssize_t Write (const void * const buffer, size_t count);
00115         void Flush ();
00117         void Drain ();
00119         std::string GetStatus () const;
00121         void SetTimeout (Timeout timeout);
00123         void SetCanRead (bool canRead);
00125         void SetCanWrite (bool canWrite);
00127         bool IsOpen () const                        { return _open; }
00128 
00129     private:
00130 #if !defined (WIN32)
00131     #if defined (FLEXIPORT_HAVE_GETADDRINFO)
00132         struct sockaddr _destSockAddr;
00133     #else
00134         struct sockaddr_in _destSockAddr;
00135     #endif
00136 #endif // !defined (WIN32)
00137         int _sendSock;      // Socket to send data from.
00138         int _recvSock;      // Socket to receive data on.
00139 
00140         std::string _destIP;
00141         unsigned int _destPort;
00142         std::string _recvIP;
00143         unsigned int _recvPort;
00144         bool _open;
00145 
00146         void CheckPort (bool read);
00147 
00148         bool ProcessOption (const std::string &option, const std::string &value);
00149 
00150         void OpenSender ();
00151         void CloseSender ();
00152         void OpenReceiver ();
00153         void CloseReceiver ();
00154         typedef enum {TIMED_OUT, DATA_AVAILABLE, CAN_WRITE} WaitStatus;
00155         WaitStatus WaitForDataOrTimeout ();
00156         bool IsDataAvailable ();
00157         WaitStatus WaitForWritableOrTimeout ();
00158         void SetSocketBlockingFlag ();
00159 };
00160 
00161 } // namespace flexiport
00162 
00165 #endif // __UDPPORT_H
00166 
 All Classes Namespaces Files Functions Variables Defines