protocol_stream.h
上传用户:psq1974
上传日期:2007-01-06
资源大小:1195k
文件大小:3k
- /* Copyright (C) 1998, 1999 State University of New York at Stony Brook
- Author: Andrew V. Shuvalov ( andrew@ecsl.cs.sunysb.edu )
- Software license is located in file "COPYING"
- */
- #ifndef protocol_stream_h
- #define protocol_stream_h
- #ifndef WIN32
- # define SOCKET int
- #endif
- /** subclass if iostream, hide from user the communication details.
- After discovering many-many bugs in NT and VC 5.0 this is replaced by
- my own stream, not subclassing from iostream
- */
- class protocol_stream
- # ifndef WIN32
- : public iostream
- # endif
- {
- SOCKET sock;
- # ifdef WIN32
- /** buffer input data here */
- char input_buffer[1024];
- unsigned input_buffer_offset;
- unsigned input_buffer_end;
- # endif
- bool before_mark;
- bool everything_read;
- std::vector< std::string > messages;
- const static char prompt[];
- const static char command[];
- const int genericTimeout;
- public:
- /** at the beginning set the everything_read to false, because we have
- prompt
- */
- protocol_stream( SOCKET sock, int gen_timeout );
- /** s will remains empty only if it is nothing to read */
- protocol_stream& operator >>( std::string &s );
- /** assume that there are nothing on that string except this integer,
- this is simplification of protocol, be warned
- */
- protocol_stream& operator >>( int &value );
- /** download everything between -> and <- */
- protocol_stream& operator >>( std::vector< std::string > &lines );
-
- protocol_stream& operator <<( const std::string &s );
- /** we know that input is available */
- void wait_input() { everything_read = false; before_mark = true; }
- /** we don't know, but curious that is the input available: test only
- the socket by 'select' */
- bool test_for_input( int millisec );
- /** test not only socket, but the buffer as well */
- bool test_for_any_pending_input( int millisec );
- /** test if s is equal to the word 'command' */
- bool is_command( const std::string &s ) {
- return 0 == s.compare( command );
- }
- # ifdef WIN32
- bool buffer_is_empty();
- bool eof();
- /** returns -1 if no input, or the next char */
- int peek();
- int get( char &location );
- char *getline( char *buf, unsigned size );
- # endif
- /** before reading, check for everything_read flag.
- If nothing to wait from input, return.
- After sending the command that should return something, user must
- call wait_input, which indicates that sequence of -> and <-
- should appear in input before '>>>'
- */
- void skip_to_prompt();
- void skip_to_prompt( std::vector< std::string > &messages );
- void append_messages( std::vector< std::string > &msgs );
- };
- #endif // protocol_stream_h