pike.git / lib / modules / Protocols.pmod / Line.pmod

version» Context lines:

pike.git/lib/modules/Protocols.pmod/Line.pmod:1:   /* -  * $Id: Line.pmod,v 1.13 2000/12/23 17:03:05 grubba Exp $ +  * $Id: Line.pmod,v 1.14 2000/12/28 14:34:20 grubba Exp $    *    * Line-buffered protocol handling.    *    * Henrik Grubbström 1998-05-27    */      #pike __REAL_VERSION__      //! Simple nonblocking line-oriented I/O.   class simple   {    static object con;    static constant line_separator = "\r\n";       //! If this variable has been set, multiple lines will be accumulated, -  //! until a line with a single @tt{.@} (period) is received. @[handle_data()] -  //! will then be called with the accumulated data as the argument. +  //! until a line with a single @tt{'.'@} (period) is received. +  //! @[handle_data()] will then be called with the accumulated data +  //! as the argument.    //!    //! @note    //! @[handle_data()] is one-shot, ie it will be cleared when it is called.    //! -  //! The line with the single @tt{.@} (period) will not be in the +  //! The line with the single @tt{'.'@} (period) will not be in the    //! accumulated data.    //!    //! @seealso    //! @[handle_command()]    //!    function(string:void) handle_data;       //! This function will be called once for every line that is received.    //!    //! Overload this function as appropriate.
pike.git/lib/modules/Protocols.pmod/Line.pmod:124:    multi_line_buffer = "";    handle(data);    }    } else {    handle_command(line);    }    }       static string read_buffer = "";    +  //! Read a line from the input. +  //! +  //! @returns +  //! Returns @tt{0@} when more input is needed. +  //! Returns the requested line otherwise. +  //! +  //! @note +  //! The returned line will not contain the line separator. +  //! +  //! @seealso +  //! @[handle_command()], @[line_separator] +  //!    static string read_line()    {    // FIXME: Should probably keep track of where the search ended last time.    int i = search(read_buffer, line_separator);    if (i == -1) {    return 0;    }    string data = read_buffer[..i-1]; // Not the line separator.    read_buffer = read_buffer[i+sizeof(line_separator)..];       return data;    }    -  +  //! Called when data has been received. +  //! +  //! Overload as appropriate. +  //! +  //! Calls the handle callbacks repeatedly until no more lines are available. +  //! +  //! @seealso +  //! @[handle_data()], @[handle_command()], @[read_line()] +  //!    static void read_callback(mixed ignored, string data)    {    touch_time();       read_buffer += data;       string line;       while( (line = read_line()) )    _handle_command(line);
pike.git/lib/modules/Protocols.pmod/Line.pmod:271:    local::timeout = timeout;       // Start the timeout handler.    touch_time();    _timeout_cb();       con->set_nonblocking(read_callback, 0, close_callback);    }   };    + //! Nonblocking line-oriented I/O with support for sending SMTP-style codes.   class smtp_style   {    inherit simple;    -  +  //! @decl mapping(int:string|array(string)) errorcodes +  //! +  //! Mapping from return-code to error-message. +  //! +  //! Overload this constant as apropriate. +  //!    constant errorcodes = ([]);    -  void send(int code, array(string)|string|void lines) +  //! Send an SMTP-style return-code. +  //! +  //! @[code] is an SMTP-style return-code. +  //! +  //! If @[lines] is omitted, @[errorcodes] will be used to lookup +  //! an appropriate error-message. +  //! +  //! If @[lines] is a string, it will be split on @tt{'\n'@} (newline), +  //! and the error-code interspersed as appropriate. +  //! +  //! @seealso +  //! @[errorcodes] +  //! +  void send(int(100,999) code, array(string)|string|void lines)    {    lines = lines || errorcodes[code] || "Error";       if (stringp(lines)) {    lines /= "\n";    }       string init = sprintf("%03d", code);    string res = "";       int i;    for(i=0; i < sizeof(lines)-1; i++) { -  res += init + "-" + lines[i] + "\r\n"; +  res += init + "-" + lines[i] + line_separator;    } -  res += init + " " + lines[-1] + "\r\n"; +  res += init + " " + lines[-1] + line_separator;    send_q->put(res);    con->set_write_callback(write_callback);    }   };    -  + //! Nonblocking line-oriented I/O with support for reading literals.   class imap_style   {    inherit simple;    -  function handle_literal = 0; -  function handle_line = 0; -  +  //! Length in bytes of the literal to read.    int literal_length;    - #if 0 -  function timeout_handler = 0; - #endif +  //! If this variable has been set, @[literal_length] bytes will be +  //! accumulated, and this function will be called with the resulting data. +  //! +  //! @note +  //! @[handle_literal()] is one-shot, ie it will be cleared when it is called. +  //! +  function(string:void) handle_literal;    -  +  //! This function will be called once for every line that is received. +  //! +  //! @note +  //! This API is provided for backward compatibility; overload +  //! @[handle_command()] instead. +  //! +  //! @seealso +  //! @[handle_command()] +  function(string:void) handle_line; +  +  void handle_command(string line) +  { +  handle_line(line); +  } +     static void read_callback(mixed ignored, string data)    {    touch_time();       read_buffer += data;       while(1) {    if (handle_literal)    {    if (strlen(read_buffer) < literal_length)
pike.git/lib/modules/Protocols.pmod/Line.pmod:332:    string literal = read_buffer[..literal_length - 1];    read_buffer = read_buffer[literal_length..];       function handler = handle_literal;    handle_literal = 0;       handler(literal);    } else {    string line = read_line();    if (line) -  handle_line(line); +  handle_command(line);    else    break;    }    }    }    -  void expect_literal(int length, function callback) +  //! Enter literal reading mode. +  //! +  //! Sets @[literal_length] and @[handle_literal()]. +  //! +  //! @seealso +  //! @[literal_length], @[handle_literal()] +  //! +  void expect_literal(int length, function(string:void) callback)    {    literal_length = length;    handle_literal = callback;    } -  - #if 0 -  static void do_timeout() -  { -  if (timeout_handler) -  { -  con->set_read_callback(0); -  con->set_close_callback(0); -  -  timeout_handler(); -  } else -  ::do_timeout(); +    } -  -  void create(object con_, int|void timeout_, function|void callback) -  { -  timeout_handler = callback; -  ::create(con_, timeout_); -  } - #endif - } +