I read the news today, oh boy.
This commit is contained in:
@@ -801,6 +801,40 @@ bool netprot::emptyPack(netprot::Packet pck) {
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<netprot::Packet> netprot::recvPacks(SOCKET sock, Buffer* buf) {
|
||||
std::vector<Packet> lsPck;
|
||||
int len = 0, end = 0;
|
||||
bool pck_received = false;
|
||||
char* cursor = nullptr;
|
||||
|
||||
while (true) {
|
||||
while (!pck_received) {
|
||||
int bytes = recv(sock, &buf->ptr[len], buf->len - len, NULL);
|
||||
if (bytes < 0)
|
||||
return lsPck;
|
||||
len += bytes;
|
||||
// TODO: Voir si on trouve un footer (5 '\0' d'affilee).
|
||||
// Si oui, mettre l'index du debut du footer dans
|
||||
// la variable "end" et mettre pck_received a true.
|
||||
}
|
||||
|
||||
cursor = &buf->ptr[end];
|
||||
|
||||
lsPck.push_back(getPack(buf));
|
||||
|
||||
while (*cursor == '\0') {
|
||||
cursor++;
|
||||
end++;
|
||||
}
|
||||
|
||||
if (cursor) { // mettre le début du prochain paquet au début du buffer.
|
||||
memcpy(buf->ptr, cursor, buf->len - end); // et voir s'il y a en un autre
|
||||
len = 0;
|
||||
pck_received = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
void netprot::sendPack<netprot::Packet>(SOCKET sock, Packet* pack, char** buf, uint32_t* buflen) {
|
||||
switch (pack->type) {
|
||||
@@ -838,6 +872,7 @@ void netprot::sendPack<netprot::Packet>(SOCKET sock, Packet* pack, char** buf, u
|
||||
return;
|
||||
case PACKET_TYPE::CHAT:
|
||||
sendPack<Chat>(sock, (Chat*)pack->ptr, buf, buflen);
|
||||
return;
|
||||
case PACKET_TYPE::ERRLOG:
|
||||
sendPack<ErrorLog>(sock, (ErrorLog*)pack->ptr, buf, buflen);
|
||||
return;
|
||||
|
@@ -28,7 +28,7 @@ namespace netprot {
|
||||
|
||||
struct Packet { // Pour pouvoir recevoir les paquets du recv() sans avoir à les aiguiller dans la même thread.
|
||||
void* ptr = nullptr; // Notez que le pointeur doit être supprimé séparément lorsqu'il n'est plus utile.
|
||||
PACKET_TYPE type;
|
||||
PACKET_TYPE type = PACKET_TYPE::ERR;
|
||||
};
|
||||
|
||||
struct Keys {
|
||||
@@ -145,10 +145,14 @@ namespace netprot {
|
||||
|
||||
template <class T> void sendPack(SOCKET sock, T* pack, Buffer* buf);
|
||||
template <class T> void sendPackTo(SOCKET sock, T* pack, Buffer* buf, sockaddr_in* sockad);
|
||||
|
||||
std::vector<Packet> recvPacks(SOCKET sock, Buffer* buf);
|
||||
|
||||
template <class T>
|
||||
void sendPack(SOCKET sock, T* pack, char** buf, uint32_t* buflen) {
|
||||
netprot::Serialize(pack, buf, buflen);
|
||||
memset(buf->ptr[buf->len], , '\0', sizeof(uint64_t) + sizeof(uint8_t));
|
||||
buf->len += sizeof(uint64_t) + sizeof(uint8_t)
|
||||
send(sock, *buf, *buflen, 0);
|
||||
*buflen = BUFFER_LENGTH;
|
||||
}
|
||||
@@ -157,6 +161,8 @@ namespace netprot {
|
||||
void sendPackTo(SOCKET sock, T* pack, char** buf, uint32_t* buflen, sockaddr_in* sockad) {
|
||||
sockaddr_in addr = *sockad;
|
||||
netprot::Serialize(pack, buf, buflen);
|
||||
memset(buf->ptr[buf->len], , '\0', sizeof(uint64_t) + sizeof(uint8_t));
|
||||
buf->len += sizeof(uint64_t) + sizeof(uint8_t)
|
||||
sendto(sock, *buf, *buflen, 0, (sockaddr*)&addr, sizeof(addr));
|
||||
*buflen = BUFFER_LENGTH;
|
||||
}
|
||||
@@ -164,6 +170,8 @@ namespace netprot {
|
||||
template <class T>
|
||||
void sendPack(SOCKET sock, T* pack, Buffer* buf) {
|
||||
netprot::Serialize(pack, &buf->ptr, &buf->len);
|
||||
memset(buf->ptr[buf->len], , '\0', sizeof(uint64_t) + sizeof(uint8_t));
|
||||
buf->len += sizeof(uint64_t) + sizeof(uint8_t)
|
||||
send(sock, buf->ptr, buf->len, 0);
|
||||
buf->rstLen();
|
||||
}
|
||||
@@ -172,8 +180,12 @@ namespace netprot {
|
||||
void sendPackTo(SOCKET sock, T* pack, Buffer* buf, sockaddr_in* sockad) {
|
||||
sockaddr_in addr = *sockad;
|
||||
netprot::Serialize(pack, &buf->ptr, &buf->len);
|
||||
memset(buf->ptr[buf->len], ,'\0', sizeof(uint64_t) + sizeof(uint8_t));
|
||||
buf->len += sizeof(uint64_t) + sizeof(uint8_t)
|
||||
sendto(sock, buf->ptr, buf->len, 0, (sockaddr*)&addr, sizeof(addr));
|
||||
buf->rstLen();
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user