From 9cbe2aa8e82a2673f8099edcd99a1df9c1df22da Mon Sep 17 00:00:00 2001 From: Fred Nicolson Date: Fri, 9 Dec 2016 22:04:37 +0000 Subject: [PATCH] Fixed some dumb bugs. Think I may have been asleep for last push. --- include/Packet.h | 2 +- main.cpp | 27 +++++++++++++++++++++------ src/TcpSocket.cpp | 32 +++++++++++++++++++------------- 3 files changed, 41 insertions(+), 20 deletions(-) diff --git a/include/Packet.h b/include/Packet.h index e8d0be2..7ec2206 100644 --- a/include/Packet.h +++ b/include/Packet.h @@ -151,7 +151,7 @@ namespace fr */ inline Packet&operator>>(std::string &var) { - uint32_t length; + uint32_t length = (uint32_t)var.length(); *this >> length; var = buffer.substr(0, length); diff --git a/main.cpp b/main.cpp index bb94fbf..0ad89f8 100644 --- a/main.cpp +++ b/main.cpp @@ -3,7 +3,9 @@ #include #include #include +#include +std::atomic data_sent; void server() { fr::TcpListener listener; @@ -19,7 +21,7 @@ void server() std::string message; packet >> message; - std::cout << "Got: " << message << std::endl; + data_sent += 0x100000; } } @@ -28,19 +30,32 @@ void client() fr::TcpSocket socket; socket.connect("127.0.0.1", "8081"); - fr::Packet packet; - packet << "Hello, World!"; - socket.send(packet); + std::string buffer(0x100000, 'c'); - std::this_thread::sleep_for(std::chrono::milliseconds(100)); + fr::Packet packet; + packet << buffer; + while(true) + { + socket.send(packet); + } } int main() { + data_sent = 0; std::thread t1(&server); std::this_thread::sleep_for(std::chrono::milliseconds(100)); - client(); + auto start = std::chrono::system_clock::now(); + std::thread t2(&client); + + while(true) + { + std::this_thread::sleep_for(std::chrono::seconds(1)); + + uint64_t mb_sent = data_sent / 0x100000; + std::cout << "Transfer speed: " << mb_sent / (uint64_t)std::chrono::duration(std::chrono::system_clock::now() - start).count() << "MBps" << std::endl; + } t1.join(); return 0; diff --git a/src/TcpSocket.cpp b/src/TcpSocket.cpp index 4a96368..2b30127 100644 --- a/src/TcpSocket.cpp +++ b/src/TcpSocket.cpp @@ -22,12 +22,19 @@ namespace fr Socket::Status TcpSocket::send(const Packet &packet) { - size_t send_index = 0; + //Get packet data size_t sent = 0; + std::string data = packet.get_buffer(); + //Prepend packet length + uint32_t length = htonl((uint32_t)data.size()); + data.insert(0, "1234"); + memcpy(&data[0], &length, sizeof(uint32_t)); + + //Send it while(sent < packet.get_buffer().size()) { - ssize_t status = ::send(socket_descriptor, &packet.get_buffer()[send_index], packet.get_buffer().size(), 0); + ssize_t status = ::send(socket_descriptor, data.c_str() + sent, data.size() - sent, 0); if(status > 0) { sent += status; @@ -61,8 +68,10 @@ namespace fr packet_length = ntohl(packet_length); //Now we've got the length, read the rest of the data in - std::string data(packet_length, '\0'); - read_recv(&data[0], packet_length); + std::string data(packet_length, 'c'); + status = read_recv(&data[0], packet_length); + if(status != Socket::Status::Success) + return status; //Set the packet to what we've read packet.set_buffer(std::move(data)); @@ -81,19 +90,12 @@ namespace fr Socket::Status TcpSocket::read_recv(void *dest, size_t size) { - //See if there's enough data in the unprocessed buffer first - if(size < unprocessed_buffer.size()) - { - memcpy(dest, &unprocessed_buffer[0], size); - unprocessed_buffer.erase(0, size); - return Socket::Status::Success; - } - - //Else, keep calling recv until there's enough data in the buffer + //Keep calling recv until there's enough data in the buffer if needed while(unprocessed_buffer.size() < size) { //Read RECV_CHUNK_SIZE bytes into the recv buffer ssize_t status = ::recv(socket_descriptor, recv_buffer.get(), RECV_CHUNK_SIZE, 0); + if(status > 0) { unprocessed_buffer += {recv_buffer.get(), (size_t)status}; @@ -111,6 +113,10 @@ namespace fr } } } + + //Copy data to where it needs to go + memcpy(dest, &unprocessed_buffer[0], size); + unprocessed_buffer.erase(0, size); return Socket::Status::Success; }