Skip to content

Commit 12add57

Browse files
feat: add network utils
1 parent 270550c commit 12add57

3 files changed

Lines changed: 16 additions & 6 deletions

File tree

src/constants/network_constants.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#pragma once
2+
3+
namespace Constants {
4+
5+
inline constexpr int PORT = 8080;
6+
inline constexpr int MAX_BUFFER_SIZE = 1024;
7+
8+
} // namespace Constants

src/core/game.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <string>
1313

1414
#include "constants/game_constants.h"
15+
#include "constants/network_constants.h"
1516
#include "core/window.h"
1617
#include "entities/actions/npc_movement_handler.h"
1718
#include "entities/actions/player_input_handler.h"
@@ -129,8 +130,7 @@ void Game::Update() {
129130
client_->Send(msg.c_str(), msg.size());
130131

131132
// TODO(ramirezfernando): Receive updated other player state from server.
132-
constexpr size_t maxBufferSize = 1024;
133-
std::array<char, maxBufferSize> buf{};
133+
std::array<char, Constants::MAX_BUFFER_SIZE> buf{};
134134
const ssize_t n = client_->Receive(buf.data(), buf.size());
135135
if (n > 0) {
136136
buf.at(static_cast<size_t>(n)) = '\0';

src/server.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
#include <memory>
1010
#include <string>
1111

12+
#include "constants/network_constants.h"
13+
1214
int main() {
13-
auto server = std::unique_ptr<Server>(Server::Create("8080"));
15+
auto server =
16+
std::unique_ptr<Server>(Server::Create(std::to_string(Constants::PORT)));
1417

15-
constexpr size_t maxBufferSize = 1024;
16-
std::array<char, maxBufferSize> buf{};
18+
std::array<char, Constants::MAX_BUFFER_SIZE> buf{};
1719
sockaddr_storage peer{};
18-
socklen_t peer_len = sizeof(peer); // Will be updated by ReceiveFrom.
20+
socklen_t peer_len = sizeof(peer);
1921

2022
while (true) {
2123
const ssize_t n =

0 commit comments

Comments
 (0)