From 2f42465ab9f3d5c068ccf5068f2fc8ae9224bbca Mon Sep 17 00:00:00 2001 From: sbarrettCC <98108807+sbarrettCC@users.noreply.github.com> Date: Thu, 27 Jan 2022 23:51:57 -0500 Subject: [PATCH 01/11] Create msbuild.yml --- .github/workflows/msbuild.yml | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 .github/workflows/msbuild.yml diff --git a/.github/workflows/msbuild.yml b/.github/workflows/msbuild.yml new file mode 100644 index 00000000..29b6aceb --- /dev/null +++ b/.github/workflows/msbuild.yml @@ -0,0 +1,32 @@ +name: MSBuild + +on: [push] + +env: + # Path to the solution file relative to the root of the project. + SOLUTION_FILE_PATH: . + + # Configuration type to build. + # You can convert this to a build matrix if you need coverage of multiple configuration types. + # https://docs.github.com/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix + BUILD_CONFIGURATION: Release + +jobs: + build: + runs-on: windows-latest + + steps: + - uses: actions/checkout@v2 + + - name: Add MSBuild to PATH + uses: microsoft/setup-msbuild@v1.0.2 + + - name: Restore NuGet packages + working-directory: ${{env.GITHUB_WORKSPACE}} + run: nuget restore ${{env.SOLUTION_FILE_PATH}} + + - name: Build + working-directory: ${{env.GITHUB_WORKSPACE}} + # Add additional options to the MSBuild command line here (like platform or verbosity level). + # See https://docs.microsoft.com/visualstudio/msbuild/msbuild-command-line-reference + run: msbuild /m /p:Configuration=${{env.BUILD_CONFIGURATION}} ${{env.SOLUTION_FILE_PATH}} From f98a75569a7676caf02333b5f141504196673e81 Mon Sep 17 00:00:00 2001 From: sbarrettCC <98108807+sbarrettCC@users.noreply.github.com> Date: Thu, 27 Jan 2022 23:57:22 -0500 Subject: [PATCH 02/11] Update msbuild.yml --- .github/workflows/msbuild.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/msbuild.yml b/.github/workflows/msbuild.yml index 29b6aceb..10dfaf58 100644 --- a/.github/workflows/msbuild.yml +++ b/.github/workflows/msbuild.yml @@ -9,7 +9,7 @@ env: # Configuration type to build. # You can convert this to a build matrix if you need coverage of multiple configuration types. # https://docs.github.com/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix - BUILD_CONFIGURATION: Release + BUILD_CONFIGURATION: Win32 jobs: build: From 524b16c921578e040a1e906368ae6e107b6711a1 Mon Sep 17 00:00:00 2001 From: sbarrettCC <98108807+sbarrettCC@users.noreply.github.com> Date: Thu, 27 Jan 2022 23:59:26 -0500 Subject: [PATCH 03/11] Update msbuild.yml --- .github/workflows/msbuild.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/msbuild.yml b/.github/workflows/msbuild.yml index 10dfaf58..cb9f5085 100644 --- a/.github/workflows/msbuild.yml +++ b/.github/workflows/msbuild.yml @@ -9,7 +9,6 @@ env: # Configuration type to build. # You can convert this to a build matrix if you need coverage of multiple configuration types. # https://docs.github.com/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix - BUILD_CONFIGURATION: Win32 jobs: build: From a4b58666623874e02fbdc0a350b8a9fcbbc36b63 Mon Sep 17 00:00:00 2001 From: sbarrettCC <98108807+sbarrettCC@users.noreply.github.com> Date: Fri, 28 Jan 2022 00:02:08 -0500 Subject: [PATCH 04/11] Update msbuild.yml --- .github/workflows/msbuild.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/msbuild.yml b/.github/workflows/msbuild.yml index cb9f5085..4fdac5aa 100644 --- a/.github/workflows/msbuild.yml +++ b/.github/workflows/msbuild.yml @@ -28,4 +28,4 @@ jobs: working-directory: ${{env.GITHUB_WORKSPACE}} # Add additional options to the MSBuild command line here (like platform or verbosity level). # See https://docs.microsoft.com/visualstudio/msbuild/msbuild-command-line-reference - run: msbuild /m /p:Configuration=${{env.BUILD_CONFIGURATION}} ${{env.SOLUTION_FILE_PATH}} + run: msbuild /m ${{env.SOLUTION_FILE_PATH}} From e91d1360982e53d0f318be10030795c7c28e0b9e Mon Sep 17 00:00:00 2001 From: sbarrettCC <98108807+sbarrettCC@users.noreply.github.com> Date: Fri, 28 Jan 2022 00:07:34 -0500 Subject: [PATCH 05/11] Update msbuild.yml --- .github/workflows/msbuild.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/msbuild.yml b/.github/workflows/msbuild.yml index 4fdac5aa..c0cb906f 100644 --- a/.github/workflows/msbuild.yml +++ b/.github/workflows/msbuild.yml @@ -29,3 +29,7 @@ jobs: # Add additional options to the MSBuild command line here (like platform or verbosity level). # See https://docs.microsoft.com/visualstudio/msbuild/msbuild-command-line-reference run: msbuild /m ${{env.SOLUTION_FILE_PATH}} + + - name: Test + working-directory: ${{env.GITHUB_WORKSPACE}} + run: Bin\Debug\SocketDemo.exe From 0e98be723ff1f84ae8e5064276f27cad5a4ce786 Mon Sep 17 00:00:00 2001 From: sbarrettCC <98108807+sbarrettCC@users.noreply.github.com> Date: Fri, 28 Jan 2022 00:07:53 -0500 Subject: [PATCH 06/11] Create msbuild.yml From 1ec43a447f4a51646b1bcd2568777b3e4fd5481e Mon Sep 17 00:00:00 2001 From: sbarrettCC <98108807+sbarrettCC@users.noreply.github.com> Date: Fri, 28 Jan 2022 00:13:41 -0500 Subject: [PATCH 07/11] Update msbuild.yml --- .github/workflows/msbuild.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/msbuild.yml b/.github/workflows/msbuild.yml index c0cb906f..9daa6f29 100644 --- a/.github/workflows/msbuild.yml +++ b/.github/workflows/msbuild.yml @@ -32,4 +32,4 @@ jobs: - name: Test working-directory: ${{env.GITHUB_WORKSPACE}} - run: Bin\Debug\SocketDemo.exe + run: tree From f5029ab026a54a563b6021abc25596f768adcff8 Mon Sep 17 00:00:00 2001 From: sbarrettCC <98108807+sbarrettCC@users.noreply.github.com> Date: Fri, 28 Jan 2022 15:18:58 -0500 Subject: [PATCH 08/11] Update msbuild.yml --- .github/workflows/msbuild.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/msbuild.yml b/.github/workflows/msbuild.yml index 9daa6f29..4a278200 100644 --- a/.github/workflows/msbuild.yml +++ b/.github/workflows/msbuild.yml @@ -32,4 +32,4 @@ jobs: - name: Test working-directory: ${{env.GITHUB_WORKSPACE}} - run: tree + run: RobotCat\Bin\Debug\SocketDemo.exe From c6bbdfe7a0adbe6c30853be260d9d940f35f3543 Mon Sep 17 00:00:00 2001 From: sbarrettCC <98108807+sbarrettCC@users.noreply.github.com> Date: Mon, 31 Jan 2022 10:48:52 -0500 Subject: [PATCH 09/11] Update msbuild.yml --- .github/workflows/msbuild.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.github/workflows/msbuild.yml b/.github/workflows/msbuild.yml index 4a278200..4fdac5aa 100644 --- a/.github/workflows/msbuild.yml +++ b/.github/workflows/msbuild.yml @@ -29,7 +29,3 @@ jobs: # Add additional options to the MSBuild command line here (like platform or verbosity level). # See https://docs.microsoft.com/visualstudio/msbuild/msbuild-command-line-reference run: msbuild /m ${{env.SOLUTION_FILE_PATH}} - - - name: Test - working-directory: ${{env.GITHUB_WORKSPACE}} - run: RobotCat\Bin\Debug\SocketDemo.exe From 31e63a7cc0835e99975a7a5929ce262ae9d4073e Mon Sep 17 00:00:00 2001 From: charlie sanford Date: Wed, 4 May 2022 21:29:40 -0400 Subject: [PATCH 10/11] work --- RoboCat/Chapter3.vcxproj | 2 + RoboCat/Inc/PlayerUser.h | 31 ++++ RoboCat/Inc/RoboCatPCH.h | 2 +- RoboCat/Inc/SocketAddress.h | 1 + RoboCat/Inc/SocketUtil.h | 1 + RoboCat/Inc/StringUtils.h | 1 + RoboCat/Inc/TCPSocket.h | 1 + RoboCat/Inc/UDPSocket.h | 2 +- RoboCat/Src/Main.cpp | 27 +++- RoboCat/Src/PlayerUser.cpp | 282 ++++++++++++++++++++++++++++++++++++ 10 files changed, 347 insertions(+), 3 deletions(-) create mode 100644 RoboCat/Inc/PlayerUser.h create mode 100644 RoboCat/Src/PlayerUser.cpp diff --git a/RoboCat/Chapter3.vcxproj b/RoboCat/Chapter3.vcxproj index f0d7817a..d962a51f 100644 --- a/RoboCat/Chapter3.vcxproj +++ b/RoboCat/Chapter3.vcxproj @@ -370,6 +370,7 @@ + @@ -379,6 +380,7 @@ + diff --git a/RoboCat/Inc/PlayerUser.h b/RoboCat/Inc/PlayerUser.h new file mode 100644 index 00000000..86cf6644 --- /dev/null +++ b/RoboCat/Inc/PlayerUser.h @@ -0,0 +1,31 @@ +#pragma once + +#include "RoboCatPCH.h" + +struct PlayerUser +{ + int playerNumber; + std::string playerName; + std::string SEPERATOR_TOKEN = "!"; + int sendRecvFlag = -1; + bool quit; + float delayTimer = 0; + + + TCPSocketPtr sendSocket, recvSocket, recvConnSocket; + + PlayerUser(); + PlayerUser(int _pnum); + ~PlayerUser(); + + void initTcpClient(std::string sendPort, std::string recvPort); + void initTcpServer(std::string listenPort); + + std::string messageIntoString(std::string _message); + void decodeMessageString(std::string _messageString); + void sendMessage(std::string _message); + + void closeSockets(); + void shutdown(); + +}; \ No newline at end of file diff --git a/RoboCat/Inc/RoboCatPCH.h b/RoboCat/Inc/RoboCatPCH.h index fa2871f8..9b5f8d1f 100644 --- a/RoboCat/Inc/RoboCatPCH.h +++ b/RoboCat/Inc/RoboCatPCH.h @@ -1,4 +1,4 @@ - +#pragma once #include diff --git a/RoboCat/Inc/SocketAddress.h b/RoboCat/Inc/SocketAddress.h index 05bad2b8..9b6e9b5c 100644 --- a/RoboCat/Inc/SocketAddress.h +++ b/RoboCat/Inc/SocketAddress.h @@ -1,3 +1,4 @@ +#pragma once class SocketAddress { public: diff --git a/RoboCat/Inc/SocketUtil.h b/RoboCat/Inc/SocketUtil.h index 50a726c3..eaf92ce7 100644 --- a/RoboCat/Inc/SocketUtil.h +++ b/RoboCat/Inc/SocketUtil.h @@ -1,3 +1,4 @@ +#pragma once enum class SocketAddressFamily { INET = AF_INET, diff --git a/RoboCat/Inc/StringUtils.h b/RoboCat/Inc/StringUtils.h index b46b745d..a2685c8c 100644 --- a/RoboCat/Inc/StringUtils.h +++ b/RoboCat/Inc/StringUtils.h @@ -1,3 +1,4 @@ +#pragma once namespace StringUtils { string GetCommandLineArg( int inIndex ); diff --git a/RoboCat/Inc/TCPSocket.h b/RoboCat/Inc/TCPSocket.h index 0779a82b..db45619c 100644 --- a/RoboCat/Inc/TCPSocket.h +++ b/RoboCat/Inc/TCPSocket.h @@ -1,3 +1,4 @@ +#pragma once class TCPSocket { public: diff --git a/RoboCat/Inc/UDPSocket.h b/RoboCat/Inc/UDPSocket.h index 939df407..dcf105c3 100644 --- a/RoboCat/Inc/UDPSocket.h +++ b/RoboCat/Inc/UDPSocket.h @@ -1,4 +1,4 @@ - +#pragma once class UDPSocket { diff --git a/RoboCat/Src/Main.cpp b/RoboCat/Src/Main.cpp index c844a29f..73b24000 100644 --- a/RoboCat/Src/Main.cpp +++ b/RoboCat/Src/Main.cpp @@ -1,6 +1,13 @@ - #include "RoboCatPCH.h" +#include "PlayerUser.h" + + +PlayerUser* user; + +std::string CLIENT_SEND_PORT = "1250", CLIENT_RECV_PORT = "2250"; + + #if _WIN32 int main(int argc, const char** argv) { @@ -16,6 +23,24 @@ int main(int argc, const char** argv) #endif SocketUtil::StaticInit(); + + CLIENT_SEND_PORT = std::to_string(rand() % 1000 + 8999); + CLIENT_RECV_PORT = std::to_string(8080);// i think this works?? + + // Server Code -------------- + bool isServer = StringUtils::GetCommandLineArg(1) == "server"; + if (isServer) + { + user = new PlayerUser(2); + user->initTcpServer(CLIENT_RECV_PORT); + } + + // Client Code -------------- + if (!isServer) + { + user = new PlayerUser(1); + user->initTcpClient(CLIENT_SEND_PORT, CLIENT_RECV_PORT); + } SocketUtil::CleanUp(); diff --git a/RoboCat/Src/PlayerUser.cpp b/RoboCat/Src/PlayerUser.cpp new file mode 100644 index 00000000..bc95b078 --- /dev/null +++ b/RoboCat/Src/PlayerUser.cpp @@ -0,0 +1,282 @@ +#include "RoboCatPCH.h" +#include "PlayerUser.h" +#include +#include + +// FUNCTION FROM user704565 AT https://stackoverflow.com/a/16286297: +std::vector split(std::string str, std::string sep) { + char* cstr = const_cast(str.c_str()); + char* current; + std::vector arr; + current = strtok(cstr, sep.c_str()); + while (current != NULL) { + arr.push_back(current); + current = strtok(NULL, sep.c_str()); + } + return arr; +} + + +PlayerUser::PlayerUser() +{ + playerName = ""; +} + +PlayerUser::PlayerUser(int _pnum) +{ + sendSocket = SocketUtil::CreateTCPSocket(SocketAddressFamily::INET); + recvSocket = SocketUtil::CreateTCPSocket(SocketAddressFamily::INET); + + playerNumber = _pnum; + playerName = _pnum == 0 ? "Server" : "Player" + std::to_string(playerNumber); + //std::cout << playerName << " client "<< " Subsystems Initialized!\n"; +} + +PlayerUser::~PlayerUser() +{ + closeSockets(); + +} + +void PlayerUser::initTcpClient(std::string sendPort, std::string recvPort) +{ + sendRecvFlag = 0; + + // Create socket + sendSocket = SocketUtil::CreateTCPSocket(SocketAddressFamily::INET); + if (sendSocket == nullptr) + { + SocketUtil::ReportError("Creating client socket"); + ExitProcess(1); + } + + LOG("%s", "Client socket created"); + + // Bind() - "Bind" socket -> tells OS we want to use a specific address + + std::string address = StringUtils::Sprintf("127.0.0.1:%s", sendPort.c_str()); + SocketAddressPtr clientAddress = SocketAddressFactory::CreateIPv4FromString(address.c_str()); + if (clientAddress == nullptr) + { + SocketUtil::ReportError("Creating client address"); + ExitProcess(1); + } + + if (sendSocket->Bind(*clientAddress) != NO_ERROR) + { + SocketUtil::ReportError("Binding client socket"); + // This doesn't block! + ExitProcess(1); + } + + LOG("%s", "Bound client socket"); + + // Connect() -> Connect socket to remote host + + SocketAddressPtr servAddress = SocketAddressFactory::CreateIPv4FromString("127.0.0.1:"+recvPort); + if (servAddress == nullptr) + { + SocketUtil::ReportError("Creating server address"); + ExitProcess(1); + } + + if (sendSocket->Connect(*servAddress) != NO_ERROR) + { + SocketUtil::ReportError("Connecting to server"); + ExitProcess(1); + } + + LOG("%s", "Connected to server!"); + + quit = false; + std::thread receiveNewThread([&]() { // don't use [&] :) + while (!quit) // Need to add a quit here to have it really exit! + { + char buffer[4096]; + int32_t bytesReceived = sendSocket->Receive(buffer, 4096); + if (bytesReceived == 0) + { + // handle disconnect + } + if (bytesReceived < 0) + { + SocketUtil::ReportError("Receiving"); + return; + } + + std::string receivedMsg(buffer, bytesReceived); + LOG("Received message: %s", receivedMsg.c_str()); + if (receivedMsg[0] == '$') // SEND MESSAGE + { + decodeMessageString(receivedMsg.erase(0, 1)); // display message + } + else if (receivedMsg[0] == '!') // CHANGE USERNAME + { + playerName = receivedMsg.erase(0, 1); + } + } + }); + + std::cout << "Press enter to exit at any time!\n"; + std::cin.get(); + quit = true; +// recvConnSocket->~TCPSocket(); // Forcibly close socket (shouldn't call destructors like this -- make a new function for it! + receiveNewThread.join(); +} + +void PlayerUser::initTcpServer(std::string listenPort) +{ + sendRecvFlag = 1; + // Create socket + recvSocket = SocketUtil::CreateTCPSocket(SocketAddressFamily::INET); + if (recvSocket == nullptr) + { + SocketUtil::ReportError("Creating listening socket"); + ExitProcess(1); + } + + //recvSocket->SetNonBlockingMode(true); + + LOG("%s", "Listening socket created"); + + // Bind() - "Bind" socket -> tells OS we want to use a specific address + + SocketAddressPtr listenAddress = SocketAddressFactory::CreateIPv4FromString("127.0.0.1:"+ listenPort); + if (listenAddress == nullptr) + { + SocketUtil::ReportError("Creating listen address"); + ExitProcess(1); + } + + if (recvSocket->Bind(*listenAddress) != NO_ERROR) + { + SocketUtil::ReportError("Binding listening socket"); + // This doesn't block! + ExitProcess(1); + } + + LOG("%s", "Bound listening socket"); + + // Blocking function call -> Waits for some input; halts the program until something "interesting" happens + // Non-Blocking function call -> Returns right away, as soon as the action is completed + + // Listen() - Listen on socket -> Non-blocking; tells OS we care about incoming connections on this socket + if (recvSocket->Listen() != NO_ERROR) + { + SocketUtil::ReportError("Listening on listening socket"); + ExitProcess(1); + } + + LOG("%s", "Listening on socket"); + + // Accept() - Accept on socket -> Blocking; Waits for incoming connection and completes TCP handshake + + LOG("%s", "Waiting to accept connections..."); + SocketAddress incomingAddress; + recvConnSocket = recvSocket->Accept(incomingAddress); + while (recvConnSocket == nullptr) + { + recvConnSocket = recvSocket->Accept(incomingAddress); + // SocketUtil::ReportError("Accepting connection"); + // ExitProcess(1); + } + + LOG("Accepted connection from %s", incomingAddress.ToString().c_str()); + + quit = false; + std::thread receiveThread([&]() { // don't use [&] :) + while (!quit) // Need to add a quit here to have it really exit! + { + char buffer[4096]; + int32_t bytesReceived = recvConnSocket->Receive(buffer, 4096); + if (bytesReceived == 0) + { + // handle disconnect + } + if (bytesReceived < 0) + { + SocketUtil::ReportError("Receiving"); + return; + } + + std::string receivedMsg(buffer, bytesReceived); + LOG("Received message from %s: %s", incomingAddress.ToString().c_str(), receivedMsg.c_str()); + if (receivedMsg[0] == '$') // SEND MESSAGE + { + decodeMessageString(receivedMsg.erase(0, 1)); // display message + } + else if (receivedMsg[0] == '!') // CHANGE USERNAME + { + playerName = receivedMsg.erase(0, 1); + } + } + }); + + std::cout << "Press enter to exit at any time!\n"; + std::cin.get(); + quit = true; +// recvConnSocket->~TCPSocket(); // Forcibly close socket (shouldn't call destructors like this -- make a new function for it! + std::cout << "HERE!"; + receiveThread.join(); +} + +std::string PlayerUser::messageIntoString(std::string _message) +{ + std::string toReturn = "$"; + + toReturn += + playerName + SEPERATOR_TOKEN + + _message; + + return toReturn; +} + +void PlayerUser::decodeMessageString(std::string _messageString) +{ + if (_messageString.length() == 0) + return; + + vector splitMessageString; + + if (_messageString[0] == '$') + _messageString = _messageString.erase(0, 1); + splitMessageString = split(_messageString, SEPERATOR_TOKEN); + + std::string str = "< " + splitMessageString[0] + " > " + splitMessageString[1]; + + std::cout << str; +} + + +void PlayerUser::sendMessage(std::string _message) +{ + if (sendRecvFlag == 0) + { + std::string msg = messageIntoString(_message); + sendSocket->Send(msg.c_str(), msg.length()); + } + else if (sendRecvFlag == 1) + { + std::string msg = messageIntoString(_message); + recvConnSocket->Send(msg.c_str(), msg.length()); + } +} + +void PlayerUser::closeSockets() +{ + if (sendRecvFlag == 0) + { + sendSocket->~TCPSocket(); + } + else if (sendRecvFlag == 1) + { + + recvConnSocket->~TCPSocket(); + recvConnSocket->~TCPSocket(); + } +} + +void PlayerUser::shutdown() +{ + quit = true; +} From a5f1955ea31cd48d7bd5a827cf73da99a1cb4d76 Mon Sep 17 00:00:00 2001 From: charlie sanford Date: Thu, 5 May 2022 00:34:01 -0400 Subject: [PATCH 11/11] works :D --- RoboCat/Inc/PlayerUser.h | 11 ++++++++-- RoboCat/Src/Main.cpp | 41 ++++++++++++++++++++++++++++---------- RoboCat/Src/PlayerUser.cpp | 39 +++++++++++++++++++++++++----------- 3 files changed, 67 insertions(+), 24 deletions(-) diff --git a/RoboCat/Inc/PlayerUser.h b/RoboCat/Inc/PlayerUser.h index 86cf6644..bb6c3dfa 100644 --- a/RoboCat/Inc/PlayerUser.h +++ b/RoboCat/Inc/PlayerUser.h @@ -1,5 +1,5 @@ #pragma once - +#include #include "RoboCatPCH.h" struct PlayerUser @@ -10,14 +10,21 @@ struct PlayerUser int sendRecvFlag = -1; bool quit; float delayTimer = 0; + std::thread chatThread; + std::string CLIENT_SEND_PORT = "1250", CLIENT_RECV_PORT = "2250"; TCPSocketPtr sendSocket, recvSocket, recvConnSocket; +public: PlayerUser(); - PlayerUser(int _pnum); + PlayerUser(int _pnum, std::string username); ~PlayerUser(); + std::string getUsername() { return playerName; }; + + void startTcpThread(bool isServer); + void initTcpClient(std::string sendPort, std::string recvPort); void initTcpServer(std::string listenPort); diff --git a/RoboCat/Src/Main.cpp b/RoboCat/Src/Main.cpp index 73b24000..cb4279e7 100644 --- a/RoboCat/Src/Main.cpp +++ b/RoboCat/Src/Main.cpp @@ -1,11 +1,12 @@ #include "RoboCatPCH.h" - +#include +#include #include "PlayerUser.h" PlayerUser* user; - -std::string CLIENT_SEND_PORT = "1250", CLIENT_RECV_PORT = "2250"; +bool isRunning = true; +//std::thread t; #if _WIN32 @@ -23,25 +24,45 @@ int main(int argc, const char** argv) #endif SocketUtil::StaticInit(); - - CLIENT_SEND_PORT = std::to_string(rand() % 1000 + 8999); - CLIENT_RECV_PORT = std::to_string(8080);// i think this works?? + + std::string username = "charles entertainment cheese"; // Server Code -------------- bool isServer = StringUtils::GetCommandLineArg(1) == "server"; if (isServer) { - user = new PlayerUser(2); - user->initTcpServer(CLIENT_RECV_PORT); + username = "server"; + user = new PlayerUser(2, username); + user->startTcpThread(isServer); } // Client Code -------------- if (!isServer) { - user = new PlayerUser(1); - user->initTcpClient(CLIENT_SEND_PORT, CLIENT_RECV_PORT); + std::cout << "Enter username please:\n"; + std::getline(std::cin, username); + user = new PlayerUser(1, username); + user->startTcpThread(isServer); + } + + std::cout << "Welcome, " + username + "!\nEnter !quit to exit.\n"; + std::cout << "< " + user->getUsername() + " >: "; + while (isRunning) + { + std::string input; + + std::getline(std::cin, input); + + std::cout << "< " + user->getUsername() + " >: "; + std::cout << input << "\n"; + if (input == "!quit" || input == "!Quit" || input == "!QUIT") + isRunning = false; + else + user->sendMessage(input); } + delete user; + SocketUtil::CleanUp(); return 0; diff --git a/RoboCat/Src/PlayerUser.cpp b/RoboCat/Src/PlayerUser.cpp index bc95b078..83b5d9da 100644 --- a/RoboCat/Src/PlayerUser.cpp +++ b/RoboCat/Src/PlayerUser.cpp @@ -22,20 +22,35 @@ PlayerUser::PlayerUser() playerName = ""; } -PlayerUser::PlayerUser(int _pnum) +PlayerUser::PlayerUser(int _pnum, std::string username) { sendSocket = SocketUtil::CreateTCPSocket(SocketAddressFamily::INET); recvSocket = SocketUtil::CreateTCPSocket(SocketAddressFamily::INET); playerNumber = _pnum; - playerName = _pnum == 0 ? "Server" : "Player" + std::to_string(playerNumber); + playerName = username; + //playerName = _pnum == 0 ? "Server" : "Player" + std::to_string(playerNumber); //std::cout << playerName << " client "<< " Subsystems Initialized!\n"; } PlayerUser::~PlayerUser() { closeSockets(); - + chatThread.detach(); +} + +void PlayerUser::startTcpThread(bool isServer) +{ + CLIENT_SEND_PORT = std::to_string(rand() % 1000 + 8999); + CLIENT_RECV_PORT = std::to_string(rand() % 1000 + 8999); + if (isServer) + { + chatThread = std::thread([this] { initTcpServer(CLIENT_RECV_PORT); }); + } + else + { + chatThread = std::thread([this] { initTcpClient(CLIENT_SEND_PORT, CLIENT_RECV_PORT); }); + } } void PlayerUser::initTcpClient(std::string sendPort, std::string recvPort) @@ -105,19 +120,19 @@ void PlayerUser::initTcpClient(std::string sendPort, std::string recvPort) } std::string receivedMsg(buffer, bytesReceived); - LOG("Received message: %s", receivedMsg.c_str()); + //LOG("Received message: %s", receivedMsg.c_str()); if (receivedMsg[0] == '$') // SEND MESSAGE { - decodeMessageString(receivedMsg.erase(0, 1)); // display message + decodeMessageString(receivedMsg); // display message } - else if (receivedMsg[0] == '!') // CHANGE USERNAME + else if (receivedMsg[0] == '~') // CHANGE USERNAME { playerName = receivedMsg.erase(0, 1); } } }); - std::cout << "Press enter to exit at any time!\n"; + //std::cout << "Press enter to exit at any time!\n"; std::cin.get(); quit = true; // recvConnSocket->~TCPSocket(); // Forcibly close socket (shouldn't call destructors like this -- make a new function for it! @@ -203,20 +218,20 @@ void PlayerUser::initTcpServer(std::string listenPort) LOG("Received message from %s: %s", incomingAddress.ToString().c_str(), receivedMsg.c_str()); if (receivedMsg[0] == '$') // SEND MESSAGE { - decodeMessageString(receivedMsg.erase(0, 1)); // display message + decodeMessageString(receivedMsg); // display message } - else if (receivedMsg[0] == '!') // CHANGE USERNAME + else if (receivedMsg[0] == '~') // CHANGE USERNAME { playerName = receivedMsg.erase(0, 1); } } }); - std::cout << "Press enter to exit at any time!\n"; + //std::cout << "Press enter to exit at any time!\n"; std::cin.get(); quit = true; // recvConnSocket->~TCPSocket(); // Forcibly close socket (shouldn't call destructors like this -- make a new function for it! - std::cout << "HERE!"; + //std::cout << "HERE!"; receiveThread.join(); } @@ -242,7 +257,7 @@ void PlayerUser::decodeMessageString(std::string _messageString) _messageString = _messageString.erase(0, 1); splitMessageString = split(_messageString, SEPERATOR_TOKEN); - std::string str = "< " + splitMessageString[0] + " > " + splitMessageString[1]; + std::string str = "< " + splitMessageString[0] + " >:" + splitMessageString[1] + "\n"; std::cout << str; }