Flexiport 2.0.0
|
00001 /* Flexiport 00002 * 00003 * Header file for the TCP 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 __TCPPORT_H 00030 #define __TCPPORT_H 00031 00032 #include <flexiport/port.h> 00033 00034 #include <map> 00035 #include <string> 00036 00041 namespace flexiport 00042 { 00043 00061 class FLEXIPORT_EXPORT TCPPort : public Port 00062 { 00063 public: 00064 TCPPort (std::map<std::string, std::string> options); 00065 ~TCPPort (); 00066 00072 void Open (); 00074 void Close (); 00076 ssize_t Read (void * const buffer, size_t count); 00078 ssize_t ReadFull (void * const buffer, size_t count); 00080 ssize_t BytesAvailable (); 00082 ssize_t BytesAvailableWait (); 00084 ssize_t Write (const void * const buffer, size_t count); 00086 void Flush (); 00088 void Drain (); 00090 std::string GetStatus () const; 00092 void SetTimeout (Timeout timeout); 00094 void SetCanRead (bool canRead); 00096 void SetCanWrite (bool canWrite); 00098 bool IsOpen () const { return _open; } 00099 00100 private: 00101 int _sock; // Socket connected to wherever the data is coming from. 00102 int _listenSock; // Socket to listen on when in listen mode. 00103 00104 std::string _ip; 00105 unsigned int _port; 00106 bool _isListener; // True if this port should listen instead of actively connecting. 00107 bool _open; 00108 00109 void CheckPort (bool read); 00110 00111 bool ProcessOption (const std::string &option, const std::string &value); 00112 00113 void Connect (); 00114 void WaitForConnection (); 00115 typedef enum {TIMED_OUT, DATA_AVAILABLE, CAN_WRITE} WaitStatus; 00116 WaitStatus WaitForDataOrTimeout (); 00117 bool IsDataAvailable (); 00118 WaitStatus WaitForWritableOrTimeout (); 00119 void SetSocketBlockingFlag (); 00120 }; 00121 00122 } // namespace flexiport 00123 00126 #endif // __TCPPORT_H 00127