Fixed some dumb bugs. Think I may have been asleep for last push.

This commit is contained in:
Fred Nicolson 2016-12-09 22:04:37 +00:00
parent d36a98b9b2
commit 9cbe2aa8e8
3 changed files with 41 additions and 20 deletions

View File

@ -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);

View File

@ -3,7 +3,9 @@
#include <TcpSocket.h>
#include <TcpListener.h>
#include <thread>
#include <atomic>
std::atomic<uint64_t> 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<double>(std::chrono::system_clock::now() - start).count() << "MBps" << std::endl;
}
t1.join();
return 0;

View File

@ -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;
}