Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linux/Unix support, continued from PR #37 #52

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
17 changes: 15 additions & 2 deletions src/CMakeSources.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,22 @@ set(GGPO_LIB_SRC_NOFILTER
)

if(UNIX)
if(APPLE)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fdeclspec")
endif(APPLE)

set(GGPO_LIB_SRC_NOFILTER
${GGPO_LIB_SRC_NOFILTER}
"lib/ggpo/platform_linux.cpp"
"lib/ggpo/platform_unix.h"
"lib/ggpo/platform_unix.cpp"
"lib/ggpo/pevents.h"
"lib/ggpo/pevents.cpp"
)
else(WIN32)
set(GGPO_LIB_SRC_NOFILTER
${GGPO_LIB_SRC_NOFILTER}
"lib/ggpo/platform_windows.h"
"lib/ggpo/platform_windows.cpp"
)
endif()

Expand Down Expand Up @@ -71,4 +84,4 @@ set(GGPO_LIB_SRC
${GGPO_LIB_INC_BACKENDS}
${GGPO_LIB_SRC_BACKENDS}
${GGPO_PUBLIC_INC}
)
)
2 changes: 1 addition & 1 deletion src/apps/vectorwar/gdi_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ GDIRenderer::DrawConnectState(HDC hdc, Ship &ship, PlayerConnectionInfo &info, C

case Disconnecting:
sprintf(status, "Waiting for player...");
progress = (timeGetTime() - info.disconnect_start) * 100 / info.disconnect_timeout;
progress = (GetCurrentTimeMS() - info.disconnect_start) * 100 / info.disconnect_timeout;
break;
}

Expand Down
6 changes: 4 additions & 2 deletions src/apps/vectorwar/ggpo_perfmon.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include <windows.h>
#include <stdio.h>

#include "resource.h"
#include "ggponet.h"
#include "ggpo_perfmon.h"
#include "vectorwar.h"

#define MAX_GRAPH_SIZE 4096
#define MAX_FAIRNESS 20
Expand Down Expand Up @@ -114,7 +116,7 @@ ggpo_perfmon_dlgproc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
case WM_INITDIALOG:
{
char pid[64];
sprintf(pid, "%d", GetCurrentProcessId());
sprintf(pid, "%d", GetProcessID());
SetWindowTextA(GetDlgItem(hwndDlg, IDC_PID), pid);
return TRUE;
}
Expand Down Expand Up @@ -210,7 +212,7 @@ ggpoutil_perfmon_update(GGPOSession *ggpo, GGPOPlayerHandle players[], int num_p
}
}

int now = timeGetTime();
uint32_t now = GetCurrentTimeMS();
if (_dialog) {
InvalidateRect(GetDlgItem(_dialog, IDC_FAIRNESS_GRAPH), NULL, FALSE);
InvalidateRect(GetDlgItem(_dialog, IDC_NETWORK_GRAPH), NULL, FALSE);
Expand Down
10 changes: 5 additions & 5 deletions src/apps/vectorwar/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ CreateMainWindow(HINSTANCE hInstance)
int width = 640, height = 480;
WCHAR titlebuf[128];

wsprintf(titlebuf, L"(pid:%d) ggpo sdk sample: vector war", GetCurrentProcessId());
wsprintf(titlebuf, L"(pid:%d) ggpo sdk sample: vector war", GetProcessID());
wndclass.cbSize = sizeof(wndclass);
wndclass.lpfnWndProc = MainWindowProc;
wndclass.lpszClassName = L"vwwnd";
Expand All @@ -69,9 +69,9 @@ void
RunMainLoop(HWND hwnd)
{
MSG msg = { 0 };
int start, next, now;
uint32_t start, next, now;

start = next = now = timeGetTime();
start = next = now = GetCurrentTimeMS();
while(1) {
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
Expand All @@ -80,7 +80,7 @@ RunMainLoop(HWND hwnd)
return;
}
}
now = timeGetTime();
now = GetCurrentTimeMS();
VectorWar_Idle(max(0, next - now - 1));
if (now >= next) {
VectorWar_RunFrame(hwnd);
Expand Down Expand Up @@ -184,4 +184,4 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
WSACleanup();
DestroyWindow(hwnd);
return 0;
}
}
11 changes: 9 additions & 2 deletions src/apps/vectorwar/vectorwar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#include <gl/glu.h>
#include <math.h>
#include <stdio.h>

#include <chrono>
#include <thread>

#include "gdi_renderer.h"
#include "vectorwar.h"
#include "ggpo_perfmon.h"
Expand Down Expand Up @@ -82,7 +86,7 @@ vw_on_event_callback(GGPOEvent *info)
break;
case GGPO_EVENTCODE_CONNECTION_INTERRUPTED:
ngs.SetDisconnectTimeout(info->u.connection_interrupted.player,
timeGetTime(),
GetCurrentTimeMS(),
info->u.connection_interrupted.disconnect_timeout);
break;
case GGPO_EVENTCODE_CONNECTION_RESUMED:
Expand All @@ -92,7 +96,7 @@ vw_on_event_callback(GGPOEvent *info)
ngs.SetConnectState(info->u.disconnected.player, Disconnected);
break;
case GGPO_EVENTCODE_TIMESYNC:
Sleep(1000 * info->u.timesync.frames_ahead / 60);
std::this_thread::sleep_for(std::chrono::milliseconds(1000 * info->u.timesync.frames_ahead / 60));
break;
}
return true;
Expand Down Expand Up @@ -449,3 +453,6 @@ VectorWar_Exit()
}
delete renderer;
}

DWORD GetProcessID() { return GetCurrentProcessId(); }
uint32_t GetCurrentTimeMS() { return timeGetTime(); }
6 changes: 6 additions & 0 deletions src/apps/vectorwar/vectorwar.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef _VECTORWAR_H
#define _VECTORWAR_H

#include <stdint.h>

#include "ggponet.h"

/*
Expand Down Expand Up @@ -28,6 +30,10 @@ void VectorWar_Idle(int time);
void VectorWar_DisconnectPlayer(int player);
void VectorWar_Exit();

// Helper functions
DWORD GetProcessID();
uint32_t GetCurrentTimeMS();

#define ARRAY_SIZE(n) (sizeof(n) / sizeof(n[0]))
#define FRAME_DELAY 2

Expand Down
15 changes: 15 additions & 0 deletions src/include/ggponet.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@
#ifndef _GGPONET_H_
#define _GGPONET_H_

#if defined(_MSC_VER)
// Microsoft
#define EXPORT __declspec(dllexport)
#elif defined(APPLE) || defined(__GNUC__)
// GCC
#define EXPORT __attribute__((visibility("default")))
#else
// do nothing and hope for the best?
#define EXPORT
#endif

#ifdef __GNUC__
#define __cdecl __attribute__((cdecl))
#endif

#ifdef __cplusplus
extern "C" {
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ggpo/backends/p2p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ Peer2PeerBackend::DoPoll(int timeout)
}
// XXX: this is obviously a farce...
if (timeout) {
Sleep(1);
Platform::SleepMS(1);
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/lib/ggpo/backends/synctest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ SyncTestBackend::RaiseSyncError(const char *fmt, ...)
va_end(args);

puts(buf);
OutputDebugStringA(buf);
EndLog();
DebugBreak();
}
Expand All @@ -187,7 +186,7 @@ SyncTestBackend::BeginLog(int saving)
EndLog();

char filename[MAX_PATH];
CreateDirectoryA("synclogs", NULL);
Platform::CreateDirectory("synclogs", NULL);
sprintf(filename, "synclogs\\%s-%04d-%s.log",
saving ? "state" : "log",
_sync.GetFrameCount(),
Expand Down
7 changes: 0 additions & 7 deletions src/lib/ggpo/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@
#include "backends/spectator.h"
#include "ggponet.h"

BOOL WINAPI
DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
srand(Platform::GetCurrentTimeMS() + Platform::GetProcessID());
return TRUE;
}

void
ggpo_log(GGPOSession *ggpo, const char *fmt, ...)
{
Expand Down
15 changes: 13 additions & 2 deletions src/lib/ggpo/network/udp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ CreateSocket(int bind_port, int retries)
sockaddr_in sin;
int port;
int optval = 1;
struct linger loptval;
loptval.l_onoff = 0;
loptval.l_linger = 0;

s = socket(AF_INET, SOCK_DGRAM, 0);
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&optval, sizeof optval);
setsockopt(s, SOL_SOCKET, SO_DONTLINGER, (const char *)&optval, sizeof optval);
setsockopt(s, SOL_SOCKET, SO_LINGER, (const char *)&loptval, sizeof loptval);

// non-blocking...
u_long iMode = 1;
Expand Down Expand Up @@ -70,8 +73,12 @@ Udp::SendTo(char *buffer, int len, int flags, struct sockaddr *dst, int destlen)

int res = sendto(_socket, buffer, len, flags, dst, destlen);
if (res == SOCKET_ERROR) {
#ifdef _WIN32
DWORD err = WSAGetLastError();
DWORD e2 = WSAENOTSOCK;
#else
int err = 1;
#endif
Log("unknown error in sendto (erro: %d wsaerr: %d).\n", res, err);
ASSERT(FALSE && "Unknown error in sendto");
}
Expand All @@ -83,7 +90,7 @@ Udp::OnLoopPoll(void *cookie)
{
uint8 recv_buf[MAX_UDP_PACKET_SIZE];
sockaddr_in recv_addr;
int recv_addr_len;
socklen_t recv_addr_len;

for (;;) {
recv_addr_len = sizeof(recv_addr);
Expand All @@ -92,7 +99,11 @@ Udp::OnLoopPoll(void *cookie)
// TODO: handle len == 0... indicates a disconnect.

if (len == -1) {
#ifdef _WIN32
int error = WSAGetLastError();
#else
int error = 1;
#endif
if (error != WSAEWOULDBLOCK) {
Log("recvfrom WSAGetLastError returned %d (%x).\n", error, error);
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ggpo/network/udp_msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,4 @@ struct UdpMsg

#pragma pack(pop)

#endif
#endif
2 changes: 1 addition & 1 deletion src/lib/ggpo/network/udp_proto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ UdpProtocol::HandlesMsg(sockaddr_in &from,
if (!_udp) {
return false;
}
return _peer_addr.sin_addr.S_un.S_addr == from.sin_addr.S_un.S_addr &&
return _peer_addr.sin_addr.s_addr == from.sin_addr.s_addr &&
_peer_addr.sin_port == from.sin_port;
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/ggpo/network/udp_proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class UdpProtocol : public IPollSink
} network_interrupted;
} u;

UdpProtocol::Event(Type t = Unknown) : type(t) { }
Event(Type t = Unknown) : type(t) { }
};

public:
Expand Down
Loading