This repository was archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsfml-network.cpp
199 lines (162 loc) · 5.25 KB
/
sfml-network.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <iostream>
#include <list>
#include <string>
#include <SFML/Network.hpp>
enum class ProtocolType {ptUdp, ptTcp};
enum class UserType {utServer, utClient};
class GameSocket
{
public:
ProtocolType protocolType;
UserType userType;
sf::UdpSocket socketUdp;
sf::TcpSocket socketTcp;
sf::TcpListener listener;
unsigned short port;
bool enabled;
GameSocket(ProtocolType protocolType, UserType userType, unsigned short port)
{
this->protocolType = protocolType;
this->port = port;
this->enabled = false;
this->userType = userType;
switch (this->protocolType)
{
case ProtocolType::ptUdp:
{
this->enabled = (this->socketUdp.bind(this->port) == sf::Socket::Done);
this->socketUdp.setBlocking(false);
break;
}
case ProtocolType::ptTcp:
{
this->enabled = this->userType == UserType::utServer ? this->listener.listen(this->port) == sf::Socket::Done : false;
this->listener.setBlocking(false);
this->socketTcp.setBlocking(false);
break;
}
}
}
bool sendPackage(sf::Packet &packet, sf::IpAddress &ipAddress, unsigned short portSender)
{
switch (this->protocolType)
{
case ProtocolType::ptUdp:
{
this->socketUdp.send(packet, ipAddress, portSender);
break;
}
case ProtocolType::ptTcp:
{
if (!this->enabled && this->userType == UserType::utClient)
this->enabled = this->socketTcp.connect(ipAddress, portSender) == sf::Socket::Status::Done;
this->socketTcp.send(packet);
break;
}
}
return true;
}
void receivePackage(sf::Packet &packetReceived, sf::IpAddress &ipAddressSender, unsigned short portSender)
{
switch (this->protocolType)
{
case ProtocolType::ptUdp:
{
this->socketUdp.receive(packetReceived, ipAddressSender, portSender);
break;
}
case ProtocolType::ptTcp:
{
if (this->userType == UserType::utClient || this->listener.accept(this->socketTcp))
this->socketTcp.receive(packetReceived);
break;
}
}
}
};
int main()
{
unsigned short clientPort = 54122, serverPort = 54123;
sf::Clock clock;
sf::Time timer = clock.getElapsedTime();
std::cout << "[t]cp or [u]dp? ";
std::string protocol = "";
std::cin >> protocol;
if (protocol != "t" && protocol != "u")
return 2;
std::cout << "Are you a server [y]? ";
std::string type = "";
std::cin >> type;
GameSocket gameSocket(protocol == "t" ? ProtocolType::ptTcp : ProtocolType::ptUdp,
type == "y" ? UserType::utServer : UserType::utClient,
type == "y" ? serverPort : clientPort);
if (gameSocket.userType == UserType::utClient)
{
std::cout << "Gotta a name? ";
std::string name = "";
std::cin >> name;
std::cout << "Server address? ";
std::string serverAddressValue = "";
std::cin >> serverAddressValue;
sf::IpAddress serverAddress(serverAddressValue);
float duration = 15.f, interval = 0.f;
clock.restart();
while (duration > 0.f)
{
sf::Time timer = clock.getElapsedTime();
clock.restart();
float time = timer.asSeconds();
duration -= time;
sf::Packet packetReceived;
sf::IpAddress sender;
unsigned short receivePort = 54122;
gameSocket.receivePackage(packetReceived, sender, receivePort);
std::string senderName = "", messageReceived = "";
packetReceived >> senderName >> messageReceived;
if (messageReceived != "")
std::cout << "Got: " << senderName << " -> " << messageReceived << std::endl;
interval += time;
if (interval >= 1.f)
{
interval = 0.f;
sf::Packet packetSend;
packetSend << name << "Sent hi!";
gameSocket.sendPackage(packetSend, serverAddress, serverPort);
}
}
}
else
{
std::list<sf::IpAddress> clients;
clients.clear();
float duration = 180.f, interval = 0.f;
clock.restart();
std::cout << "server ON";
while (duration > 0.f)
{
sf::Time timer = clock.getElapsedTime();
clock.restart();
float time = timer.asSeconds();
duration -= time;
sf::Packet packetReceived;
sf::IpAddress sender;
unsigned short receivePort = 54123;
gameSocket.receivePackage(packetReceived, sender, receivePort);
int senderPort = 0;
std::string senderName = "", messageReceived = "";
packetReceived >> senderName >> messageReceived;
if (messageReceived != "")
{
auto senderIterator = std::find_if(clients.begin(), clients.end(), [sender](const sf::IpAddress senderIndex) {return senderIndex == sender; });
if (clients.end() == senderIterator)
clients.emplace_back(sender);
std::cout << "Got: " << senderName << " -> " << messageReceived << std::endl;
sf::Packet packetSend;
packetSend << senderName << messageReceived;
for (auto& senderIndex : clients)
gameSocket.sendPackage(packetSend, senderIndex, clientPort);
}
}
}
return 0;
}