mirror of
https://github.com/EQEmu/Server.git
synced 2025-12-11 16:51:29 +00:00
Simple static buffer
This commit is contained in:
parent
65bcc22f19
commit
ad28a4326a
@ -53,7 +53,24 @@ void EQ::Net::DaybreakConnectionManager::Attach(uv_loop_t *loop)
|
||||
uv_ip4_addr("0.0.0.0", m_options.port, &recv_addr);
|
||||
int rc = uv_udp_bind(&m_socket, (const struct sockaddr *)&recv_addr, UV_UDP_REUSEADDR);
|
||||
|
||||
rc = uv_udp_recv_start(&m_socket, AllocCallback, RecvCallback);
|
||||
static char static_buffer[65536];
|
||||
|
||||
rc = uv_udp_recv_start(&m_socket,
|
||||
[](uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
|
||||
buf->base = static_buffer;
|
||||
buf->len = sizeof(static_buffer); // Ensure it matches the buffer size
|
||||
},
|
||||
[](uv_udp_t* handle, ssize_t nread, const uv_buf_t* buf, const struct sockaddr* addr, unsigned flags) {
|
||||
DaybreakConnectionManager *c = (DaybreakConnectionManager*)handle->data;
|
||||
if (nread < 0 || addr == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
static char endpoint[16];
|
||||
uv_ip4_name((const sockaddr_in*)addr, endpoint, 16);
|
||||
static auto port = ntohs(((const sockaddr_in*)addr)->sin_port);
|
||||
c->ProcessPacket(endpoint, port, buf->base, nread);
|
||||
});
|
||||
|
||||
m_attached = loop;
|
||||
}
|
||||
|
||||
@ -11,27 +11,6 @@
|
||||
#include <queue>
|
||||
#include <list>
|
||||
|
||||
class BufferPool {
|
||||
public:
|
||||
// Allocate a buffer from the pool or create a new one
|
||||
uv_buf_t AllocateBuffer(size_t size) {
|
||||
if (!pool.empty()) {
|
||||
auto buf = std::move(pool.back());
|
||||
pool.pop_back();
|
||||
return uv_buf_init(buf.release(), size);
|
||||
}
|
||||
return uv_buf_init(new char[size], size);
|
||||
}
|
||||
|
||||
// Return a buffer to the pool for reuse
|
||||
void ReleaseBuffer(char* buffer) {
|
||||
pool.emplace_back(buffer);
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<std::unique_ptr<char[]>> pool; // Pool of reusable buffers
|
||||
};
|
||||
|
||||
namespace EQ
|
||||
{
|
||||
namespace Net
|
||||
@ -339,35 +318,6 @@ namespace EQ
|
||||
void Attach(uv_loop_t *loop);
|
||||
void Detach();
|
||||
|
||||
static void AllocCallback(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
|
||||
auto* manager = static_cast<DaybreakConnectionManager*>(handle->data);
|
||||
*buf = manager->buffer_pool.AllocateBuffer(suggested_size); // Use buffer pool
|
||||
}
|
||||
|
||||
static void RecvCallback(uv_udp_t* handle, ssize_t nread, const uv_buf_t* buf, const struct sockaddr* addr, unsigned flags) {
|
||||
auto* manager = static_cast<DaybreakConnectionManager*>(handle->data);
|
||||
|
||||
if (nread < 0 || addr == nullptr) {
|
||||
manager->buffer_pool.ReleaseBuffer(buf->base); // Return buffer to pool
|
||||
return;
|
||||
}
|
||||
|
||||
// Get endpoint details
|
||||
char endpoint[16];
|
||||
uv_ip4_name((const sockaddr_in*)addr, endpoint, sizeof(endpoint));
|
||||
auto port = ntohs(((const sockaddr_in*)addr)->sin_port);
|
||||
|
||||
std::cout << "Received " << nread << " bytes from " << endpoint << ":" << port << std::endl;
|
||||
|
||||
// Process the packet
|
||||
manager->ProcessPacket(endpoint, port, buf->base, nread);
|
||||
|
||||
// Release buffer back to the pool
|
||||
manager->buffer_pool.ReleaseBuffer(buf->base);
|
||||
}
|
||||
|
||||
BufferPool buffer_pool;
|
||||
|
||||
EQ::Random m_rand;
|
||||
uv_timer_t m_timer;
|
||||
uv_udp_t m_socket;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user