DrivenEngine channels are now shared_ptr

This commit is contained in:
2022-01-05 12:50:16 -05:00
parent 6b23651606
commit c733023823
7 changed files with 103 additions and 87 deletions

View File

@@ -148,16 +148,18 @@ public:
//
void set_prompt(const std::string &prompt);
// You may delete any channel except for stdio. This closes
// the channel.
// Do not construct your own Channels. Instead,
// use methods of class DrivenEngine like new_outgoing_channel.
// Channels are referenced by shared_ptr. You can
// release your shared_ptr at any time.
//
~Channel();
Channel(DrivenEngine *de, int chid, int port, const std::string &target, bool stop);
~Channel() {};
private:
// Constructor is deliberately private. Use
// DrivenEngine::new_outgoing_channel to create outgoing socket channels.
//
Channel(DrivenEngine *de, int chid, int port, const std::string &target);
void feed_readline(int nbytes, const char *bytes);
void peek_outgoing(int *nbytes, const char **bytes);
@@ -166,7 +168,6 @@ private:
private:
static const int READLINE_MAX=512;
DrivenEngine *driven_;
int chid_;
std::unique_ptr<StreamBuffer> sb_in_;
std::unique_ptr<StreamBuffer> sb_out_;
@@ -174,6 +175,7 @@ private:
bool closed_;
std::string error_;
std::string target_;
bool stop_driver_;
// Readline stuff.
std::string desired_command_;
@@ -187,7 +189,7 @@ private:
friend class DrivenEngine;
};
using UniqueChannel = std::unique_ptr<Channel>;
using SharedChannel = std::shared_ptr<Channel>;
class DrivenEngine {
public:
@@ -229,7 +231,7 @@ public:
// actually opening the connection and relaying data into the channel using
// drv_get_target, drv_peek_outgoing, drv_sent_outgoing, drv_recv_incoming.
//
UniqueChannel new_outgoing_channel(const std::string &target);
SharedChannel new_outgoing_channel(const std::string &target);
// Create a new channel from any pending incoming connection. If there is no
// incoming connection, returns nullptr.
@@ -242,17 +244,16 @@ public:
// using drv_get_target, drv_peek_outgoing, drv_sent_outgoing,
// drv_recv_incoming.
//
UniqueChannel new_incoming_channel();
SharedChannel new_incoming_channel();
// Obtain the stdio channel. There is only one stdio channel. It is owned
// by the DrivenEngine. It is an error to delete the stdio channel.
// Obtain the stdio channel. There is only one stdio channel.
//
// DRIVER: the stdio channel is created automatically when the DrivenEngine
// is created. The driver is responsible for relaying data into the channel
// using drv_get_target, drv_peek_outgoing, drv_sent_outgoing,
// drv_recv_incoming.
//
Channel *get_stdio_channel();
SharedChannel get_stdio_channel();
// Obtain the output buffer of the stdio channel as an ostream.
//
@@ -292,13 +293,6 @@ public:
//
void drv_get_listen_ports(std::set<int> &ports);
// Get a list of all recently-closed channels. The driver should
// discard all socket information associated with these channels.
// Caution: this may contain channels that the driver has never
// heard of. In that case, just ignore the close-request.
//
void drv_get_new_closed(std::set<int> &opened);
// Get a list of all recently-opened channels that were created using
// drv_new_outgoing_channel. The driver should initiate outgoing
// connections for these channels.
@@ -316,6 +310,12 @@ public:
//
bool drv_outgoing_empty(int chid);
// Return true if the user has released all references to this channel.
// In this case, the driver should initiate shutdown of the channel,
// and the driver should eventually call drv_notify_close.
//
bool drv_get_channel_released(int chid);
// Get a pointer to the bytes in the outgoing buffer. The pointer returned
// here is naturally only valid until the buffer is changed. This function
// is used for all channels, including sockets and stdio.
@@ -415,11 +415,10 @@ private:
Channel *get_chid(int chid);
private:
Channel *channels_[MAX_CHAN];
SharedChannel channels_[MAX_CHAN];
int next_unused_chid_;
UniqueChannel stdio_channel_;
std::vector<UniqueChannel> accepted_channels_;
std::set<int> new_closed_;
SharedChannel stdio_channel_;
std::vector<SharedChannel> accepted_channels_;
std::set<int> new_outgoing_;
util::LuaSourcePtr lua_source_;
std::set<int> listen_ports_;