123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- #pragma once
- #include <iostream>
- #include "TCPSocket.h"
- #include "TLSSocket.h"
- namespace bell {
- class SocketBuffer : public std::streambuf {
- private:
- std::unique_ptr<bell::Socket> internalSocket;
- static const int bufLen = 1024;
- char ibuf[bufLen], obuf[bufLen];
- public:
- SocketBuffer() { internalSocket = nullptr; }
- SocketBuffer(const std::string& hostname, int port, bool isSSL = false) {
- open(hostname, port);
- }
- int open(const std::string& hostname, int port, bool isSSL = false);
- int close();
- bool isOpen() {
- return internalSocket != nullptr && internalSocket->isOpen();
- }
- ~SocketBuffer() { close(); }
- protected:
- virtual int sync();
- virtual int_type underflow();
- virtual int_type overflow(int_type c = traits_type::eof());
- virtual std::streamsize xsgetn(char_type* __s, std::streamsize __n);
- virtual std::streamsize xsputn(const char_type* __s, std::streamsize __n);
- };
- class SocketStream : public std::iostream {
- private:
- SocketBuffer socketBuf;
- public:
- SocketStream() : std::iostream(&socketBuf) {}
- SocketStream(const std::string& hostname, int port, bool isSSL = false)
- : std::iostream(&socketBuf) {
- open(hostname, port, isSSL);
- }
- SocketBuffer* rdbuf() { return &socketBuf; }
- int open(const std::string& hostname, int port, bool isSSL = false) {
- int err = socketBuf.open(hostname, port, isSSL);
- if (err)
- setstate(std::ios::failbit);
- return err;
- }
- int close() { return socketBuf.close(); }
- bool isOpen() { return socketBuf.isOpen(); }
- };
- } // namespace bell
|