diff options
Diffstat (limited to '')
-rw-r--r-- | src/client.cpp | 71 |
1 files changed, 27 insertions, 44 deletions
diff --git a/src/client.cpp b/src/client.cpp index d2b7562..b6f49b5 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -1,3 +1,6 @@ +// Author Name: Matt Strapp +// Date: 25 April 2022 +// x500: strap012 #include "client.hpp" // Connection 0: Client to Server data communication @@ -31,7 +34,6 @@ void RemoveConnection(int i) { memmove(buf + i, buf + i + 1, (nConns - i) * sizeof(BYTE *)); } nConns--; - // Log("[%s] %d active connections after removal", SERVER_NAME, nConns); } int Send_Blocking(int sockFD, const BYTE *data, int len) { @@ -65,7 +67,6 @@ void DoClient(const char *svrIP, int svrPort, const char *fileName) { struct timeb currentTime; ftime(¤tTime); id = currentTime.time ^ (((int)getpid() & 0x0000FFFF) << 16); - // Log("My identifier is %d (%08X)", id, id); // create the data communication sockets for (int i = 0; i < 2; i++) { @@ -75,7 +76,7 @@ void DoClient(const char *svrIP, int svrPort, const char *fileName) { if (connect(fd, (const struct sockaddr *)&serverAddr, sizeof(serverAddr)) != 0) { - Error("C2s cannot connect to server %s:%d.", svrIP, svrPort); + Error("Cannot connect to server %s:%d.", svrIP, svrPort); } enum Direction direction; @@ -93,7 +94,7 @@ void DoClient(const char *svrIP, int svrPort, const char *fileName) { memcpy(header.m_name, "--------", MAX_USERNAME_LEN); header.m_size = 4; header.encode(connectBuf); - i2buf(id, connectBuf + HEADER_LEN); + LittleEndianToBig(id, connectBuf + HEADER_LEN); // header.displayContents(true); if (Send_Blocking(fd, connectBuf, HEADER_LEN + header.m_size) < 0) { @@ -116,7 +117,6 @@ void DoClient(const char *svrIP, int svrPort, const char *fileName) { connStat[i].messageLen = 0; if (i == S2C_DATA) { - // Log("S2C_DATA is setting POLLRDNORM"); connStat[i].expectingHeader = true; connStat[i].nToDo = HEADER_LEN; peers[i].events |= POLLRDNORM; // S2C reads @@ -150,7 +150,7 @@ void DoClient(const char *svrIP, int svrPort, const char *fileName) { if (fgets(line, LINE_SIZE, fp) == NULL) { Log("End of command file."); endOfFile = true; - goto DO_POLL; + exit(0); } new_line_pos = strchr(line, '\n'); @@ -264,10 +264,10 @@ void DoClient(const char *svrIP, int svrPort, const char *fileName) { } offset = 0; - i2buf(id, &stagingBuf[offset]); // stagingBuf skips header - offset += 4; // unique id + LittleEndianToBig(id, &stagingBuf[offset]); // stagingBuf skips header + offset += 4; // unique id filenameLen = strlen(tok); - i2buf(filenameLen, &stagingBuf[offset]); + LittleEndianToBig(filenameLen, &stagingBuf[offset]); offset += 4; // int length of file name memcpy(&stagingBuf[offset], tok, strlen(tok)); @@ -276,7 +276,7 @@ void DoClient(const char *svrIP, int svrPort, const char *fileName) { nbytes = file2buf(tok, &stagingBuf[offset + 4]); if (nbytes == -1) break; - i2buf(nbytes, &stagingBuf[offset]); + LittleEndianToBig(nbytes, &stagingBuf[offset]); offset += 4; // file size int offset += nbytes; // file length (offest now tells how many bytes to // copy into buf) @@ -294,8 +294,8 @@ void DoClient(const char *svrIP, int svrPort, const char *fileName) { Log("Cannot connect to server for file transfer"); } else { if (command == SENDF) - Log("[%s] Sending %s to everyone currently logged in.", - NAME, tok); + Log("[%s] Sending %s to everyone currently logged in.", NAME, + tok); else Log("[%s] Sending %s to %s.", NAME, tok, header.m_name); @@ -323,46 +323,39 @@ void DoClient(const char *svrIP, int svrPort, const char *fileName) { free(stagingBuf); } - DO_POLL: int nReady = poll(peers, nConns, POLL_TIMEOUT); - // Log("nReady %d", nReady); if (nConns == 0) { - Log("[%s] The server has gone offline.", NAME); + Log("[%s] The server has gone offline, exiting.", NAME); return; } for (int i = 0; i < nConns; i++) { // Handle reading data (s2c) if (peers[i].revents & (POLLRDNORM | POLLERR | POLLHUP)) { - // Log("Recieving nonblocking!"); if (connStat[i].direction == S2C) { if (Recv_NonBlocking(peers[i].fd, buf[i], &connStat[i], &peers[i]) < 0) { - // Log("Line %d: Removing connection %d", __LINE__, i); RemoveConnection(i); - goto NEXT_CONNECTION; + continue; } if (connStat[i].nToDo == 0) { if (processReception(i) != 0) { - // Log("Line %d: Removing connection %d", __LINE__, i); RemoveConnection(i); } - goto NEXT_CONNECTION; + continue; } } } // Handle sending data (c2s) if (peers[i].revents & (POLLWRNORM | POLLERR | POLLHUP)) { - // Log("Sending nonblocking!"); if (connStat[i].direction == C2S) { if (Send_NonBlocking(peers[i].fd, buf[i], &connStat[i], &peers[i]) < 0) { - // Log("Line %d: Removing connection %d", __LINE__, i); RemoveConnection(i); - goto NEXT_CONNECTION; + continue; } } } @@ -373,7 +366,6 @@ void DoClient(const char *svrIP, int svrPort, const char *fileName) { C2S_SERVICE_FREQ) && (connStat[i].nToDo == 0) && (connStat[i].messageLen == 0) && (connStat[i].nBuffered == 0)) { - // Log("[%s] Pinging server on connection %d due to timeout", // SERVER_NAME, i); Header header; header.setFlags(C2S, SUCCESS, PRIV, SIGN); @@ -388,9 +380,6 @@ void DoClient(const char *svrIP, int svrPort, const char *fileName) { peers[i].events |= POLLWRNORM; } - - NEXT_CONNECTION: - asm("nop"); } } @@ -411,16 +400,14 @@ int processReception(int i) { Header header; header.decode(buf[i]); - if (connStat[i] - .expectingHeader) { // Expecting a header read sequence was completed - // Log("\nProcessing header reception."); - // header.displayContents(true); + if (connStat[i].expectingHeader) { // Expecting a header read sequence was + // completed header.displayContents(true); if (header.m_size > 0) { connStat[i].expectingHeader = false; connStat[i].nToDo = header.m_size; } else if (header.m_size == 0) { // printMessage(i); - doClientCommand(i); + DoCommand(i); connStat[i].expectingHeader = true; connStat[i].nBuffered = 0; connStat[i].nToDo = HEADER_LEN; @@ -429,9 +416,8 @@ int processReception(int i) { return -1; // Error, signal to caller to end connection } } else { // expecting a data read sequence was completed - // Log("\nProcessing data reception."); - // printMessage(i); - doClientCommand(i); + // printMessage(i); + DoCommand(i); connStat[i].expectingHeader = true; connStat[i].nBuffered = 0; connStat[i].nToDo = HEADER_LEN; @@ -439,7 +425,6 @@ int processReception(int i) { } if (connStat[i].shouldClose) { - // Log("[%s] Line %d: Removing connection %d due to shouldClose", SERVER_NAME, // __LINE__, i); RemoveConnection(i); } @@ -447,7 +432,7 @@ int processReception(int i) { return 0; } -void doClientCommand(int i) { +void DoCommand(int i) { Header header; header.decode(buf[i]); @@ -476,7 +461,7 @@ void doClientCommand(int i) { case SENDF2: // "[person] File transfer: <file name>" // First 4 bytes are file id - buf2i(&buf[i][HEADER_LEN], file_id); + BigEndianToLittle(&buf[i][HEADER_LEN], file_id); if (header.m_command == SENDF) Log("[%s] Initiating file transfer: %s", header.m_name, &buf[i][HEADER_LEN + 4]); @@ -520,7 +505,7 @@ void doClientCommand(int i) { 4 + header.m_size; // [client id] [file id] [file name] outHeader.encode(buf[nConns - 1]); offset = HEADER_LEN; - i2buf(id, buf[nConns - 1] + offset); // encode client id + LittleEndianToBig(id, buf[nConns - 1] + offset); // encode client id offset += 4; memcpy(&buf[nConns - 1][offset], &buf[i][HEADER_LEN], header.m_size); // encode file id and file name @@ -535,9 +520,9 @@ void doClientCommand(int i) { break; case GETF: if (header.m_flag == SUCCESS) { - buf2i(&buf[i][HEADER_LEN], filenameLen); + BigEndianToLittle(&buf[i][HEADER_LEN], filenameLen); memcpy(filename, &buf[i][HEADER_LEN + 4], filenameLen); - buf2i(&buf[i][HEADER_LEN + 4 + filenameLen], fileLen); + BigEndianToLittle(&buf[i][HEADER_LEN + 4 + filenameLen], fileLen); buf2file(&buf[i][HEADER_LEN + 4 + filenameLen + 4], fileLen, filename); if (header.m_recipient == PUB) Log("[%s] File transfer complete: %s", header.m_name, filename); @@ -552,12 +537,10 @@ void doClientCommand(int i) { connStat[i].shouldClose = true; break; case PING: - // Log("Ping from %s", header.m_name); asm("nop"); // do nothing break; default: - Log("[%s] No doClientCommand() for %s", NAME, - com2str(header.m_command)); + Log("[%s] No DoCommand() for %s", NAME, com2str(header.m_command)); } } |