Skip to content

Commit

Permalink
增加packet接口
Browse files Browse the repository at this point in the history
  • Loading branch information
wlgq2 committed Oct 29, 2019
1 parent d088f82 commit d6d8882
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 29 deletions.
5 changes: 1 addition & 4 deletions examples/all_example/EchoServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,8 @@ void EchoServer::newMessage(shared_ptr<TcpConnection> connection,const char* buf
{
packetbuf->append(buf, static_cast<int>(size));
//循环读取buffer
std::string data("");
while (0 == packetbuf->readPacket(data))
while (0 == packetbuf->readPacket(packet))
{
packet.swap(data);
data.clear();
std::cout << "reserve data "<< packet.DataSize()<<":" << packet.getData() << std::endl;
connection->write(packet.Buffer().c_str(), packet.PacketSize(), nullptr);
}
Expand Down
5 changes: 2 additions & 3 deletions examples/pingpang/Clinet.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,10 @@ class Client : public uv::TcpClient
if (nullptr != packetbuf)
{
packetbuf->append(buf, static_cast<int>(size));
std::string packet;
uv::Packet packet;
while (0 == packetbuf->readPacket(packet))
{
write(packet.c_str(), (unsigned)packet.size(), nullptr);
packet.clear();
write(packet.Buffer().c_str(), (unsigned)packet.PacketSize(), nullptr);
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions examples/pingpang/EchoServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ void EchoServer::newMessage(shared_ptr<TcpConnection> connection, const char* bu
{
packetbuf->append(buf, static_cast<int>(size));
//循环读取buffer
std::string out;
while (0 == packetbuf->readPacket(out))
Packet packet;
while (0 == packetbuf->readPacket(packet))
{
connection->write(out.c_str(), out.size(), nullptr);
out.clear();
connection->write(packet.Buffer().c_str(), packet.PacketSize(), nullptr);
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion uv/GlobalConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ GlobalConfig::BufferMode GlobalConfig::BufferModeStatus = GlobalConfig::BufferMo
uint64_t GlobalConfig::CycleBufferSize = 1024 << 5;

//默认包解析函数
ReadBufferFunc GlobalConfig::ReadBufCallback = std::bind(&Packet::readFromBuffer, placeholders::_1, placeholders::_2);
ReadBufferStringFunc GlobalConfig::ReadBufferString = nullptr;
ReadBufferPacketFunc GlobalConfig::ReadBufferPacket = std::bind(&Packet::readFromBuffer, placeholders::_1, placeholders::_2);;
ReadBufferVoidFunc GlobalConfig::ReadBufferVoid = nullptr;
11 changes: 9 additions & 2 deletions uv/GlobalConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
namespace uv
{
class PacketBuffer;
using ReadBufferFunc = std::function<int(PacketBuffer*, std::string&)>;
class Packet;
using ReadBufferStringFunc = std::function<int(PacketBuffer*, std::string&)>;
using ReadBufferPacketFunc = std::function<int(PacketBuffer*, Packet&)>;
using ReadBufferVoidFunc = std::function<int(PacketBuffer*, void*)>;

class GlobalConfig
{
Expand All @@ -29,7 +32,11 @@ class GlobalConfig
};
static BufferMode BufferModeStatus;
static uint64_t CycleBufferSize;
static ReadBufferFunc ReadBufCallback;


static ReadBufferStringFunc ReadBufferString;
static ReadBufferPacketFunc ReadBufferPacket;
static ReadBufferVoidFunc ReadBufferVoid;
};
}
#endif
18 changes: 9 additions & 9 deletions uv/Packet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ uv::Packet::~Packet()

}

int uv::Packet::readFromBuffer(PacketBuffer* packetbuf, std::string& out)
int uv::Packet::readFromBuffer(PacketBuffer* packetbuf, Packet& out)
{
std::string data("");
while (true)
{
out.clear();
auto size = packetbuf->readSize();
//数据小于包头大小
if (size < PacketMinSize())
Expand All @@ -42,30 +42,30 @@ int uv::Packet::readFromBuffer(PacketBuffer* packetbuf, std::string& out)
}
//找包头
uint16_t dataSize;
packetbuf->readBufferN(out, sizeof(dataSize)+1);
if ((uint8_t)out[0] != HeadByte) //包头不正确,从下一个字节开始继续找
packetbuf->readBufferN(data, sizeof(dataSize)+1);
if ((uint8_t)data[0] != HeadByte) //包头不正确,从下一个字节开始继续找
{
out.clear();
data.clear();
packetbuf->clearBufferN(1);
continue;
}
UnpackNum((uint8_t*)out.c_str() + 1, dataSize);
UnpackNum((uint8_t*)data.c_str() + 1, dataSize);
uint16_t msgsize = dataSize + PacketMinSize();
//包不完整
if (size < msgsize)
{
out.clear();
return -1;
}
packetbuf->clearBufferN(sizeof(dataSize)+1);
packetbuf->readBufferN(out, dataSize +1);
packetbuf->readBufferN(data, dataSize +1);
//检查包尾
if ((uint8_t)out.back() == EndByte)
if ((uint8_t)data.back() == EndByte)
{
packetbuf->clearBufferN(dataSize +1);
break;
}
}
out.swap(data);
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion uv/Packet.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Packet
Packet();
~Packet();

static int readFromBuffer(PacketBuffer*, std::string&);
static int readFromBuffer(PacketBuffer*, Packet&);

void pack(const char* data, uint16_t size);

Expand Down
24 changes: 20 additions & 4 deletions uv/PacketBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Description: https://github.com/wlgq2/uv-cpp

namespace uv
{

class Packet;
class PacketBuffer
{

Expand All @@ -33,10 +33,26 @@ class PacketBuffer
virtual int clear() = 0;
virtual uint64_t readSize() = 0;

int readPacket(std::string& data)
int readString(std::string& out)
{
if (nullptr != GlobalConfig::ReadBufferString)
return GlobalConfig::ReadBufferString(this, out);
uv::LogWriter::Instance()->error("not defined packet parse func.");
return -1;
}

int readPacket(Packet& out)
{
if (nullptr != GlobalConfig::ReadBufferPacket)
return GlobalConfig::ReadBufferPacket(this, out);
uv::LogWriter::Instance()->error("not defined packet parse func.");
return -1;
}

int readGeneric(void* out)
{
if (nullptr != GlobalConfig::ReadBufCallback)
return GlobalConfig::ReadBufCallback(this, data);
if (nullptr != GlobalConfig::ReadBufferVoid)
return GlobalConfig::ReadBufferVoid(this, out);
uv::LogWriter::Instance()->error("not defined packet parse func.");
return -1;
}
Expand Down
2 changes: 1 addition & 1 deletion uv/uv11.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#ifndef UV_UV11_H
#define UV_UV11_H

#define UV_CPP_VERSION "1.3.0"
#define UV_CPP_VERSION "1.3.1"

#include "Async.h"
#include "Signal.h"
Expand Down

0 comments on commit d6d8882

Please sign in to comment.