Skip to content

Commit

Permalink
Mega iffy client update
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanRylko committed Oct 3, 2023
1 parent 18e0cc2 commit 214382c
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 73 deletions.
108 changes: 107 additions & 1 deletion source/Interfaces/NetWorth/client/ClientInterface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,129 @@
#include <vector>
#include <SFML/Network/UdpSocket.hpp>
#include <SFML/Network/Packet.hpp>
#include <memory>

#include "../NetworkInterface.hpp"
#include "../../TrashInterface.hpp"

using namespace sf;

namespace cse491 {
namespace netWorth{
class ClientInterface : public NetworkingInterface {
private:
std::unique_ptr<TrashInterface> mTrash; /// Display interface

sf::IpAddress mIp; /// Destination IP address
sf::UdpSocket mSocket; /// UDP socket for sending and receiving
unsigned short mPort; /// Destination port

protected:


public:
ClientInterface(size_t id, const std::string & name) : NetworkingInterface(id, name) { }
/**
* ClientInterface constructor (NetworkingInterface superclass)
* @param id Interface identifier
* @param name Interface name
* @param ip_string String for destination IP address, make into IpAddress object
* @param port Destination port number
*/
ClientInterface(size_t id, const std::string & name,
const std::string & ip_string,
unsigned short port) : NetworkingInterface(id, name) {
mIp = sf::IpAddress(ip_string);
mPort = port;
}

/**
* Default destructor
*/
~ClientInterface() = default;

/**
* Establish connection with server
* @return True if successful, false if error
*/
bool EstablishConnection() {
// send ping to server
std::string message = "Ping!";
if (mSocket.send(message.c_str(), message.size() + 1, mIp, mPort) != Socket::Status::Done) {
std::cout << "Could not connect to " << mIp << " at port " << mPort << std::endl;
return false;
}

// receive pong from server
size_t received;
char data[100]; // TODO: Make packets instead of C strings
if (mSocket.receive(data, 100, received, mIp, mPort) != sf::Socket::Done)
{
std::cout << "Failure to receive" << std::endl;
return false;
}
std::cout << data << std::endl;

return true;
}

/**
* Game loop
*/
void Loop() {
char input;
bool valid_input = false;
bool wait_for_input = true;
sf::Packet recv_pkt;
std::string recv_str;

// receive initial map
if (mSocket.receive(recv_pkt, mIp, mPort) != sf::Socket::Done)
{
std::cout << "Failure to receive" << std::endl;
return;
}

recv_pkt >> recv_str;
std::cout << recv_str;

while (input != 'q')
{
do {
std::cin >> input;
} while (!std::cin && wait_for_input);

switch (input) {
case 'w': case 'W': valid_input = true; break;
case 'a': case 'A': valid_input = true; break;
case 's': case 'S': valid_input = true; break;
case 'd': case 'D': valid_input = true; break;
case 'q': case 'Q': valid_input = true; break;
default: valid_input = false;
}

// If we waited for input, but don't understand it, notify the user.
if (wait_for_input && !valid_input) {
std::cout << "Unknown key '" << input << "'." << std::endl;
} else {
if (mSocket.send(&input, 1, mIp, mPort) != Socket::Status::Done) {
std::cout << "Could not connect to " << mIp << " at port " << mPort << std::endl;
return;
}

if (mSocket.receive(recv_pkt, mIp, mPort) != sf::Socket::Done)
{
std::cout << "Failure to receive" << std::endl;
return;
}

recv_pkt >> recv_str;
std::cout << recv_str;
}

valid_input = false;
}

}
}; //End of ClientInterface
}// End of namespace NetWorth

Expand Down
76 changes: 4 additions & 72 deletions source/Interfaces/NetWorth/client/clientmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "../../../Agents/PacingAgent.hpp"
#include "../../../Worlds/MazeWorld.hpp"
#include "networkingagent.hpp"
#include "ClientInterface.hpp"

int main(int argc, char *argv[])
{
Expand All @@ -17,81 +18,12 @@ int main(int argc, char *argv[])
return 1;
}

// create socket, get IP and port from args
sf::UdpSocket socket;
std::string ip_string(argv[1]);
sf::IpAddress server_ip(ip_string);
unsigned short port = stoi(std::string(argv[2]));
cse491::netWorth::ClientInterface interface(1, "client", ip_string, port);

// send ping to server
std::string message = "Ping!";
if (socket.send(message.c_str(), message.size() + 1, server_ip, port) != Socket::Status::Done) {
std::cout << "Could not connect to " << ip_string << " at port " << port << std::endl;
return 1;
}

// receive pong from server
size_t received;
char data[100];
if (socket.receive(data, 100, received, server_ip, port) != sf::Socket::Done)
{
std::cout << "Failure to receive" << std::endl;
return 1;
}
std::cout << data << std::endl;

char input;
bool valid_input = false;
bool wait_for_input = true;
sf::Packet recv_pkt;
std::string recv_str;

// receive initial map
if (socket.receive(recv_pkt, server_ip, port) != sf::Socket::Done)
{
std::cout << "Failure to receive" << std::endl;
return 1;
}

recv_pkt >> recv_str;
std::cout << recv_str;

while (input != 'q')
{
do {
std::cin >> input;
} while (!std::cin && wait_for_input);

switch (input) {
case 'w': case 'W': valid_input = true; break;
case 'a': case 'A': valid_input = true; break;
case 's': case 'S': valid_input = true; break;
case 'd': case 'D': valid_input = true; break;
case 'q': case 'Q': valid_input = true; break;
default: valid_input = false;
}

// If we waited for input, but don't understand it, notify the user.
if (wait_for_input && !valid_input) {
std::cout << "Unknown key '" << input << "'." << std::endl;
} else {
if (socket.send(&input, 1, server_ip, port) != Socket::Status::Done) {
std::cout << "Could not connect to " << ip_string << " at port " << port << std::endl;
return 1;
}

if (socket.receive(recv_pkt, server_ip, port) != sf::Socket::Done)
{
std::cout << "Failure to receive" << std::endl;
return 1;
}

recv_pkt >> recv_str;
std::cout << recv_str;
}

valid_input = false;
}
if (!interface.EstablishConnection()) return 1;
interface.Loop();

return 0;
}

0 comments on commit 214382c

Please sign in to comment.