-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
158 lines (139 loc) · 5.82 KB
/
main.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
#include <iostream>
#include <enet/enet.h>
#include "Buffers/PartitionedPacketBuffer.h"
#include "Buffers/PacketBuffer.h"
#include "Buffers/LIFOCircularBuffer.h"
#include "Components/GameLobby.h"
#include "Components/Gateway.h"
#include "Components/Transmitter.h"
#include "Servers/DummyServer.h"
#include <mutex>
mutex consoleMutex;
// ______________________________________________________________________________________________
//void producer(PartitionedPacketBuffer& buffer, size_t partitionIndex, int startId, int endId) {
// for (int i = startId; i <= endId; ++i) {
// ENetPacket* enetPacket = enet_packet_create(&i, sizeof(i), ENET_PACKET_FLAG_RELIABLE);
// auto packet = make_unique<Packet>("Packet " + to_string(i), enetPacket);
//// cout << "Producer put packet with label" << packet->label << endl;
//
// buffer.pushToPartition(partitionIndex, move(packet));
// this_thread::sleep_for(chrono::milliseconds(10));
// }
//}
// ______________________________________________________________________________________________
//void consumer(PartitionedPacketBuffer& buffer, size_t partitionIndex, int numPackets){
// for(int i = 0; i < numPackets; i++){
// auto packet = buffer.popFromPartition(partitionIndex);
// if(packet) {
// lock_guard<std::mutex> guard(consoleMutex);
// cout << "Consumer got packet with label " << packet->label << endl;
// enet_packet_destroy(packet->packet);
// }
// this_thread::sleep_for(chrono::milliseconds(5));
// }
//}
//void producer(PartitionedPacketBuffer& buffer, int numPackets) {
// for (int i = 1; i <= numPackets; ++i) {
// // Simplified packet creation without actual ENetPacket payload for clarity
// auto packet = std::make_unique<Packet>("Packet " + std::to_string(i), nullptr);
// buffer.pushToPartition(0, std::move(packet)); // Assuming partition 0 is pre-allocated
// std::this_thread::sleep_for(std::chrono::milliseconds(1)); // Simulate some work
// }
//}
//
//void consumer(PartitionedPacketBuffer& buffer, int numPackets) {
// int packetsConsumed = 0;
// while (packetsConsumed < numPackets) {
// auto packet = buffer.popFromPartition(0); // Assuming partition 0 is where packets are pushed
// if (packet) {
// std::cout << "Consumer got " << packet->label << std::endl;
// packetsConsumed++;
// }
// }
//}
int main() {
if (enet_initialize () != 0)
{
fprintf (stderr, "An error occurred while initializing ENet.\n");
return EXIT_FAILURE;
}
atexit (enet_deinitialize);
// ______________________________Official Game Server Code______________________________________________
PartitionedPacketBuffer* receiveBuffer = new PartitionedPacketBuffer(100, 500, consoleMutex);
PacketBuffer* outputBuffer = new PacketBuffer();
//create the ENetHost here for Transmitter, and ENetAddress corresponding to the host here
auto gateway = new Gateway("192.168.1.12", 5450, receiveBuffer);
auto transmitter = new Transmitter("192.168.1.12", 5451, outputBuffer);
GameLobby gameLobby(receiveBuffer, outputBuffer, consoleMutex);
gateway->start();
transmitter->start();
gameLobby.start();
while(true){
}
// ______________________________________________________________________________________________
// DummyServer dummyServer("192.168.1.12", 5450);
//
// while(true){
// dummyServer.listenForPacket();
// }
// ______________________________________________________________________________________________
//
// const size_t bufferSize = 100;
// const size_t numPartitions = 3;
// PartitionedPacketBuffer receiveBuffer(numPartitions, bufferSize, consoleMutex);
// PacketBuffer outputBuffer;
//
// GameLobby lobby(receiveBuffer, outputBuffer, consoleMutex);
// auto partitionIndex = lobby.getPartitionIndex();
//
//// auto partition1 = receiveBuffer.allocatePartition().value_or(-1);
//
// if (partitionIndex == -1) {
// std::cerr << "Failed to allocate partition" << std::endl;
// return EXIT_FAILURE;
// }
//
// lobby.start();
//
// // Start producer to simulate packet creation
// std::thread producerThread(producer, std::ref(receiveBuffer), partitionIndex, 1, 50);
// std::this_thread::sleep_for(std::chrono::seconds(5));
// producerThread.join();
// std::this_thread::sleep_for(std::chrono::seconds(5)); // Give some time for processing
// lobby.stop();
//
// return 0;
// ______________________________________________________________________________________________
// auto partition2 = buffer.allocatePartition().value_or(-1);
// if(partition1 == -1 || partition1 == -1) {
// cerr << "Failed to allocate partitions" << endl;
// }
//
// // Start producer and consumer threads for each partition
// std::thread producer1(producer, std::ref(buffer), partition1, 1, 50);
// std::thread consumer1(consumer, std::ref(buffer), partition1, 50);
//
// std::thread producer2(producer, std::ref(buffer), partition2, 51, 100);
// std::thread consumer2(consumer, std::ref(buffer), partition2, 50);
//
// // Wait for all threads to finish
// producer1.join();
// consumer1.join();
// producer2.join();
// consumer2.join();
// const size_t bufferSize = 100; // Large enough to avoid overflow
// const size_t numPackets = 50; // Number of packets to produce/consume
// PartitionedPacketBuffer buffer(1, bufferSize);
// auto partition = buffer.allocatePartition().value_or(-1);
//
// if(partition == -1){
// cout << "Partition creation error" << endl;
// }
//
// thread producerThread(producer, ref(buffer), numPackets);
// thread consumerThread(consumer, ref(buffer), numPackets);
//
// producerThread.join();
// consumerThread.join();
return 0;
}