xapian-core  1.4.27
Public Member Functions | Protected Attributes | Private Member Functions | Private Attributes | List of all members
RemoteConnection Class Reference

A RemoteConnection object provides a bidirectional connection to another RemoteConnection object on a remote machine. More...

#include <remoteconnection.h>

+ Inheritance diagram for RemoteConnection:
+ Collaboration diagram for RemoteConnection:

Public Member Functions

 RemoteConnection (int fdin_, int fdout_, const std::string &context_=std::string())
 Constructor. More...
 
bool ready_to_read () const
 See if there is data available to read. More...
 
int sniff_next_message_type (double end_time)
 Check what the next message type is. More...
 
int get_message (std::string &result, double end_time)
 Read one message from fdin. More...
 
int get_message_chunked (double end_time)
 Prepare to read one message from fdin in chunks. More...
 
int get_message_chunk (std::string &result, size_t at_least, double end_time)
 Read a chunk of a message from fdin. More...
 
int receive_file (const std::string &file, double end_time)
 Save the contents of a message as a file. More...
 
void send_message (char type, const std::string &s, double end_time)
 Send a message. More...
 
void send_file (char type, int fd, double end_time)
 Send the contents of a file as a message. More...
 
void shutdown ()
 Shutdown the connection. More...
 
void do_close ()
 Close the connection. More...
 

Protected Attributes

std::string context
 The context to report with errors. More...
 

Private Member Functions

void operator= (const RemoteConnection &)
 Don't allow assignment. More...
 
 RemoteConnection (const RemoteConnection &)
 Don't allow copying. More...
 
bool read_at_least (size_t min_len, double end_time)
 Read until there are at least min_len bytes in buffer. More...
 
ssize_t send_or_write (const void *p, size_t n)
 Helper which calls send() or write(). More...
 

Private Attributes

int fdin
 The file descriptor used for reading. More...
 
int fdout
 The file descriptor used for writing. More...
 
std::string buffer
 Buffer to hold unprocessed input. More...
 
off_t chunked_data_left
 Remaining bytes of message data still to come over fdin for a chunked read. More...
 

Detailed Description

A RemoteConnection object provides a bidirectional connection to another RemoteConnection object on a remote machine.

The connection is implemented using a pair of file descriptors. Messages with a single byte type code and arbitrary data as the contents can be sent and received.

Definition at line 169 of file remoteconnection.h.

Constructor & Destructor Documentation

◆ RemoteConnection() [1/2]

RemoteConnection::RemoteConnection ( const RemoteConnection )
private

Don't allow copying.

◆ RemoteConnection() [2/2]

RemoteConnection::RemoteConnection ( int  fdin_,
int  fdout_,
const std::string &  context_ = std::string() 
)

Constructor.

Definition at line 90 of file remoteconnection.cc.

References context, and fdout.

Member Function Documentation

◆ do_close()

void RemoteConnection::do_close ( )

Close the connection.

Definition at line 832 of file remoteconnection.cc.

References close_fd_or_socket(), context, RealTime::end_time(), fdin, fdout, LOGCALL_VOID, RealTime::now(), and throw_timeout().

Referenced by RemoteDatabase::do_close().

◆ get_message()

int RemoteConnection::get_message ( std::string &  result,
double  end_time 
)

Read one message from fdin.

Parameters
[out]resultMessage data.
end_timeIf this time is reached, then a timeout exception will be thrown. If (end_time == 0.0) then the operation will never timeout.
Returns
Message type code or -1 for EOF.

Definition at line 626 of file remoteconnection.cc.

References buffer, fdin, LOGCALL, read_at_least(), RETURN, throw_database_closed(), and throw_network_error_insane_message_length().

Referenced by Xapian::DatabaseReplica::Internal::apply_db_copy(), Xapian::DatabaseReplica::Internal::apply_next_changeset(), RemoteServer::get_message(), RemoteDatabase::get_message(), ReplicateTcpServer::handle_one_connection(), and RemoteDatabase::send_message().

◆ get_message_chunk()

int RemoteConnection::get_message_chunk ( std::string &  result,
size_t  at_least,
double  end_time 
)

Read a chunk of a message from fdin.

You must call get_message_chunked() before calling this method.

Parameters
[in,out]resultMessage data. This is appended to, so if you read more than needed the previous time, leave the excess in result.
at_leastReturn at least this many bytes in result, unless there isn't enough data left in the message (in which case all remaining data is read and 0 is returned).
end_timeIf this time is reached, then a timeout exception will be thrown. If (end_time == 0.0) then the operation will never timeout.
Returns
1 if at least at_least bytes are now in result; -1 on EOF on the connection; 0 for having read < at_least bytes, but finished the message.

Definition at line 722 of file remoteconnection.cc.

References buffer, chunked_data_left, fdin, LOGCALL, read_at_least(), RETURN, and throw_database_closed().

Referenced by ChertDatabaseReplicator::apply_changeset_from_conn(), GlassDatabaseReplicator::apply_changeset_from_conn(), ChertDatabaseReplicator::process_changeset_chunk_base(), GlassDatabaseReplicator::process_changeset_chunk_blocks(), ChertDatabaseReplicator::process_changeset_chunk_blocks(), and GlassDatabaseReplicator::process_changeset_chunk_version().

◆ get_message_chunked()

int RemoteConnection::get_message_chunked ( double  end_time)

Prepare to read one message from fdin in chunks.

Sometimes a message can be sufficiently large that you don't want to read it all into memory before processing it. Also, it may be more efficient to process it as you go.

This method doesn't actually return any message data - call get_message_chunk() to do that.

Parameters
end_timeIf this time is reached, then a timeout exception will be thrown. If (end_time == 0.0) then the operation will never timeout.
Returns
Message type code or -1 for EOF.

Definition at line 667 of file remoteconnection.cc.

References buffer, chunked_data_left, fdin, LOGCALL, rare, read_at_least(), RETURN, throw_database_closed(), and throw_network_error_insane_message_length().

Referenced by ChertDatabaseReplicator::apply_changeset_from_conn(), GlassDatabaseReplicator::apply_changeset_from_conn(), and receive_file().

◆ operator=()

void RemoteConnection::operator= ( const RemoteConnection )
private

Don't allow assignment.

◆ read_at_least()

bool RemoteConnection::read_at_least ( size_t  min_len,
double  end_time 
)
private

Read until there are at least min_len bytes in buffer.

If for some reason this isn't possible, returns false upon EOF and otherwise throws NetworkError.

Parameters
min_lenMinimum number of bytes required in buffer.
end_timeIf this time is reached, then a timeout exception will be thrown. If (end_time == 0.0), then keep trying indefinitely.
Returns
false on EOF, otherwise true.

Definition at line 158 of file remoteconnection.cc.

References Assert, buffer, CHUNKSIZE, context, fdin, LOGCALL, LOGLINE, RealTime::now(), RETURN, RealTime::sleep(), throw_timeout(), and RealTime::to_timeval().

Referenced by get_message(), get_message_chunk(), get_message_chunked(), receive_file(), and sniff_next_message_type().

◆ ready_to_read()

bool RemoteConnection::ready_to_read ( ) const

See if there is data available to read.

Returns
true if there is data waiting to be read.

Definition at line 283 of file remoteconnection.cc.

References buffer, fdin, LOGCALL, RETURN, and throw_database_closed().

Referenced by RemoteDatabase::get_remote_stats().

◆ receive_file()

int RemoteConnection::receive_file ( const std::string &  file,
double  end_time 
)

Save the contents of a message as a file.

Parameters
filePath to file to save the message data into. If the file exists it will be overwritten.
end_timeIf this time is reached, then a timeout exception will be thrown. If (end_time == 0.0) then the operation will never timeout.
Returns
Message type code or -1 for EOF.

Definition at line 762 of file remoteconnection.cc.

References buffer, chunked_data_left, CHUNKSIZE, fdin, get_message_chunked(), LOGCALL, O_CLOEXEC, posixy_open, read_at_least(), RETURN, throw_database_closed(), and write_all().

Referenced by Xapian::DatabaseReplica::Internal::apply_db_copy().

◆ send_file()

void RemoteConnection::send_file ( char  type,
int  fd,
double  end_time 
)

Send the contents of a file as a message.

Parameters
typeMessage type code.
fdFile containing the message data.
end_timeIf this time is reached, then a timeout exception will be thrown. If (end_time == 0.0) then the operation will never timeout.

Definition at line 462 of file remoteconnection.cc.

References AssertRel, CHUNKSIZE, context, encode_length(), fdout, file_size(), LOGCALL_VOID, LOGLINE, RealTime::now(), POLLSELECT, send_or_write(), RealTime::sleep(), throw_database_closed(), throw_timeout(), and RealTime::to_timeval().

Referenced by GlassDatabase::send_whole_database(), ChertDatabase::send_whole_database(), GlassDatabase::write_changesets_to_fd(), and ChertDatabase::write_changesets_to_fd().

◆ send_message()

void RemoteConnection::send_message ( char  type,
const std::string &  s,
double  end_time 
)

◆ send_or_write()

ssize_t RemoteConnection::send_or_write ( const void *  p,
size_t  n 
)
private

Helper which calls send() or write().

Definition at line 317 of file remoteconnection.cc.

References fdout, and usual.

Referenced by send_file(), and send_message().

◆ shutdown()

void RemoteConnection::shutdown ( )

Shutdown the connection.

Sends a shutdown message to the server and waits for it to close its end of the connection.

Definition at line 786 of file remoteconnection.cc.

References dummy, fdin, LOGCALL_VOID, MSG_SHUTDOWN, and send_message().

Referenced by RemoteDatabase::do_close().

◆ sniff_next_message_type()

int RemoteConnection::sniff_next_message_type ( double  end_time)

Check what the next message type is.

This must not be called after a call to get_message_chunked() until get_message_chunk() has returned 0 to indicate the whole message has been received.

Other than that restriction, this may be called at any time to determine what the next message waiting to be processed is: it will not affect subsequent calls which read messages.

Parameters
end_timeIf this time is reached, then a timeout exception will be thrown. If (end_time == 0.0) then the operation will never timeout.
Returns
Message type code or -1 for EOF.

Definition at line 613 of file remoteconnection.cc.

References buffer, fdin, LOGCALL, read_at_least(), RETURN, and throw_database_closed().

Referenced by Xapian::DatabaseReplica::Internal::apply_db_copy(), and Xapian::DatabaseReplica::Internal::apply_next_changeset().

Member Data Documentation

◆ buffer

std::string RemoteConnection::buffer
private

Buffer to hold unprocessed input.

Definition at line 219 of file remoteconnection.h.

Referenced by get_message(), get_message_chunk(), get_message_chunked(), read_at_least(), ready_to_read(), receive_file(), and sniff_next_message_type().

◆ chunked_data_left

off_t RemoteConnection::chunked_data_left
private

Remaining bytes of message data still to come over fdin for a chunked read.

Definition at line 222 of file remoteconnection.h.

Referenced by get_message_chunk(), get_message_chunked(), and receive_file().

◆ context

std::string RemoteConnection::context
protected

The context to report with errors.

Subclasses are allowed to manage this.

Definition at line 262 of file remoteconnection.h.

Referenced by do_close(), RemoteServer::msg_writeaccess(), read_at_least(), RemoteConnection(), RemoteServer::RemoteServer(), send_file(), and send_message().

◆ fdin

int RemoteConnection::fdin
private

The file descriptor used for reading.

If this is -1, the connection is unidirectional and write-only. If both fdin and fdout are -1, then the connection has been closed.

Definition at line 181 of file remoteconnection.h.

Referenced by do_close(), get_message(), get_message_chunk(), get_message_chunked(), read_at_least(), ready_to_read(), receive_file(), shutdown(), and sniff_next_message_type().

◆ fdout

int RemoteConnection::fdout
private

The file descriptor used for writing.

If this is -1, the connection is unidirectional and read-only. If both fdin and fdout are -1, then the connection has been closed. It is valid for fdout to be the same as fdin.

Definition at line 189 of file remoteconnection.h.

Referenced by do_close(), RemoteConnection(), send_file(), send_message(), and send_or_write().


The documentation for this class was generated from the following files: