Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members

SocketStream.h

00001 #ifndef _SOCKETSTREAM_H_
00002 #define _SOCKETSTREAM_H_
00003 
00004 //Stream wrapper
00005 //Dominic Letourneau 03/10/2001
00006 
00007 #include <stddef.h>
00008 #include <sys/socket.h>
00009 #include <netinet/in.h>
00010 #include "Object.h"
00011 #include "net_types.h"
00012 #include <iostream>
00013 #include <stdio.h>
00014 #include <sys/types.h>
00015 #include <sys/stat.h>
00016 #include <fcntl.h>
00017 
00018 #ifndef WIN32
00019 #include <unistd.h>
00020 #endif
00021 
00022 namespace FD {
00023 
00036 class network_socket {
00037 
00038  public:
00039 
00041   static const int BROADCAST_TYPE;
00042 
00044   static const int TCP_STREAM_TYPE;
00045 
00046  public:
00047 
00054   network_socket(int type, int port);
00055 
00056   
00058   ~network_socket();
00059 
00064   void printOn (std::ostream &out) const;
00065 
00067   void init_broadcast();
00068 
00069 
00075   size_t send_packet(unsigned char *packet, size_t size);
00076 
00077 
00083   size_t recv_packet(unsigned char *packet, size_t size);
00084 
00086   void shutdown();
00087 
00092   void socket_connect(const char *host);
00093 
00094 
00100   void socket_listen(int backlog, bool blocking);
00101 
00102 
00106   void socket_accept();
00107 
00112   int get_type() {return m_type;}
00113 
00114 
00119   int get_port() {return m_port;}
00120 
00125   int get_read_socket() {return m_read_socket;}
00126 
00131   int get_write_socket() {return m_write_socket;}
00132 
00137   int get_listen_socket() {return m_listen_socket;}
00138 
00139  private:
00140   
00142   void init_tcp_stream(bool blocking);
00143 
00145   bool m_blocking;
00146 
00148   int m_port;
00149 
00151   int m_type;
00152 
00154   int m_listen_socket;
00155   
00157   int m_write_socket;
00158   sockaddr_in m_write_addr;
00159   
00161   int m_read_socket;
00162   sockaddr_in m_read_addr;
00163 
00164 };
00165 
00172 class socket_streambuf : public std::streambuf, public network_socket {
00173 
00174   protected:
00175    virtual int overflow(int = EOF);
00176    virtual std::streamsize xsputn(const char *s, std::streamsize n);
00177 
00178    virtual int uflow();
00179    virtual int underflow();
00180    virtual std::streamsize xsgetn(char *s, std::streamsize n);
00181    virtual int pbackfail(int c);
00182 
00183   public:
00184    socket_streambuf(int type, int port);
00185 
00186    virtual ~socket_streambuf() {
00187 
00188    }
00189 
00190   protected:
00191 
00192    bool owner;
00193    bool takeFromBuf;
00194    char charBuf;
00195 };
00196 
00203 class socket_ostream : public std::ostream {
00204   socket_streambuf _streambuffer; 
00205   public:
00206 
00207   operator socket_streambuf&(){return _streambuffer;}
00208 
00209   socket_ostream(int type, int port)
00210     : _streambuffer (type,port)
00211     , std::ostream(&_streambuffer)
00212     {clear();}
00213 };
00214 
00221 class socket_istream : public std::istream {
00222   socket_streambuf _streambuffer;
00223   public:
00224 
00225   operator socket_streambuf&(){return _streambuffer;}
00226   
00227   socket_istream(int type, int port)
00228     : _streambuffer (type, port)
00229     , std::istream(&_streambuffer)
00230     {clear();}
00231 };
00232 
00239 class socket_iostream : public std::iostream {
00240   socket_streambuf _streambuffer;
00241   public:
00242 
00243   operator socket_streambuf&(){return _streambuffer;}
00244   
00245   socket_iostream(int type, int port)
00246     : _streambuffer (type, port)
00247     , std::iostream(&_streambuffer)
00248     {clear();}
00249 };
00250 
00251 }//namespace FD
00252 #endif

Generated on Wed Oct 5 14:28:56 2005 for FlowDesigner by  doxygen 1.4.4