|
| 1 | +#include <examples/ace/ttcp/common.h> |
| 2 | + |
| 3 | +#include <muduo/base/Logging.h> |
| 4 | +#include <muduo/net/EventLoop.h> |
| 5 | +#include <muduo/net/TcpClient.h> |
| 6 | +#include <muduo/net/TcpServer.h> |
| 7 | + |
| 8 | +#include <boost/bind.hpp> |
| 9 | + |
| 10 | +#include <stdio.h> |
| 11 | + |
| 12 | +using namespace muduo; |
| 13 | +using namespace muduo::net; |
| 14 | + |
| 15 | +EventLoop* g_loop; |
| 16 | + |
| 17 | +struct Context |
| 18 | +{ |
| 19 | + int count; |
| 20 | + int64_t bytes; |
| 21 | + SessionMessage session; |
| 22 | + Buffer output; |
| 23 | + |
| 24 | + Context() |
| 25 | + : count(0), |
| 26 | + bytes(0) |
| 27 | + { |
| 28 | + session.number = 0; |
| 29 | + session.length = 0; |
| 30 | + } |
| 31 | +}; |
| 32 | + |
| 33 | +///////////////////////////////////////////////////////////////////// |
| 34 | +// T R A N S M I T |
| 35 | +///////////////////////////////////////////////////////////////////// |
| 36 | + |
| 37 | +namespace trans |
| 38 | +{ |
| 39 | + |
| 40 | +void onConnection(const Options& opt, const TcpConnectionPtr& conn) |
| 41 | +{ |
| 42 | + if (conn->connected()) |
| 43 | + { |
| 44 | + printf("connected\n"); |
| 45 | + Context context; |
| 46 | + context.count = 1; |
| 47 | + context.bytes = opt.length; |
| 48 | + context.session.number = opt.number; |
| 49 | + context.session.length = opt.length; |
| 50 | + context.output.appendInt32(opt.length); |
| 51 | + context.output.ensureWritableBytes(opt.length); |
| 52 | + for (int i = 0; i < opt.length; ++i) |
| 53 | + { |
| 54 | + context.output.beginWrite()[i] = "0123456789ABCDEF"[i % 16]; |
| 55 | + } |
| 56 | + context.output.hasWritten(opt.length); |
| 57 | + conn->setContext(context); |
| 58 | + |
| 59 | + SessionMessage sessionMessage = { 0, 0 }; |
| 60 | + sessionMessage.number = htonl(opt.number); |
| 61 | + sessionMessage.length = htonl(opt.length); |
| 62 | + conn->send(&sessionMessage, sizeof(sessionMessage)); |
| 63 | + |
| 64 | + conn->send(context.output.toStringPiece()); |
| 65 | + } |
| 66 | + else |
| 67 | + { |
| 68 | + const Context& context = boost::any_cast<Context>(conn->getContext()); |
| 69 | + LOG_INFO << "payload bytes " << context.bytes; |
| 70 | + conn->getLoop()->quit(); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +void onMessage(const TcpConnectionPtr& conn, Buffer* buf, Timestamp time) |
| 75 | +{ |
| 76 | + Context* context = boost::any_cast<Context>(conn->getMutableContext()); |
| 77 | + while (buf->readableBytes() >= sizeof(int32_t)) |
| 78 | + { |
| 79 | + int32_t length = buf->readInt32(); |
| 80 | + if (length == context->session.length) |
| 81 | + { |
| 82 | + if (context->count < context->session.number) |
| 83 | + { |
| 84 | + conn->send(context->output.toStringPiece()); |
| 85 | + ++context->count; |
| 86 | + context->bytes += length; |
| 87 | + } |
| 88 | + else |
| 89 | + { |
| 90 | + conn->shutdown(); |
| 91 | + break; |
| 92 | + } |
| 93 | + } |
| 94 | + else |
| 95 | + { |
| 96 | + conn->shutdown(); |
| 97 | + break; |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +} |
| 103 | + |
| 104 | +void transmit(const Options& opt) |
| 105 | +{ |
| 106 | + InetAddress addr(resolveOrDie(opt.host.c_str(), opt.port)); |
| 107 | + muduo::Timestamp start(muduo::Timestamp::now()); |
| 108 | + EventLoop loop; |
| 109 | + g_loop = &loop; |
| 110 | + TcpClient client(&loop, addr, "TtcpClient"); |
| 111 | + client.setConnectionCallback( |
| 112 | + boost::bind(&trans::onConnection, opt, _1)); |
| 113 | + client.setMessageCallback( |
| 114 | + boost::bind(&trans::onMessage, _1, _2, _3)); |
| 115 | + client.connect(); |
| 116 | + loop.loop(); |
| 117 | + double elapsed = timeDifference(muduo::Timestamp::now(), start); |
| 118 | + double total_mb = 1.0 * opt.length * opt.number / 1024 / 1024; |
| 119 | + printf("%.3f MiB\n%.3f MiB/s\n", total_mb, total_mb / elapsed); |
| 120 | +} |
| 121 | + |
| 122 | +///////////////////////////////////////////////////////////////////// |
| 123 | +// R E C E I V E |
| 124 | +///////////////////////////////////////////////////////////////////// |
| 125 | + |
| 126 | +namespace receiving |
| 127 | +{ |
| 128 | + |
| 129 | +void onConnection(const TcpConnectionPtr& conn) |
| 130 | +{ |
| 131 | + if (conn->connected()) |
| 132 | + { |
| 133 | + Context context; |
| 134 | + conn->setContext(context); |
| 135 | + } |
| 136 | + else |
| 137 | + { |
| 138 | + const Context& context = boost::any_cast<Context>(conn->getContext()); |
| 139 | + LOG_INFO << "payload bytes " << context.bytes; |
| 140 | + conn->getLoop()->quit(); |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +void onMessage(const TcpConnectionPtr& conn, Buffer* buf, Timestamp time) |
| 145 | +{ |
| 146 | + while (buf->readableBytes() >= sizeof(int32_t)) |
| 147 | + { |
| 148 | + Context* context = boost::any_cast<Context>(conn->getMutableContext()); |
| 149 | + SessionMessage& session = context->session; |
| 150 | + if (session.number == 0 && session.length == 0) |
| 151 | + { |
| 152 | + if (buf->readableBytes() >= sizeof(SessionMessage)) |
| 153 | + { |
| 154 | + session.number = buf->readInt32(); |
| 155 | + session.length = buf->readInt32(); |
| 156 | + context->output.appendInt32(session.length); |
| 157 | + printf("receive number = %d\nreceive length = %d\n", |
| 158 | + session.number, session.length); |
| 159 | + } |
| 160 | + else |
| 161 | + { |
| 162 | + break; |
| 163 | + } |
| 164 | + } |
| 165 | + else |
| 166 | + { |
| 167 | + const unsigned total_len = session.length + static_cast<int>(sizeof(int32_t)); |
| 168 | + const int32_t length = buf->peekInt32(); |
| 169 | + if (length == session.length) |
| 170 | + { |
| 171 | + if (buf->readableBytes() >= total_len) |
| 172 | + { |
| 173 | + buf->retrieve(total_len); |
| 174 | + conn->send(context->output.toStringPiece()); |
| 175 | + ++context->count; |
| 176 | + context->bytes += length; |
| 177 | + if (context->count >= session.number) |
| 178 | + { |
| 179 | + conn->shutdown(); |
| 180 | + break; |
| 181 | + } |
| 182 | + } |
| 183 | + else |
| 184 | + { |
| 185 | + break; |
| 186 | + } |
| 187 | + } |
| 188 | + else |
| 189 | + { |
| 190 | + printf("wrong length %d\n", length); |
| 191 | + conn->shutdown(); |
| 192 | + break; |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +} |
| 199 | + |
| 200 | +void receive(const Options& opt) |
| 201 | +{ |
| 202 | + EventLoop loop; |
| 203 | + g_loop = &loop; |
| 204 | + InetAddress listenAddr(opt.port); |
| 205 | + TcpServer server(&loop, listenAddr, "TtcpReceive"); |
| 206 | + server.setConnectionCallback( |
| 207 | + boost::bind(&receiving::onConnection, _1)); |
| 208 | + server.setMessageCallback( |
| 209 | + boost::bind(&receiving::onMessage, _1, _2, _3)); |
| 210 | + server.start(); |
| 211 | + loop.loop(); |
| 212 | +} |
0 commit comments