Compare commits

...

5 Commits

3 changed files with 66 additions and 15 deletions

View File

@@ -56,6 +56,9 @@ public:
/* Returns socket identifier */
int getSockFD();
/* Returns whether or not the given socket is connected to a remote address */
bool has_remote_address();
/* This is a pure virtual function (AKA an abstract function). It's purpose
is to be redefined by the children classes (client and server). */
virtual int get_type() = 0;

View File

@@ -4,6 +4,45 @@ add_global_arguments('-std=c++11', language: ['cpp'])
compiler = meson.get_compiler('cpp')
cmake = import('cmake')
# For macOS only - define extra dependencies early
if build_machine.system() == 'darwin'
objc_dep = dependency('objc', required: false)
if not objc_dep.found()
objc_dep = compiler.find_library('objc', required: true)
endif
# Add other macOS frameworks that might be needed
foundation_dep = dependency('Foundation', required: false)
if not foundation_dep.found()
foundation_dep = compiler.find_library('Foundation', required: false)
endif
cocoa_dep = dependency('Cocoa', required: false)
if not cocoa_dep.found()
cocoa_dep = compiler.find_library('Cocoa', required: false)
endif
iokit_dep = dependency('IOKit', required: false)
if not iokit_dep.found()
iokit_dep = compiler.find_library('IOKit', required: false)
endif
extra_deps = [objc_dep]
if foundation_dep.found()
extra_deps += [foundation_dep]
endif
if cocoa_dep.found()
extra_deps += [cocoa_dep]
endif
if iokit_dep.found()
extra_deps += [iokit_dep]
endif
else
extra_deps = []
endif
# For Windows only
ws2_dep = compiler.find_library('ws2_32', required: false)
winmm = compiler.find_library('winmm', required: false)
# Handle raylib dependency based on library type
# if we are building a shared library
if get_option('default_library') == 'shared'
raylib = dependency('raylib', required: false) # Try to find dependency with pkg-config
@@ -17,24 +56,20 @@ if get_option('default_library') == 'shared'
raylib_proj = cmake.subproject('raylib', options: opt_var)
raylib = raylib_proj.dependency('raylib')
endif
else
# For static library (default case)
opt_var = cmake.subproject_options()
opt_var.add_cmake_defines({'BUILD_SHARED_LIBS' : false})
raylib_proj = cmake.subproject('raylib', options: opt_var)
raylib = raylib_proj.dependency('raylib')
endif
# I we are building a static library
if get_option('default_library') == 'static'
raylib_proj = cmake.subproject('raylib')
raylib = raylib_proj.dependency('raylib')
endif
#For Windows only
ws2_dep = compiler.find_library('ws2_32', required: false)
winmm = compiler.find_library('winmm', required: false)
if build_machine.system() == 'windows'
add_project_arguments('-Wl,--subsystem,windows', '-mwindows', language: ['cpp', 'c']) # Prevent opening console when game is run
add_project_arguments('-Wl,--subsystem,windows', '-mwindows', language: ['cpp', 'c']) # Prevent opening console when game is run
endif
executable('pong',
'main.cpp', 'sock.cpp','paddle.cpp', 'ball.cpp', 'numeric_base.cpp', 'connect_code.cpp', 'server.cpp', 'client.cpp', 'check_input.cpp', 'raygui_helpers.cpp', 'display_text.cpp',
'netpong-serialization/serialization.c', 'timer.c', 'easysock.c',
dependencies: [raylib, ws2_dep, winmm]
'main.cpp', 'sock.cpp','paddle.cpp', 'ball.cpp', 'numeric_base.cpp', 'connect_code.cpp', 'server.cpp', 'client.cpp', 'check_input.cpp', 'raygui_helpers.cpp', 'display_text.cpp',
'netpong-serialization/serialization.c', 'timer.c', 'easysock.c',
dependencies: [raylib, ws2_dep, winmm] + extra_deps
)

View File

@@ -37,6 +37,13 @@ Sock::Sock(char protocol, const char* address, int port) {
this->address = std::string(address);
}
/* This method returns whether or not the socket is connected to a remote address */
bool Sock::has_remote_address() {
struct sockaddr_storage addr;
socklen_t len = sizeof(addr);
return getpeername(this->sock_fd, (struct sockaddr*)&addr, &len) == 0;
}
/* This method sends the given data, through the 'other_sockt' variable.. Client
and server classes extend this method, by setting this variable to different values.
This function needs more testing for TCP, as it focuses on UDP right now. */
@@ -48,7 +55,13 @@ void Sock::sendAll(std::string to_send) {
/* For UDP sockets */
if (this->protocol == ES_UDP) {
if (sendto(this->sock_fd, to_send.data(), str_length, 0, (struct sockaddr *)dest, addrlen) == -1) {
int retval;
if (this->has_remote_address()) {
retval = send(this->sock_fd, to_send.data(), str_length, 0);
} else {
retval = sendto(this->sock_fd, to_send.data(), str_length, 0, (struct sockaddr *)dest, addrlen);
}
if (retval == -1) {
throw errno;
}
}