Method Stdio.Buffer()->set_error_mode()
- Method set_error_mode
Buffer
set_error_mode(int
m
)
Buffer
set_error_mode(program
m
)- Description
Set the error mode of this buffer to
m
.If true operations that would normally return 0 (like trying to read too much) will instead throw an error. If
m
is a program a clone of it will be thrown on error.This is useful when parsing received data, you do not have to verify that each and every read operation suceeds.
However, the non-error mode is more useful when checking to see if a packet/segment/whatever has arrived.
The thrown error object will have the constant buffer_error set to a non-false value.
- Example
void read_callback(int i, string new_data) { inbuffer->add( new_data ); while( Buffer packet = inbuffer->read_hbuffer(2) ) { packet->set_error_mode(Buffer.THROW_ERROR); if( mixed e = catch( handle_packet( packet ) ) ) if( e->buffer_error ) protocol_error(); // illegal data in packet else throw(e); // the other code did something bad } } void handle_packet( Buffer pack ) { switch( pack->read_int8() ) { ... case HEADER_FRAME: int num_headers = pack->read_int32(); for( int i = 0; i<num_headers; i++ ) headers[pack->read_hstring(2)] = pack->read_hstring(2); ... } }