Skip to content

Commit e66a6bc

Browse files
tcp: Deprecated on_error. on_connect may now be invoked with nullptr on failed (outgoing) connection
1 parent f0761a0 commit e66a6bc

5 files changed

Lines changed: 30 additions & 50 deletions

File tree

api/net/tcp/connection.hpp

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,13 @@ class Connection : public std::enable_shared_from_this<Connection> {
6565
using WriteBuffer = Write_queue::WriteBuffer;
6666

6767
public:
68-
/** Called with the connection itself when it's been established. */
68+
/** Called with the connection itself when it's been established. May be a nullptr if the connection failed. */
6969
using ConnectCallback = delegate<void(Connection_ptr self)>;
7070
/**
7171
* @brief Event when a connection has been established.
7272
* This event lets you know when to start using the connection,
7373
* and should always be assigned.
74+
* NOTE: The Connection_ptr will be a nullptr when an outgoing connection failed.
7475
*
7576
* @param[in] callback The callback
7677
*
@@ -137,18 +138,6 @@ class Connection : public std::enable_shared_from_this<Connection> {
137138
*/
138139
inline Connection& on_write(WriteCallback callback);
139140

140-
/** Called with the error encountered. */
141-
using ErrorCallback = delegate<void(const TCPException& err)>;
142-
/**
143-
* @brief Event when a connection has experienced an error of any kind.
144-
* Pretty useless in it's current form, and only useful for printing.
145-
*
146-
* @param[in] callback The callback
147-
*
148-
* @return This connection
149-
*/
150-
inline Connection& on_error(ErrorCallback callback);
151-
152141
/** Called with the packet that got dropped and the reason why. */
153142
using PacketDroppedCallback = delegate<void(const Packet&, Drop_reason)>;
154143
/**
@@ -231,7 +220,10 @@ class Connection : public std::enable_shared_from_this<Connection> {
231220
*/
232221
Stream(Connection_ptr conn)
233222
: tcp{std::move(conn)}
234-
{}
223+
{
224+
// stream for a nullptr makes no sense
225+
Expects(tcp != nullptr);
226+
}
235227

236228
/**
237229
* @brief Event when the stream is connected/established/ready to use.
@@ -788,6 +780,7 @@ class Connection : public std::enable_shared_from_this<Connection> {
788780
/**
789781
* @brief Open the connection.
790782
* Active determines whether the connection is active or passive.
783+
* May throw if no remote host, or state isnt valid for opening.
791784
*
792785
* @param[in] active Whether its an active (outgoing) or passive (listening)
793786
*/
@@ -843,7 +836,6 @@ class Connection : public std::enable_shared_from_this<Connection> {
843836
/** Callbacks */
844837
ConnectCallback on_connect_;
845838
DisconnectCallback on_disconnect_;
846-
ErrorCallback on_error_;
847839
PacketDroppedCallback on_packet_dropped_;
848840
RtxTimeoutCallback on_rtx_timeout_;
849841
CloseCallback on_close_;
@@ -998,15 +990,15 @@ class Connection : public std::enable_shared_from_this<Connection> {
998990
/*
999991
Invoke/signal the diffrent TCP events.
1000992
*/
1001-
void signal_connect()
1002-
{ if(on_connect_) on_connect_(shared_from_this()); }
993+
void signal_connect(const bool success = true)
994+
{
995+
if(on_connect_)
996+
(success) ? on_connect_(shared_from_this()) : on_connect_(nullptr);
997+
}
1003998

1004999
void signal_disconnect(Disconnect::Reason&& reason)
10051000
{ on_disconnect_(shared_from_this(), Disconnect{reason}); }
10061001

1007-
void signal_error(TCPException error)
1008-
{ if(on_error_) on_error_(std::forward<TCPException>(error)); }
1009-
10101002
void signal_packet_dropped(const Packet& packet, Drop_reason reason)
10111003
{ if(on_packet_dropped_) on_packet_dropped_(packet, reason); }
10121004

api/net/tcp/connection.inc

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@ inline Connection& Connection::on_write(WriteCallback cb) {
2424
return *this;
2525
}
2626

27-
inline Connection& Connection::on_error(ErrorCallback callback) {
28-
on_error_ = callback;
29-
return *this;
30-
}
31-
3227
inline Connection& Connection::on_packet_dropped(PacketDroppedCallback callback) {
3328
on_packet_dropped_ = callback;
3429
return *this;

src/net/tcp/connection.cpp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ void Connection::reset_callbacks()
8282
on_disconnect_ = {this, &Connection::default_on_disconnect};
8383
on_connect_.reset();
8484
writeq.on_write(nullptr);
85-
on_error_.reset();
8685
on_packet_dropped_.reset();
8786
on_rtx_timeout_.reset();
8887
on_close_.reset();
@@ -246,16 +245,10 @@ void Connection::writeq_reset() {
246245
rtx_timer.stop();
247246
}
248247

249-
void Connection::open(bool active) {
250-
try {
251-
debug("<TCP::Connection::open> Trying to open Connection...\n");
252-
state_->open(*this, active);
253-
}
254-
// No remote host, or state isnt valid for opening.
255-
catch (const TCPException& e) {
256-
debug("<TCP::Connection::open> Cannot open Connection. \n");
257-
signal_error(e);
258-
}
248+
void Connection::open(bool active)
249+
{
250+
debug("<TCP::Connection::open> Trying to open Connection...\n");
251+
state_->open(*this, active);
259252
}
260253

261254
void Connection::close() {
@@ -269,8 +262,9 @@ void Connection::close() {
269262
if(is_state(Closed::instance()))
270263
signal_close();
271264
}
272-
catch(const TCPException& err) {
273-
signal_error(err);
265+
catch(const TCPException&) {
266+
// just ignore for now, it's kinda stupid its even throwing (i think)
267+
// early return is_closing will probably prevent this from happening
274268
}
275269
}
276270

@@ -883,7 +877,6 @@ void Connection::clean_up() {
883877

884878
on_connect_.reset();
885879
on_disconnect_.reset();
886-
on_error_.reset();
887880
on_packet_dropped_.reset();
888881
on_rtx_timeout_.reset();
889882
on_close_.reset();

src/net/tcp/connection_states.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -890,7 +890,7 @@ State::Result Connection::SynSent::handle(Connection& tcp, Packet_ptr in) {
890890
// 2. check RST
891891
if(UNLIKELY(in->isset(RST))) {
892892
if(in->isset(ACK)) {
893-
tcp.signal_error(TCPException{"Connection reset."});
893+
tcp.signal_connect(false);
894894
tcp.drop(*in, Drop_reason::RST);
895895
return CLOSED;
896896
} else {

test/net/integration/tcp/service.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,16 @@ void OUTGOING_TEST_INTERNET(const HostAddress& address) {
8080
[port](auto ip_address) {
8181
CHECK(ip_address != 0, "Resolved host");
8282

83-
if(ip_address != 0) {
83+
if(ip_address != 0)
84+
{
8485
Inet4::stack<0>().tcp().connect({ip_address, port})
85-
->on_connect([](tcp::Connection_ptr conn) {
86-
CHECK(true, "Connected");
87-
conn->on_read(1024, [](tcp::buffer_t, size_t n) {
88-
CHECK(n > 0, "Received a response");
89-
});
90-
})
91-
.on_error([](tcp::TCPException err) {
92-
CHECK(false, "Error occured: %s", err.what());
93-
});
86+
->on_connect([](tcp::Connection_ptr conn)
87+
{
88+
CHECKSERT(conn != nullptr, "Connected");
89+
conn->on_read(1024, [](tcp::buffer_t, size_t n) {
90+
CHECK(n > 0, "Received a response");
91+
});
92+
});
9493
}
9594
});
9695
}
@@ -102,6 +101,7 @@ void OUTGOING_TEST(tcp::Socket outgoing) {
102101
INFO("TEST", "Outgoing Connection (%s)", outgoing.to_string().c_str());
103102
Inet4::stack<0>().tcp().connect(outgoing, [](tcp::Connection_ptr conn)
104103
{
104+
CHECKSERT(conn != nullptr, "Connection successfully established.");
105105
conn->on_read(small.size(), [](tcp::buffer_t buffer, size_t n)
106106
{
107107
CHECKSERT(std::string((char*)buffer.get(), n) == small, "Received SMALL");

0 commit comments

Comments
 (0)