xapian-core
1.4.26
|
A RemoteConnection object provides a bidirectional connection to another RemoteConnection object on a remote machine. More...
#include <remoteconnection.h>
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... | |
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.
|
private |
Don't allow copying.
RemoteConnection::RemoteConnection | ( | int | fdin_, |
int | fdout_, | ||
const std::string & | context_ = std::string() |
||
) |
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().
int RemoteConnection::get_message | ( | std::string & | result, |
double | end_time | ||
) |
Read one message from fdin.
[out] | result | Message data. |
end_time | If 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 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().
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.
[in,out] | result | Message data. This is appended to, so if you read more than needed the previous time, leave the excess in result. |
at_least | Return 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_time | If 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 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().
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.
end_time | If 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 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().
|
private |
Don't allow assignment.
|
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.
min_len | Minimum number of bytes required in buffer. |
end_time | If this time is reached, then a timeout exception will be thrown. If (end_time == 0.0), then keep trying indefinitely. |
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().
bool RemoteConnection::ready_to_read | ( | ) | const |
See if there is data available to read.
Definition at line 283 of file remoteconnection.cc.
References buffer, fdin, LOGCALL, RETURN, and throw_database_closed().
Referenced by RemoteDatabase::get_remote_stats().
int RemoteConnection::receive_file | ( | const std::string & | file, |
double | end_time | ||
) |
Save the contents of a message as a file.
file | Path to file to save the message data into. If the file exists it will be overwritten. |
end_time | If 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 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().
void RemoteConnection::send_file | ( | char | type, |
int | fd, | ||
double | end_time | ||
) |
Send the contents of a file as a message.
type | Message type code. |
fd | File containing the message data. |
end_time | If 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().
void RemoteConnection::send_message | ( | char | type, |
const std::string & | s, | ||
double | end_time | ||
) |
Send a message.
type | Message type code. |
s | Message data. |
end_time | If 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 334 of file remoteconnection.cc.
References context, encode_length(), fdout, LOGCALL_VOID, LOGLINE, RealTime::now(), POLLSELECT, send_or_write(), RealTime::sleep(), Xapian::Internal::str(), throw_database_closed(), throw_timeout(), and RealTime::to_timeval().
Referenced by RemoteServer::send_message(), RemoteDatabase::send_message(), GlassDatabase::send_whole_database(), ChertDatabase::send_whole_database(), shutdown(), Xapian::DatabaseMaster::write_changesets_to_fd(), GlassDatabase::write_changesets_to_fd(), and ChertDatabase::write_changesets_to_fd().
|
private |
Helper which calls send() or write().
Definition at line 317 of file remoteconnection.cc.
Referenced by send_file(), and send_message().
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().
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.
end_time | If 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 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().
|
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().
|
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().
|
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().
|
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().
|
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().