diff --git a/examples/all_example/EchoServer.cpp b/examples/all_example/EchoServer.cpp index 7a8a4db..e6b2da3 100644 --- a/examples/all_example/EchoServer.cpp +++ b/examples/all_example/EchoServer.cpp @@ -57,11 +57,8 @@ void EchoServer::newMessage(shared_ptr connection,const char* buf { packetbuf->append(buf, static_cast(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); } diff --git a/examples/pingpang/Clinet.h b/examples/pingpang/Clinet.h index 9207367..624197e 100644 --- a/examples/pingpang/Clinet.h +++ b/examples/pingpang/Clinet.h @@ -73,11 +73,10 @@ class Client : public uv::TcpClient if (nullptr != packetbuf) { packetbuf->append(buf, static_cast(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); } } } diff --git a/examples/pingpang/EchoServer.cpp b/examples/pingpang/EchoServer.cpp index 0ab991d..1ad1241 100644 --- a/examples/pingpang/EchoServer.cpp +++ b/examples/pingpang/EchoServer.cpp @@ -29,11 +29,10 @@ void EchoServer::newMessage(shared_ptr connection, const char* bu { packetbuf->append(buf, static_cast(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); } } } diff --git a/uv/GlobalConfig.cpp b/uv/GlobalConfig.cpp index ef9fbdd..c10cbfc 100644 --- a/uv/GlobalConfig.cpp +++ b/uv/GlobalConfig.cpp @@ -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; diff --git a/uv/GlobalConfig.h b/uv/GlobalConfig.h index 818ca4d..654eea8 100644 --- a/uv/GlobalConfig.h +++ b/uv/GlobalConfig.h @@ -16,7 +16,10 @@ namespace uv { class PacketBuffer; -using ReadBufferFunc = std::function; +class Packet; +using ReadBufferStringFunc = std::function; +using ReadBufferPacketFunc = std::function; +using ReadBufferVoidFunc = std::function; class GlobalConfig { @@ -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 diff --git a/uv/Packet.cpp b/uv/Packet.cpp index ed6f028..6997655 100644 --- a/uv/Packet.cpp +++ b/uv/Packet.cpp @@ -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()) @@ -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; } diff --git a/uv/Packet.h b/uv/Packet.h index 9c45235..a68956a 100644 --- a/uv/Packet.h +++ b/uv/Packet.h @@ -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); diff --git a/uv/PacketBuffer.h b/uv/PacketBuffer.h index d8352c5..e36c343 100644 --- a/uv/PacketBuffer.h +++ b/uv/PacketBuffer.h @@ -20,7 +20,7 @@ Description: https://github.com/wlgq2/uv-cpp namespace uv { - +class Packet; class PacketBuffer { @@ -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; } diff --git a/uv/uv11.h b/uv/uv11.h index 24701f2..301d040 100644 --- a/uv/uv11.h +++ b/uv/uv11.h @@ -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"