Navigation Menu

Skip to content

Commit

Permalink
send heartbeats
Browse files Browse the repository at this point in the history
  • Loading branch information
DexterHaslem committed Jun 4, 2015
1 parent e298a88 commit 2e0396e
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 17 deletions.
12 changes: 10 additions & 2 deletions cl_dll/ff/achievements/ff_steamworks_msg.cpp
Expand Up @@ -4,9 +4,16 @@
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

CFFSteamworksMessage::CFFSteamworksMessage( SteamworksCommand_e eCmd, const char *key, const char *val ) : m_cmd(eCmd),
m_strKey(key), m_strValRaw(val)
CFFSteamworksMessage::CFFSteamworksMessage( SteamworksCommand_e eCmd ) : m_cmd(eCmd)
{
m_strKey = "";
m_strValRaw = "";
}

CFFSteamworksMessage::CFFSteamworksMessage( SteamworksCommand_e eCmd, const char *key, const char *val ) : m_cmd(eCmd)
{
m_strKey = key ? key : "";
m_strValRaw = val ? val : "";
}

CFFSteamworksMessage::~CFFSteamworksMessage( )
Expand All @@ -26,3 +33,4 @@ const char * CFFSteamworksMessage::GetVal( ) const
{
return m_strValRaw.c_str( );
}

2 changes: 2 additions & 0 deletions cl_dll/ff/achievements/ff_steamworks_msg.h
Expand Up @@ -23,6 +23,8 @@ class CFFSteamworksMessage
bool GetNetworkFormat ( char* buff ) const;
//CFFSteamworksMessage(const char *rawStr);
CFFSteamworksMessage( SteamworksCommand_e eCmd, const char *key, const char *val );
CFFSteamworksMessage( SteamworksCommand_e eCmd );
//static CFFSteamworksMessage& CreateHeartbeat( );
~CFFSteamworksMessage( );
private:
SteamworksCommand_e m_cmd;
Expand Down
42 changes: 27 additions & 15 deletions cl_dll/ff/achievements/ff_steamworks_thread.cpp
Expand Up @@ -25,7 +25,9 @@
#define SERVER_EXEC_NAME "bin/sws.exe"


CFFSteamworksThread::CFFSteamworksThread( void ) : m_iPollRate(500), m_hProcess(NULL), m_hThread(NULL)
// send a heartbeat every 5 poll (2.5 second), send heartbeat right away
CFFSteamworksThread::CFFSteamworksThread( void ) : m_iPollRate(500), m_hProcess(NULL), m_hThread(NULL),
m_iHeartbeatRate(5), m_iHeartbeatCheckCount(m_iHeartbeatRate)
{
SetName("SteamworksThread");
m_bIsRunning = false;
Expand Down Expand Up @@ -69,8 +71,9 @@ void CFFSteamworksThread::KillServerProcess( void )
// the server loop will actually end once the socket closes, however make sure and axe it here just incase dragons
if ( !m_hProcess || !m_hThread)
return;
CloseHandle( m_hProcess );
CloseHandle( m_hThread );
// yikes: results in race condition with os if thread falls through, so just let it..
// CloseHandle( m_hProcess );
// CloseHandle( m_hThread );
}

void CFFSteamworksThread::QueueMessage( const CFFSteamworksMessage &msg )
Expand All @@ -83,7 +86,7 @@ void CFFSteamworksThread::ShutdownServer( void )
DevMsg( "[Steamworks thread] ShutdownServer" );
m_Sock.Close( );
m_bIsShutdown = true;
KillServerProcess ( );
KillServerProcess( );
// avoid race conditions
if ( m_bIsRunning )
Terminate( );
Expand All @@ -110,14 +113,12 @@ int CFFSteamworksThread::Run()
return -2;
}

QueueMessage(CFFSteamworksMessage(SWC_HEARTBEAT, "key", "val"));

while ( IsAlive( ) )
while ( IsAlive( ) && !m_bIsShutdown )
{
if (m_bIsShutdown)
if ( ++m_iHeartbeatCheckCount >= m_iHeartbeatRate )
{
m_bIsRunning = false;
return 1;
m_iHeartbeatCheckCount = 0;
SendMsg( CFFSteamworksMessage( SWC_HEARTBEAT ) );
}

int queueCount = m_QueuedMessages.Count( );
Expand All @@ -130,14 +131,11 @@ int CFFSteamworksThread::Run()
for (int i = 0; i < queueCount; ++i)
{
CFFSteamworksMessage &msg = m_QueuedMessages.Element ( i );
const int maxSize = 1024;
char buff[maxSize];
Q_memset( buff, maxSize, 0 );
Q_snprintf( buff, maxSize,"%d|%s|%s!",(int)msg.GetCommand( ), msg.GetKey( ), msg.GetVal( ) );
m_Sock.Send( buff );
SendMsg( msg );
}

m_QueuedMessages.RemoveAll( );
Sleep ( m_iPollRate );
}

DevMsg( "[Steamworks thread] Run3 - fallthrough\n" );
Expand All @@ -146,6 +144,20 @@ int CFFSteamworksThread::Run()
return 1;
}

void CFFSteamworksThread::SendMsg( const CFFSteamworksMessage &msg )
{
const int maxSize = 1024;
char buff[maxSize] = { 0 };
Q_snprintf( buff, maxSize,"%d|%s|%s!",(int)msg.GetCommand( ), msg.GetKey( ), msg.GetVal( ) );
DevMsg( "[Steamworks thread] SendMsg - '%s'\n", buff );

if ( !m_Sock.Send( buff ) )
{
// make sure to check this here, it will return false and we want to fall through
m_bIsShutdown = true;
}
}

CFFSteamworksThread& CFFSteamworksThread::GetInstance()
{
static CFFSteamworksThread thread;
Expand Down
4 changes: 4 additions & 0 deletions cl_dll/ff/achievements/ff_steamworks_thread.h
Expand Up @@ -18,12 +18,16 @@ class CFFSteamworksThread : public CThread
private:
void KillServerProcess( void );
bool CreateServerProcess( void );
void SendMsg( const CFFSteamworksMessage &msg );

// bit of a hack, these are HANDLEs which are just void* so do this to avoid the windows.h crap here
void* m_hProcess;
void* m_hThread;

int m_iPollRate; //ms
int m_iHeartbeatRate; // per poll
int m_iHeartbeatCheckCount;

bool m_bIsShutdown;
CUtlVector<CFFSteamworksMessage> m_QueuedMessages;
Socks m_Sock;
Expand Down
10 changes: 10 additions & 0 deletions external/steamworks-server/Server.cs
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Globalization;
using System.Net;
using System.Net.Sockets;
Expand All @@ -20,13 +21,16 @@ public enum Command

private class Message
{
private readonly string _raw;

public Command Command { get; private set; }
public string Key { get; private set; }
public string Value { get; private set; }
public bool IsValid { get; private set; }

public Message(string raw)
{
_raw = raw;
if (string.IsNullOrWhiteSpace(raw))
return;

Expand All @@ -52,6 +56,11 @@ public Message(string raw)
Command = command;
IsValid = true;
}

public override string ToString()
{
return _raw;
}
}

// <cmd>|<key>|<value>!
Expand Down Expand Up @@ -140,6 +149,7 @@ private void HandleMessage(Message msg)
if (msg == null || !msg.IsValid)
return;

Debug.WriteLine("[Steamworks Server] HandleMessage: '{0}'", msg);
if (_steamworksManager == null)
{
_steamworksManager = new SteamworksManager();
Expand Down

0 comments on commit 2e0396e

Please sign in to comment.