Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ project(FPS-Game)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_VERBOSE_MAKEFILE ON)



set(RAYLIB_VERSION 5.0)
find_package(raylib ${RAYLIB_VERSION} QUIET) # QUIET or REQUIRED
if (NOT raylib_FOUND) # If there's none, fetch and build raylib
Expand All @@ -21,23 +23,28 @@ if (NOT raylib_FOUND) # If there's none, fetch and build raylib
add_subdirectory(${raylib_SOURCE_DIR} ${raylib_BINARY_DIR})
endif()
endif()
include_directories("C:/Users/Joseph/Desktop/Intellectuals/FPS-Game/DevEnv/flatbuffers/include")
include_directories("C:/Users/Joseph/Desktop/Intellectuals/FPS-Game/DevEnv/Enet Headers/include")
link_directories(C:/Users/Joseph/Desktop/Intellectuals/FPS-Game/Dev Libs/"Dev_Libs")

find_library(ENET_LIBRARY NAMES enet.a PATHS "Dev_Libs")
if(NOT ENET_LIBRARY)
MESSAGE(FATAL_ERROR "ENet library not found")
endif()
#include_directories("C:/Users/Joseph/Desktop/Intellectuals/FPS-Game/DevEnv/flatbuffers/include")
include_directories("/Users/anshulgowda/Documents/CODE/Odyssey/FPS-Game/DevEnv/flatbuffers/include")
include_directories(/usr/local/include)
#include_directories("C:/Users/Joseph/Desktop/Intellectuals/FPS-Game/DevEnv/Enet Headers/include")
#link_directories(C:/Users/Joseph/Desktop/Intellectuals/FPS-Game/Dev Libs/"Dev_Libs") #contains enet.lib, I don't need that
link_directories(/usr/local/lib)
link_directories(/usr/local/bin)

message(STATUS "ENet library found: ${ENET_LIBRARY}")
#find_library(ENET_LIBRARY NAMES enet.a PATHS "Dev_Libs")
#if(NOT ENET_LIBRARY)
# MESSAGE(FATAL_ERROR "ENet library not found")
#endif()
#
#message(STATUS "ENet library found: ${ENET_LIBRARY}")



add_executable(FPS-Game main.cpp Player.cpp Bullet.cpp game_state_generated.h game_state.fbs Transmitter.cpp Transmitter.h PacketBuffer.cpp PacketBuffer.h DummyClient.cpp DummyClient.h Gateway.cpp Gateway.h BufferHandler.h)

target_link_libraries(${PROJECT_NAME} raylib)
target_link_libraries(${PROJECT_NAME} ${ENET_LIBRARY} ws2_32 winmm)
#target_link_libraries(${PROJECT_NAME} ${ENET_LIBRARY} ws2_32 winmm)
target_link_libraries(${PROJECT_NAME} enet)

# Web Configurations
if (${PLATFORM} STREQUAL "Web")
Expand Down
1 change: 1 addition & 0 deletions Gateway.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ void Gateway::networkLoop() {
memcpy(bufferCopy.get(), event.packet->data, event.packet->dataLength);
unique_ptr<BufferHandler> packetBufferHandler = std::make_unique<BufferHandler>(std::move(bufferCopy), event.packet->dataLength);
receiveBuffer.addBufferHandler(std::move(packetBufferHandler));
break;
}
case ENET_EVENT_TYPE_NONE:
cout << "nothing happened" << endl;
Expand Down
67 changes: 36 additions & 31 deletions PacketBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ void PacketBuffer::addPacket(unique_ptr<ENetPacket> packet) {
}
packetQueue.push(std::move(packet));
numberOfPackets++;
// {
// std::lock_guard<std::mutex> guard(consoleMutex);
{
std::lock_guard<std::mutex> guard(consoleMutex);
// printf("Packet received in Output Buffer.\n\ttick = %zu\n", GetOD_Packet(packetQueue.back()->data)->tick()->tick_number());
//
// }
printf("Packet received in Output Buffer.\n\ttick = %zu\n", GetOD_Packet(packetQueue.back()->data)->client_tick()->tick_number());

}
}

buffer_Condition.notify_all();
Expand All @@ -47,11 +48,11 @@ void PacketBuffer::addBufferHandler(unique_ptr<BufferHandler> packet) {
}
packetQueueIn.push(std::move(packet));
numberOfPackets++;
// {
// std::lock_guard<std::mutex> guard(consoleMutex);
// printf("Packet received in Input Buffer.\n\ttick = %zu\n", packetQueueIn.back()->getPacketView()->tick()->tick_number());
//
// }
{
std::lock_guard<std::mutex> guard(consoleMutex);
printf("Packet received in Input Buffer.\n\ttick = %zu\n", packetQueueIn.back()->getPacketView()->client_tick()->tick_number());

}
}

buffer_Condition.notify_all();
Expand All @@ -77,11 +78,11 @@ vector<unique_ptr<BufferHandler>> PacketBuffer::removePacketInstant() {
packetList.reserve(packetQueueIn.size());
while(!packetQueueIn.empty()){
auto packet = std::move(packetQueueIn.front()); // pull out the packet
// {
// std::lock_guard<std::mutex> guard(consoleMutex);
// cout <<"Number of packets(pulling from buffer in transmitter): " <<numberOfPackets << endl;
// cout <<"Tick no. in transmitter: " <<packet->getPacketView()->tick()->tick_number() << endl;
// }
{
std::lock_guard<std::mutex> guard(consoleMutex);
cout <<"Number of packets(pulling from buffer in transmitter): " <<numberOfPackets << endl;
cout <<"Tick no. in transmitter: " <<packet->getPacketView()->client_tick()->tick_number() << endl;
}
packetQueueIn.pop();
packetList.push_back(std::move(packet));
numberOfPackets--;
Expand All @@ -99,23 +100,23 @@ vector<unique_ptr<BufferHandler>> PacketBuffer::removePacketInstant() {
*/
vector<unique_ptr<ENetPacket>> PacketBuffer::removePacketWait() {
unique_lock<mutex> lock(bufferMutex); // lock the buffer
// {
// std::lock_guard<std::mutex> guard(consoleMutex);
// cout << "transmitter is attempting to pull from buffer" << endl;
//
//
// }
{
std::lock_guard<std::mutex> guard(consoleMutex);
cout << "transmitter is attempting to pull from buffer" << endl;


}
// enter wait state and unlock lock until the packetQueue is notified, then check if it satisfies the lambda function if not
// go back to waiting. This approach prevents random wakeups as even if it is woken up randomly it will not proceed unless it
// can
buffer_Condition.wait(lock, [this] {
return (!packetQueue.empty() || shutdownFlag.load());
});
// {
// std::lock_guard<std::mutex> guard(consoleMutex);
// cout << "transmitter got the green light" << endl;
//
// }
{
std::lock_guard<std::mutex> guard(consoleMutex);
cout << "transmitter got the green light" << endl;

}
if(packetQueue.empty() || shutdownFlag.load()){
{
std::lock_guard<std::mutex> guard(consoleMutex);
Expand All @@ -131,11 +132,11 @@ vector<unique_ptr<ENetPacket>> PacketBuffer::removePacketWait() {

while(!packetQueue.empty()){
auto packet = std::move(packetQueue.front()); // pull out the packet
// {
// std::lock_guard<std::mutex> guard(consoleMutex);
// cout <<"Number of packets(pulling from buffer in transmitter): " <<numberOfPackets << endl;
// cout <<"Tick no. in transmitter: " <<GetOD_Packet(packet->data)->tick()->tick_number() << endl;
// }
{
std::lock_guard<std::mutex> guard(consoleMutex);
cout <<"Number of packets(pulling from buffer in transmitter): " <<numberOfPackets << endl;
cout <<"Tick no. in transmitter: " <<GetOD_Packet(packet->data)->client_tick()->tick_number() << endl;
}
packetQueue.pop();
packetList.push_back(std::move(packet));
numberOfPackets--;
Expand All @@ -153,4 +154,8 @@ void PacketBuffer::notifyAll() {
void PacketBuffer::shutdown() {
shutdownFlag.store(true);
buffer_Condition.notify_all(); // wake all threads waiting of this shutdown command
}
}

int PacketBuffer::getNumberOfPackets() const {
return numberOfPackets;
}
1 change: 1 addition & 0 deletions PacketBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class PacketBuffer {
vector<unique_ptr<BufferHandler>> removePacketInstant();
// wake all waiting threads
void notifyAll();
int getNumberOfPackets() const;

// TODO: get buffer stats, (% full, service rate, how often full, etc)

Expand Down
19 changes: 15 additions & 4 deletions Transmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,34 @@ void Transmitter::start(){
transmitThread = thread(&Transmitter::transmitLoop,this);
}
void Transmitter::transmitLoop(){
<<<<<<< HEAD
{
std::lock_guard<std::mutex> guard(consoleMutex);
cout << "in transmit loop" << endl;
}
connect("172.17.8.211",5450);
=======
// {
// std::lock_guard<std::mutex> guard(consoleMutex);
// cout << "in transmit loop" << endl;
// }
connect("192.168.56.1",5450);
>>>>>>> 550e05ef4bdf2e0a8fb9d4f5a36438ce19f2141f
while(!shutdownFlag.load()){

auto packetList = transmitBuffer.removePacketWait();
if(packetList.empty()) {
<<<<<<< HEAD
{
std::lock_guard<std::mutex> guard(consoleMutex);
cout << "no packets to pull" << endl;
}
=======
// {
// std::lock_guard<std::mutex> guard(consoleMutex);
// cout << "no packtes to pull" << endl;
// }
>>>>>>> 550e05ef4bdf2e0a8fb9d4f5a36438ce19f2141f
continue;
}else{
int packetListSize = packetList.size();
Expand Down Expand Up @@ -123,10 +138,6 @@ void Transmitter::transmitPacket(unique_ptr<ENetPacket> packet) {
//
// }
std::move(packet);




}

PacketBuffer &Transmitter::getPacketBuffer() {
Expand Down
Loading