diff options
Diffstat (limited to '')
-rw-r--r-- | src/comms.cpp | 21 |
1 files changed, 6 insertions, 15 deletions
diff --git a/src/comms.cpp b/src/comms.cpp index 14fcd61..7661155 100644 --- a/src/comms.cpp +++ b/src/comms.cpp @@ -1,3 +1,6 @@ +// Author Name: Matt Strapp +// Date: 25 April 2022 +// x500: strap012 #include "comms.hpp" int Send_NonBlocking(int sockFD, BYTE *data, struct CONN_STAT *pStat, @@ -8,17 +11,14 @@ int Send_NonBlocking(int sockFD, BYTE *data, struct CONN_STAT *pStat, ftime(&(pStat->lastTime)); int n = send(sockFD, data + pStat->messageLen - pStat->nToDo, pStat->nToDo, 0); - // Log("Send_NonBlocking n: %d", n); if (n >= 0) { pStat->nToDo -= n; } else if (n < 0 && (errno == ECONNRESET || errno == EPIPE)) { - // Log("Connection closed."); close(sockFD); return -1; } else if (n < 0 && (errno == EWOULDBLOCK)) { // The socket becomes non-writable. Exit now to prevent blocking. // OS will notify us when we can write - // Log("Writing would block. Waiting for OS to notify send."); pPeer->events |= POLLWRNORM; return 0; } else { @@ -26,7 +26,6 @@ int Send_NonBlocking(int sockFD, BYTE *data, struct CONN_STAT *pStat, } } - // Log("Message was completely written out (messagelen was message written // out)"); Log("What's currently in there: nBuffered %d, messageLen: %d, nToDo // %d", pStat->nBuffered, pStat->messageLen, pStat->nToDo); memcpy(data, data + pStat->messageLen, BUF_LEN - pStat->messageLen); // Good @@ -36,22 +35,18 @@ int Send_NonBlocking(int sockFD, BYTE *data, struct CONN_STAT *pStat, if (pStat->nBuffered > 0) { // start off the next send if one is queued Header nextHeader; nextHeader.decode(data); - // Log("Another message is queued of size 13 + %d", nextHeader.m_size); pStat->messageLen = HEADER_LEN + nextHeader.m_size; pStat->nToDo = HEADER_LEN + nextHeader.m_size; } else { - // Log("No other messages are queued"); pStat->messageLen = 0; pStat->nToDo = 0; pPeer->events &= ~POLLWRNORM; // no more bytes to send? Stop listening for // the writable cue. } - // Log("What's in there now: nBuffered %d, messageLen: %d, nToDo %d", // pStat->nBuffered, pStat->messageLen, pStat->nToDo); - // Change the connection from a sender to a reciever (used by client) + // Change the connection from a sender to a receiver (used by client) if (pStat->changeDirection && (pStat->nToDo == 0)) { - // Log("Changing direction!: nBuffered: %d", pStat->nBuffered); pStat->direction = (pStat->direction == C2S) ? S2C : C2S; pStat->expectingHeader = true; pStat->nToDo = HEADER_LEN; @@ -71,16 +66,13 @@ int Recv_NonBlocking(int sockFD, BYTE *data, struct CONN_STAT *pStat, // pStat->nToDo %d",pStat->nToDo); while (pStat->nToDo > 0) { int n = recv(sockFD, data + pStat->nBuffered, pStat->nToDo, 0); - // Log("Rev_NonBlocking recieved %d bytes", n); if (n > 0) { pStat->nBuffered += n; pStat->nToDo -= n; } else if (n == 0 || (n < 0 && errno == ECONNRESET)) { - // Log("\nConnection closed. n = %d", n); close(sockFD); return -1; } else if (n < 0 && (errno == EWOULDBLOCK)) { - // Log("\nWould block waiting for read..."); // The socket becomes non-readable. Exit now to prevent blocking. // OS will notify us when we can read return 0; @@ -125,8 +117,7 @@ void Header::encode(BYTE *buf) { memcpy(&buf[index], m_name, 8); index += 8; - // NOTE: There was a problem encoding very large sizes. - i2buf(m_size, &buf[index]); + LittleEndianToBig(m_size, &buf[index]); index += 4; } @@ -144,7 +135,7 @@ void Header::decode(BYTE *buf) { for (int x = 0; x < MAX_USERNAME_LEN; x++) m_name[x] = buf[index++]; - buf2i(&buf[index], m_size); + BigEndianToLittle(&buf[index], m_size); index += 4; } |