-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectionHandler.cpp
More file actions
41 lines (34 loc) · 1.41 KB
/
Copy pathConnectionHandler.cpp
File metadata and controls
41 lines (34 loc) · 1.41 KB
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
//
// Created by Matt on 15/09/2016.
//
#include <iostream>
#include "ConnectionHandler.h"
ConnectionHandler::ConnectionHandler(UDPSocket *socket) : socket(socket) {
connections = std::list<Connection>();
}
int ConnectionHandler::processPacket(Packet *packet) {
std::cout << "Processing packet in the connection handler" << std::endl;
//Determine owner, add it to their queue
UDPAddress sender = packet->getSender();
bool hit = false;
for (std::list<Connection>::iterator ci = connections.begin(); ci != connections.end(); ci++) {
std::cout << ci->getAddress().getIntegerIP() << ", " << sender.getIntegerIP() << std::endl;
if (ci->getAddress().getIntegerIP() == sender.getIntegerIP() &&
ci->getAddress().getPort() == sender.getPort()) {
//We have a match. Connection set contains the packet sender
std::cout << "Sender already connected, forwarding packet to their connection's packet queue: "
<< std::endl;
ci->addPacket(packet);
hit = true;
break;
}
}
if (!hit) {
//Connection doesn't exist in set
//Create connection and forward it to them
std::cout << "New sender. Creating connection" << std::endl;
Connection newConnection = Connection(sender, socket);
newConnection.addPacket(packet);
connections.push_back(newConnection);
}
}