-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.cpp
102 lines (91 loc) · 2.62 KB
/
server.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "server.h"
Server::Server() : m_listenThread(&Server::Listen, this) //, m_broadcastThread(&Server::Broadcast, this), get_positionThread(&Server::ReceivePosition, this)
{
s_socket.bind(port);
}
void Server::setPlayers(int l_players)
{
m_players = l_players;
}
void Server::ReceiveConnection()
{
cout << "Listening: ";
sf::IpAddress l_Ip;
unsigned short l_port;
sf::Socket::Status value;
value = s_socket.receive(m_buffer, sizeof(m_buffer), s_received, l_Ip, l_port);
std::cout << "from ip : " << l_Ip << "from port : " << l_port << endl;
if (s_received > 0)
{
for (int i = 0; i < m_playerno; i++)
{
if (players[i].getIp() == l_Ip && players[i].getPort() == l_port)
{
std::cout << "The port and Ip address you chose already belongs to some other players." << endl;
break;
}
}
m_computer.setIp(l_Ip);
m_computer.setPort(l_port);
players.push_back(m_computer);
cout << "Received connection from Ip: " << l_Ip << " Port: " << l_port << endl;
m_playerno++;
ConfirmConnection(l_Ip, l_port);
}
}
void Server::ConfirmConnection(sf::IpAddress l_ip, unsigned short l_port)
{
sf::Packet packet;
std::string confirmation = "Connection Received by Server. ";
packet << confirmation << m_players << m_playerno;
s_socket.send(packet, l_ip, l_port);
}
void Server::SendPlaySignal()
{
int playnumber = -2;
sf::Packet packet;
packet << playnumber;
for (int i = 0; i < m_playerno; i++)
{
s_socket.send(packet, players[i].getIp(), players[i].getPort());
}
}
void Server::Listen()
{
sf::IpAddress tempip;
unsigned short tempport;
while (1)
{
sf::Packet packet;
s_socket.receive(packet, tempip, tempport);
if (s_received > 0)
{
SendtoOthers(tempip, tempport, packet);
}
}
}
void Server::SendtoOthers(sf::IpAddress l_ip, unsigned short l_port, sf::Packet packet)
{
sf::IpAddress ip;
unsigned short port;
float x, y;
int playerid;
packet >> playerid >> x >> y;
for (int i = 0; i < m_playerno; i++)
{
ip = players[i].getIp();
port = players[i].getPort();
if (ip == l_ip && port == l_port)
{
continue;
}
// std::cout << "Received position of PLAYER " << playerid << playerid << " (" << x << ", " << y << ")" << std::endl;
s_socket.send(packet, ip, port);
}
}
void Server::Thread()
{
m_listenThread.launch();
// m_broadcastThread.launch();
// get_positionThread.launch();
}