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 <memory>
00030 #include <string>
00031 
00032 #include <sockets/ipsocket.h>
00033 #include <sockets/ssl.h>
00034 
00035 /*************************************************************************/
00036 
00037 namespace sockets
00038 {
00039 
00040 /*************************************************************************/
00041 
00042 class Buffer;
00043 
00044 /*************************************************************************/
00045 
00047 
00054 class TCPClient : public IPSocket
00055 {
00056     public:
00057         /* Constants */
00058         
00060         enum
00061         {
00063             poInBuf = 0x0001,
00065             poLine = 0x0002
00066         };
00067         
00068         /* Functions */
00069         
00071 
00078         explicit TCPClient(Buffer *rbuf = 0, Buffer *wbuf = 0) :
00079                 IPSocket(SOCK_STREAM), m_rbuf(rbuf), m_wbuf(wbuf),
00080                 m_ssl(0), m_linesep("\r\n") { }
00082 
00084         TCPClient(TCPClient const &tcpc);
00086         virtual TCPClient &operator=(TCPClient const &right);
00087         
00089 
00106         virtual bool assign(int fd, uint32_t status);
00107         
00109 
00114         virtual bool close();
00115         
00117 
00129         virtual ssize_t read(char *buf, size_t len);
00130         
00132 
00148         virtual ssize_t read_inbuf(size_t max = 0);
00149         
00151 
00166         virtual ssize_t read_line(char *buf, size_t len) const;
00167         
00169 
00200         virtual ssize_t write(char const *buf, size_t len, bool inbuf = false);
00202         virtual ssize_t write_str(char const *str, bool inbuf = false)
00203                 { return write(str, strlen(str), inbuf); }
00205         virtual ssize_t write_string(std::string const &str, bool inbuf = false)
00206                 { return write(str.data(), str.size(), inbuf); }
00207         
00209 
00221         virtual ssize_t write_line(char const *line, bool inbuf = false);
00222         
00224 
00235         virtual ssize_t printf(uint32_t options, char const *format,
00236                 ...) FORMAT(printf,3,4);
00237         
00239 
00250         virtual ssize_t vprintf(uint32_t options, char const *format,
00251                 va_list args);
00252         
00254 
00266         virtual ssize_t write_outbuf(size_t max = 0);
00267         
00269 
00279         virtual ssize_t write_unbuffered(char const *buf, size_t len);
00280         
00282 
00288         Buffer *rbuf() const { return m_rbuf; }
00290 
00300         Buffer *rbuf(Buffer *rbuf) { return (m_rbuf = rbuf); }
00301         
00303 
00308         Buffer *wbuf() const { return m_wbuf; }
00310 
00319         Buffer *wbuf(Buffer *wbuf) { return (m_wbuf = wbuf); }
00320         
00322 
00335         bool set_ssl(SSLContext const *context);
00336         
00338         SSLInfo const *ssl() const { return m_ssl.get(); }
00339         
00341 
00345         std::string const &linesep() const { return m_linesep; }
00347         std::string const &linesep(std::string const linesep)
00348                 { return (m_linesep = linesep); } 
00349     protected:
00351 
00352         virtual bool create();
00353         
00355 
00365         virtual ssize_t read_real(void *buf, size_t len);
00366         
00368 
00377         virtual ssize_t write_real(void const *buf, size_t len);
00378         
00380 
00387         virtual bool read_event();
00388         
00390 
00402         virtual bool write_event();
00403         
00405 
00415         virtual bool on_connect(int error) { return true; }
00416         
00418 
00424         virtual bool on_read() { return true; }
00425         
00427 
00437         virtual bool on_write() { return true; }
00438         
00440 
00453         virtual bool on_writing() { return true; }
00454         
00456         SSLInfo *ssl() { return m_ssl.get(); }
00457     private:
00458         Buffer *m_rbuf;
00459         Buffer *m_wbuf;
00460         
00461         std::auto_ptr<SSLInfo> m_ssl;
00462         
00463         std::string m_linesep;
00464 };
00465 
00466 /*************************************************************************/
00467 
00468 } /* namespace sockets */
00469 
00470 /*************************************************************************/
00471 
00472 #endif /* SOCKETS_TCPCLIENT_H */

Generated on Fri Apr 18 22:03:27 2008 for Epona API by  doxygen 1.5.3