Poll McSocket
This commit is contained in:
@@ -950,7 +950,7 @@ netprot::Packet netprot::makePack(void* ptr, PACKET_TYPE type) {
|
||||
return pck;
|
||||
}
|
||||
|
||||
std::vector<char*> netprot::recvPacks(SOCKET sock, Buffer* buf) {
|
||||
std::vector<char*> netprot::recvPacks(SOCKET sock, Buffer* buf, Buffer* outbuf) {
|
||||
std::vector<char*> lsPck;
|
||||
int len = buf->tmp? buf->tmp - buf->ptr: 0,
|
||||
end = 0;
|
||||
@@ -958,8 +958,18 @@ std::vector<char*> netprot::recvPacks(SOCKET sock, Buffer* buf) {
|
||||
* next = buf->tmp ? buf->tmp + 1: buf->ptr,
|
||||
* last = buf->tmp ? buf->tmp: buf->ptr;
|
||||
bool ended = true;
|
||||
struct pollfd fds[1];
|
||||
|
||||
fds[0].fd = sock;
|
||||
fds[0].events = POLLIN;
|
||||
|
||||
while (true) {
|
||||
if (!poll(fds, 1, 0)) {
|
||||
if (ended)
|
||||
buf->tmp = nullptr;
|
||||
return lsPck;
|
||||
}
|
||||
|
||||
int bytes = recv(sock, &buf->ptr[len], buf->len - len, 0);
|
||||
if (bytes <= 0) { // si recv() retourne -1 ou 0; ça veut dire qu'il y a plus rien a lire qui n'a pas déjà été traité.
|
||||
if (ended)
|
||||
@@ -983,15 +993,24 @@ std::vector<char*> netprot::recvPacks(SOCKET sock, Buffer* buf) {
|
||||
cursor = (char*)memchr(next, '\r', end);
|
||||
|
||||
if (cursor) {
|
||||
next = cursor;
|
||||
cursor--;
|
||||
next = cursor + 1;
|
||||
|
||||
cmp = memcmp(cursor, Footer, sizeof(uint32_t));
|
||||
if (cmp == 0) {
|
||||
lsPck.push_back(last);
|
||||
cursor += sizeof(uint32_t);
|
||||
last = cursor;
|
||||
next = cursor + 1;
|
||||
if (!outbuf) {
|
||||
lsPck.push_back(last);
|
||||
cursor += sizeof(uint32_t);
|
||||
last = cursor;
|
||||
next = cursor + 1;
|
||||
}
|
||||
else {
|
||||
memcpy(&outbuf->ptr[cursor - last], last, cursor - last);
|
||||
lsPck.push_back(&outbuf->ptr[cursor - last]);
|
||||
cursor += sizeof(uint32_t);
|
||||
last = cursor;
|
||||
next = cursor + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
Reference in New Issue
Block a user