| Line |
Branch |
Exec |
Source |
| 1 |
|
|
/* |
| 2 |
|
|
** EPITECH PROJECT, 2024 |
| 3 |
|
|
** zappy |
| 4 |
|
|
** File description: |
| 5 |
|
|
** ServerConnect |
| 6 |
|
|
*/ |
| 7 |
|
|
|
| 8 |
|
|
#include <cstring> |
| 9 |
|
|
#include <iostream> |
| 10 |
|
|
#include <sys/socket.h> |
| 11 |
|
|
#include <netinet/in.h> |
| 12 |
|
|
#include <arpa/inet.h> |
| 13 |
|
|
#include <unistd.h> |
| 14 |
|
|
#include <vector> |
| 15 |
|
|
|
| 16 |
|
|
#include "ServerConnect.hpp" |
| 17 |
|
|
#include "../utils/GuiException.hpp" |
| 18 |
|
|
#include "../utils/Debug.hpp" |
| 19 |
|
|
|
| 20 |
|
3 |
void ServerConnect::connectToServer(int port, const char *ip) |
| 21 |
|
|
{ |
| 22 |
|
|
debug_print << "Connecting to server" << std::endl; |
| 23 |
|
3 |
this->fd = socket(AF_INET, SOCK_STREAM, 0); |
| 24 |
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
|
3 |
if (this->fd < 0) { |
| 25 |
2/4
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
|
2 |
throw guiException("Failed to create a socket"); |
| 26 |
|
|
} |
| 27 |
|
|
|
| 28 |
|
2 |
select.addFd(this->fd); |
| 29 |
|
|
struct sockaddr_in server_addr; |
| 30 |
|
|
memset(&server_addr, 0, sizeof(server_addr)); |
| 31 |
|
2 |
server_addr.sin_family = AF_INET; |
| 32 |
|
2 |
server_addr.sin_port = htons(port); |
| 33 |
|
|
|
| 34 |
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
|
2 |
if (inet_pton(AF_INET, ip, &server_addr.sin_addr) <= 0) { |
| 35 |
2/4
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
|
2 |
throw guiException("Invalid address"); |
| 36 |
|
|
} |
| 37 |
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 |
if (connect(this->fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) { |
| 38 |
2/4
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
|
2 |
throw guiException("Failed to connect to the server"); |
| 39 |
|
|
} |
| 40 |
|
✗ |
connectionState = CONNECTED; |
| 41 |
|
|
debug_print << "Connected to server" << std::endl; |
| 42 |
|
✗ |
} |
| 43 |
|
|
|
| 44 |
|
✗ |
std::string ServerConnect::readFromServer() |
| 45 |
|
|
{ |
| 46 |
|
|
std::string message; |
| 47 |
|
✗ |
std::vector<char> buffer(1024); |
| 48 |
|
|
int valread; |
| 49 |
|
|
|
| 50 |
|
✗ |
if (connectionState == CONNECTED && select.select() > 0 && select.isSet(fd)) { |
| 51 |
|
✗ |
if (recv(this->fd, buffer.data(), buffer.size(), MSG_PEEK) == 0) { |
| 52 |
|
✗ |
std::cerr << "Server is down" << std::endl; |
| 53 |
|
✗ |
connectionState = SERVERDOWN; |
| 54 |
|
✗ |
return message; |
| 55 |
|
|
} |
| 56 |
|
✗ |
while ((valread = read(this->fd, buffer.data(), buffer.size())) > 0) { |
| 57 |
|
✗ |
message += std::string(buffer.data(), valread); |
| 58 |
|
✗ |
if (message.back() == '\n' && valread != static_cast<int>(buffer.size())) |
| 59 |
|
|
break; |
| 60 |
|
|
} |
| 61 |
|
✗ |
if (valread < 0) |
| 62 |
|
✗ |
throw guiException("Failed to read from the server"); |
| 63 |
|
|
} |
| 64 |
|
|
return message; |
| 65 |
|
✗ |
} |
| 66 |
|
|
|
| 67 |
|
4 |
void ServerConnect::sendToServer(std::string message) |
| 68 |
|
|
{ |
| 69 |
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
|
4 |
if (write(this->fd, message.c_str(), message.size()) < 0) { |
| 70 |
2/4
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
|
8 |
throw guiException("Failed to send message to the server"); |
| 71 |
|
|
} |
| 72 |
|
✗ |
std::cout << "Sent: " << message << std::endl; |
| 73 |
|
✗ |
} |
| 74 |
|
|
|
| 75 |
|
✗ |
bool ServerConnect::disconectFromServer() |
| 76 |
|
|
{ |
| 77 |
|
✗ |
if (this->fd >= 0) { |
| 78 |
|
✗ |
close(this->fd); |
| 79 |
|
✗ |
this->fd = -1; |
| 80 |
|
✗ |
connectionState = NOTCONNECTED; |
| 81 |
|
✗ |
return true; |
| 82 |
|
|
} |
| 83 |
|
|
return false; |
| 84 |
|
|
} |
| 85 |
|
|
|