Remove opcode from read, async_read (API Change):

fix #446

* Read signatures no longer include `opcode`

* stream::got_binary and stream::got_text inform the caller if
  the current incoming message is binary or text.

Actions Required:

* Remove the `opcode` reference parameter from calls to synchronous
  and asynchronous read functions, replace the logic with calls to
  stream::got_binary and stream::got_text instead.
This commit is contained in:
Vinnie Falco committed 2017-07-20 08:12:19 -07:00
1 parent 3d6574da81
commit 620ce08d6a
13 files changed
+142 -145

No files matched your search

+4 -7
View File
@@ -151,10 +151,9 @@ boost::asio::ip::tcp::socket sock{ios};
stream<boost::asio::ip::tcp::socket> ws{ios};
//[ws_snippet_15
multi_buffer buffer;
opcode op;
ws.read(op, buffer);
ws.read(buffer);
ws.binary(op == opcode::binary);
ws.text(ws.got_text());
ws.write(buffer.data());
buffer.consume(buffer.size());
//]
@@ -210,9 +209,8 @@ boost::asio::ip::tcp::socket sock{ios};
//]
//[ws_snippet_20
opcode op;
multi_buffer buffer;
ws.async_read(op, buffer,
ws.async_read(buffer,
[](error_code ec)
{
// Do something with the buffer
@@ -226,8 +224,7 @@ boost::asio::ip::tcp::socket sock{ios};
void echo(stream<boost::asio::ip::tcp::socket>& ws,
multi_buffer& buffer, boost::asio::yield_context yield)
{
opcode op;
ws.async_read(op, buffer, yield);
ws.async_read(buffer, yield);
std::future<void> fut =
ws.async_write(buffer.data(), boost::asio::use_future);
}
+2 -3
View File
@@ -130,14 +130,13 @@ public:
// Receive Secure WebSocket message, print and close using Beast
beast::multi_buffer b;
beast::websocket::opcode op;
ws.read(op, b);
ws.read(b);
ws.close(beast::websocket::close_code::normal);
try
{
for(;;)
{
ws.read(op, b);
ws.read(b);
b.consume(b.size());
}
}
@@ -243,7 +243,7 @@ private:
return fail("async_write", ec);
d.db.consume(d.db.size());
d.state = 4;
d.ws.async_read(d.op, d.db,
d.ws.async_read(d.db,
d.strand.wrap(std::move(*this)));
return;
@@ -254,7 +254,7 @@ private:
if(ec)
return fail("async_read", ec);
d.state = 3;
d.ws.binary(d.op == beast::websocket::opcode::binary);
d.ws.binary(d.ws.got_binary());
d.ws.async_write(d.db.data(),
d.strand.wrap(std::move(*this)));
return;
+27 -46
View File
@@ -265,9 +265,9 @@ public:
class NextLayer, class DynamicBuffer>
void
read(stream<NextLayer>& ws,
opcode& op, DynamicBuffer& buffer) const
DynamicBuffer& buffer) const
{
ws.read(op, buffer);
ws.read(buffer);
}
template<
@@ -503,10 +503,10 @@ public:
class NextLayer, class DynamicBuffer>
void
read(stream<NextLayer>& ws,
opcode& op, DynamicBuffer& buffer) const
DynamicBuffer& buffer) const
{
error_code ec;
ws.async_read(op, buffer, yield_[ec]);
ws.async_read(buffer, yield_[ec]);
if(ec)
throw system_error{ec};
}
@@ -741,9 +741,8 @@ public:
cbuf(0x88, 0x82, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x17));
try
{
opcode op;
multi_buffer b;
c.read(ws, op, b);
c.read(ws, b);
fail("success", __FILE__, __LINE__);
}
catch(system_error const& e)
@@ -771,9 +770,8 @@ public:
BEAST_EXPECT(called);
try
{
opcode op;
multi_buffer b;
c.read(ws, op, b);
c.read(ws, b);
fail("success", __FILE__, __LINE__);
}
catch(system_error const& e)
@@ -799,9 +797,8 @@ public:
c.accept(ws, req);
try
{
opcode op;
multi_buffer b;
c.read(ws, op, b);
c.read(ws, b);
fail("success", __FILE__, __LINE__);
}
catch(system_error const& e)
@@ -828,9 +825,8 @@ public:
cbuf(0x88, 0x82, 0xff, 0xff));
try
{
opcode op;
multi_buffer b;
c.read(ws, op, b);
c.read(ws, b);
fail("success", __FILE__, __LINE__);
}
catch(system_error const& e)
@@ -1193,9 +1189,8 @@ public:
ws.write(boost::asio::buffer(v), ec);
if(! BEAST_EXPECTS(! ec, ec.message()))
break;
opcode op;
multi_buffer db;
ws.read(op, db, ec);
ws.read(db, ec);
if(! BEAST_EXPECTS(! ec, ec.message()))
break;
BEAST_EXPECT(to_string(db.data()) ==
@@ -1219,9 +1214,8 @@ public:
ws.async_write(boost::asio::buffer(v), do_yield[ec]);
if(! BEAST_EXPECTS(! ec, ec.message()))
break;
opcode op;
multi_buffer db;
ws.async_read(op, db, do_yield[ec]);
ws.async_read(db, do_yield[ec]);
if(! BEAST_EXPECTS(! ec, ec.message()))
break;
BEAST_EXPECT(to_string(db.data()) ==
@@ -1284,10 +1278,9 @@ public:
});
// Read
opcode op;
multi_buffer db;
++count;
ws.async_read(op, db,
ws.async_read(db,
[&](error_code ec)
{
--count;
@@ -1340,12 +1333,11 @@ public:
ws.binary(true);
ws.write(buffer_cat(sbuf("TEXT"),
cbuf(0x03, 0xea, 0xf0, 0x28, 0x8c, 0xbc)));
opcode op;
multi_buffer db;
std::size_t count = 0;
// Read text message with bad utf8.
// Causes a close to be sent, blocking writes.
ws.async_read(op, db,
ws.async_read(db,
[&](error_code ec)
{
// Read should fail with protocol error
@@ -1353,7 +1345,7 @@ public:
BEAST_EXPECTS(
ec == error::failed, ec.message());
// Reads after failure are aborted
ws.async_read(op, db,
ws.async_read(db,
[&](error_code ec)
{
++count;
@@ -1408,12 +1400,11 @@ public:
// Cause close to be received
ws.binary(true);
ws.write(sbuf("CLOSE"));
opcode op;
multi_buffer db;
std::size_t count = 0;
// Read a close frame.
// Sends a close frame, blocking writes.
ws.async_read(op, db,
ws.async_read(db,
[&](error_code ec)
{
// Read should complete with error::closed
@@ -1474,10 +1465,9 @@ public:
// Cause close to be received
ws.binary(true);
ws.write(sbuf("CLOSE"));
opcode op;
multi_buffer db;
std::size_t count = 0;
ws.async_read(op, db,
ws.async_read(db,
[&](error_code ec)
{
++count;
@@ -1525,9 +1515,8 @@ public:
BEAST_EXPECT(! ec);
});
});
opcode op;
multi_buffer db;
ws.async_read(op, db,
ws.async_read(db,
[&](error_code ec)
{
BEAST_EXPECTS(ec == error::closed, ec.message());
@@ -1558,8 +1547,7 @@ public:
ws.write_frame(false, sbuf("u"));
ws.write_frame(true, sbuf("v"));
multi_buffer b;
opcode op;
ws.read(op, b, ec);
ws.read(b, ec);
if(! BEAST_EXPECTS(! ec, ec.message()))
return;
}
@@ -1617,9 +1605,8 @@ public:
{
try
{
opcode op;
multi_buffer db;
c.read(ws, op, db);
c.read(ws, db);
fail();
throw abort_test{};
}
@@ -1651,10 +1638,9 @@ public:
c.write(ws, sbuf("Hello"));
{
// receive echoed message
opcode op;
multi_buffer db;
c.read(ws, op, db);
BEAST_EXPECT(op == opcode::text);
c.read(ws, db);
BEAST_EXPECT(ws.got_text());
BEAST_EXPECT(to_string(db.data()) == "Hello");
}
@@ -1685,11 +1671,10 @@ public:
c.write(ws, sbuf("Hello"));
{
// receive echoed message
opcode op;
multi_buffer db;
c.read(ws, op, db);
c.read(ws, db);
BEAST_EXPECT(pong == 1);
BEAST_EXPECT(op == opcode::binary);
BEAST_EXPECT(ws.got_binary());
BEAST_EXPECT(to_string(db.data()) == "Hello");
}
ws.ping_callback({});
@@ -1707,9 +1692,8 @@ public:
c.write_frame(ws, true, sbuf("World!"));
{
// receive echoed message
opcode op;
multi_buffer db;
c.read(ws, op, db);
c.read(ws, db);
BEAST_EXPECT(pong == 1);
BEAST_EXPECT(to_string(db.data()) == "Hello, World!");
}
@@ -1724,9 +1708,8 @@ public:
c.write(ws, sbuf("Now is the time for all good men"));
{
// receive echoed message
opcode op;
multi_buffer b;
c.read(ws, op, b);
c.read(ws, b);
BEAST_EXPECT(to_string(b.data()) == "Now is the time for all good men");
}
ws.auto_fragment(false);
@@ -1739,9 +1722,8 @@ public:
c.write(ws, buffer(s.data(), s.size()));
{
// receive echoed message
opcode op;
multi_buffer db;
c.read(ws, op, db);
c.read(ws, db);
BEAST_EXPECT(to_string(db.data()) == s);
}
}
@@ -1753,10 +1735,9 @@ public:
c.write(ws, sbuf("Hello"));
{
// receive echoed message
opcode op;
multi_buffer db;
c.read(ws, op, db);
BEAST_EXPECT(op == opcode::text);
c.read(ws, db);
BEAST_EXPECT(ws.got_text());
BEAST_EXPECT(to_string(db.data()) == "Hello");
}
@@ -312,7 +312,7 @@ private:
d.db.consume(d.db.size());
// read message
d.state = 2;
d.ws.async_read(d.op, d.db,
d.ws.async_read(d.db,
d.strand.wrap(std::move(*this)));
return;
@@ -357,7 +357,7 @@ private:
}
// write message
d.state = 1;
d.ws.binary(d.op == beast::websocket::opcode::binary);
d.ws.binary(d.ws.got_binary());
d.ws.async_write(d.db.data(),
d.strand.wrap(std::move(*this)));
return;
@@ -304,15 +304,14 @@ private:
}
for(;;)
{
beast::websocket::opcode op;
beast::multi_buffer b;
ws.read(op, b, ec);
ws.read(b, ec);
if(ec)
{
auto const s = ec.message();
break;
}
ws.binary(op == beast::websocket::opcode::binary);
ws.binary(ws.got_binary());
if(match(b, "RAW"))
{
boost::asio::write(