00001 #ifndef GLUE_NET_NET_STREAM_H
00002 #define GLUE_NET_NET_STREAM_H
00003 #include "tty/tty.H"
00004 #include "mem_push.H"
00005 #include "std/strstream.H"
00006 #include "std/fstream.H"
00007 #include "std/strings.H"
00008
00009 class Network;
00010
00012
00013
00014
00015
00016
00017
00018
00019
00021
00022 friend class Network;
00023 public:
00024 enum StreamFlags {
00025 write = 0x1,
00026 read = 0x2,
00027 rw = 0x3,
00028 ascii = 0x4,
00029 ascii_w = 0x5,
00030 ascii_r = 0x6,
00031 ascii_rw = 0x7
00032 };
00033
00034 NetStream (int port, const char *name);
00035 NetStream (int fd, struct sockaddr_in *client,bool block=false);
00036 NetStream& operator= (const NetStream&);
00037 virtual ~NetStream();
00038
00039 str_ptr name (void) const { return name_; }
00040 str_ptr print_name (void) const { return print_name_; }
00041 int port (void) const { return port_; }
00042 int processing (void) const { return processing_; }
00043 Network* network (void) const { return network_; }
00044 void subscribe (Cstr_ptr &t){ tags_ += t; }
00045 void subscribe (Cstr_list &t){ tags_ = t; }
00046
00047 void set_port (int p);
00048 void add_network(Network *n) { network_ = n; }
00049 virtual void flush_data (void);
00050 int read_stuff (void);
00051
00052 virtual void sample();
00053
00054
00055 void no_tcp_delay();
00056 void flush();
00057
00058 static void no_linger(int fd);
00059 static NetStream *ostream_to_NetStream(ostream *os)
00060 { return ostream_map.find((long) os);}
00061 ostream *ostr() { return _ostr;}
00062 istream *istr() { return _istr;}
00063
00064 protected:
00065 void set_blocking(bool val) const;
00066 ssize_t write_to_net (const void *buf, size_t nbyte) const;
00067 ssize_t read_from_net( void *buf, size_t nbyte) const;
00068 int interpret (void);
00069 void remove_me();
00070 ostrstream *_ostr;
00071 istream *_istr;
00072 STDmem_queue _in_queue;
00073 ARRAY<char> _charbuf;
00074 void init_streams(bool readable, bool writeable);
00075
00076 private:
00077
00078 void _die(const char *msg);
00079
00080 str_list tags_;
00081 str_ptr print_name_;
00082 str_ptr name_;
00083 int port_;
00084 int msgSize_;
00085 int processing_;
00086 Network *network_;
00087 static HASH<NetStream *> ostream_map;
00088 };
00089
00090 enum NETenum {
00091 NETadd_connection,
00092 NETquit,
00093 NETflush,
00094 NETapp,
00095 NETidentify,
00096 NETbroadcast,
00097 NETbarrier,
00098 NETswap_ack,
00099 NETbad
00100 };
00101 DllImpExp istream &operator >> (istream &, NETenum &);
00102 DllImpExp ostream &operator << (ostream &, NETenum );
00103 #endif