tcpclient.h

Go to the documentation of this file.
00001 /* TCP client class.
00002  *
00003  * PegSoft sockets library (c) 2007 PegSoft
00004  * Contact us at pegsoft@pegsoft.net
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program (see the file COPYING); if not, write to the
00018  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  */
00020 
00024 #ifndef SOCKETS_TCPCLIENT_H
00025 #define SOCKETS_TCPCLIENT_H
00026 
00027 /*************************************************************************/
00028 
00029 #include <string>
00030 
00031 #include <sockets/ipsocket.h>
00032 
00033 /*************************************************************************/
00034 
00035 namespace sockets
00036 {
00037 
00038 /*************************************************************************/
00039 
00040 class Buffer;
00041 
00042 /*************************************************************************/
00043 
00045 
00052 class TCPClient : public IPSocket
00053 {
00054     public:
00055         /* Constants */
00056         
00058         enum
00059         {
00061             poInBuf = 0x0001,
00063             poLine = 0x0002
00064         };
00065         
00066         /* Functions */
00067         
00069 
00076         explicit TCPClient(Buffer *rbuf = 0, Buffer *wbuf = 0) :
00077                 IPSocket(SOCK_STREAM), m_rbuf(rbuf), m_wbuf(wbuf),
00078                 m_linesep("\r\n") { }
00080 
00082         TCPClient(TCPClient const &tcpc) : IPSocket(tcpc), m_rbuf(0),
00083                 m_wbuf(0), m_linesep(tcpc.m_linesep) { }
00085         virtual TCPClient &operator=(TCPClient const &right);
00086         
00088 
00105         virtual bool assign(int fd, uint32_t status);
00106         
00108 
00113         virtual bool close();
00114         
00116 
00128         virtual ssize_t read(char *buf, size_t len);
00129         
00131 
00147         virtual ssize_t read_inbuf(size_t max = 0);
00148         
00150 
00165         virtual ssize_t read_line(char *buf, size_t len) const;
00166         
00168 
00198         virtual ssize_t write(char const *buf, size_t len, bool inbuf = false);
00200         virtual ssize_t write_str(char const *str, bool inbuf = false)
00201                 { return write(str, strlen(str), inbuf); }
00203         virtual ssize_t write_string(std::string const &str, bool inbuf = false)
00204                 { return write(str.data(), str.size(), inbuf); }
00205         
00207 
00219         virtual ssize_t write_line(char const *line, bool inbuf = false);
00220         
00222 
00233         virtual ssize_t printf(uint32_t options, char const *format,
00234                 ...) FORMAT(printf,3,4);
00235         
00237 
00248         virtual ssize_t vprintf(uint32_t options, char const *format,
00249                 va_list args);
00250         
00252 
00264         virtual ssize_t write_outbuf(size_t max = 0);
00265         
00267 
00277         virtual ssize_t write_unbuffered(char const *buf, size_t len);
00278         
00280 
00286         Buffer *rbuf() const { return m_rbuf; }
00288 
00298         Buffer *rbuf(Buffer *rbuf) { return (m_rbuf = rbuf); }
00299         
00301 
00306         Buffer *wbuf() const { return m_wbuf; }
00308 
00317         Buffer *wbuf(Buffer *wbuf) { return (m_wbuf = wbuf); }
00318         
00320 
00324         std::string const &linesep() const { return m_linesep; }
00326         std::string const &linesep(std::string const linesep)
00327                 { return (m_linesep = linesep); } 
00328     protected:
00330 
00340         virtual ssize_t read_real(void *buf, size_t len);
00341         
00343 
00352         virtual ssize_t write_real(void const *buf, size_t len);
00353         
00355 
00362         virtual bool read_event();
00363         
00365 
00377         virtual bool write_event();
00378         
00380 
00390         virtual bool on_connect(int error) { return true; }
00391         
00393 
00399         virtual bool on_read() { return true; }
00400         
00402 
00412         virtual bool on_write() { return true; }
00413         
00415 
00428         virtual bool on_writing() { return true; }
00429     private:
00430         Buffer *m_rbuf;
00431         Buffer *m_wbuf;
00432         
00433         std::string m_linesep;
00434 };
00435 
00436 /*************************************************************************/
00437 
00438 } /* namespace sockets */
00439 
00440 /*************************************************************************/
00441 
00442 #endif /* SOCKETS_TCPCLIENT_H */

Generated on Sun May 20 21:32:15 2007 for Epona API by  doxygen 1.4.6