dfhack/shmserver/shms.h

125 lines
3.8 KiB
C

2009-12-22 14:19:39 -07:00
#ifndef DFCONNECT_H
#define DFCONNECT_H
2010-03-02 21:43:38 -07:00
#define CORE_VERSION 3
2009-12-22 14:19:39 -07:00
#define SHM_KEY 123466
2010-03-02 21:43:38 -07:00
#define SHM_HEADER 1024 // 1kB reserved for a header
#define SHM_BODY 1024*1024 // 1MB reserved for bulk data transfer
2010-01-01 16:21:55 -07:00
#define SHM_SIZE SHM_HEADER+SHM_BODY
2009-12-22 14:19:39 -07:00
2010-01-04 15:20:17 -07:00
2010-01-11 11:57:57 -07:00
// FIXME: add YIELD for linux, add single-core and multi-core compile targets for optimal speed
#ifdef LINUX_BUILD
2010-01-04 15:20:17 -07:00
// a full memory barrier! better be safe than sorry.
#define full_barrier asm volatile("" ::: "memory"); __sync_synchronize();
#define SCHED_YIELD sched_yield(); // slow but allows the SHM to work on single-core
// #define SCHED_YIELD usleep(0); // extremely slow
// #define SCHED_YIELD // works only on multi-core
#else
2010-01-11 11:57:57 -07:00
// we need windows.h for Sleep()
#define _WIN32_WINNT 0x0501 // needed for INPUT struct
#define WINVER 0x0501 // OpenThread(), PSAPI, Toolhelp32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
2010-02-28 13:21:20 -07:00
#define SCHED_YIELD Sleep(0); // slow on single-core, but avoids infinite lockup
// FIXME: detect MSVC here and use the right barrier magic
2010-02-28 13:21:20 -07:00
#ifdef __MINGW32__
#define full_barrier asm volatile("" ::: "memory");
#else
#include <intrin.h>
#pragma intrinsic(_ReadWriteBarrier)
#define full_barrier _ReadWriteBarrier();
#endif
#endif
enum DF_SHM_ERRORSTATE
{
SHM_OK, // all OK
SHM_CANT_GET_SHM, // getting the SHM ID failed for some reason
SHM_CANT_ATTACH, // we can't attach the shm for some reason
SHM_SECOND_DF // we are a second DF process, can't use SHM at all
};
2010-03-02 21:43:38 -07:00
enum CORE_COMMAND
2009-12-22 14:19:39 -07:00
{
2010-03-02 21:43:38 -07:00
CORE_RUNNING = 0, // no command, normal server execution
2009-12-22 14:19:39 -07:00
2010-03-02 21:43:38 -07:00
CORE_GET_VERSION, // protocol version query
CORE_RET_VERSION, // return the protocol version
2009-12-22 14:19:39 -07:00
2010-03-02 21:43:38 -07:00
CORE_GET_PID, // query for the process ID
CORE_RET_PID, // return process ID
2009-12-22 14:19:39 -07:00
// version 1 stuff below
2010-03-02 21:43:38 -07:00
CORE_DFPP_READ, // cl -> sv, read some data
CORE_RET_DATA, // sv -> cl, returned data
2009-12-22 14:19:39 -07:00
2010-03-02 21:43:38 -07:00
CORE_READ_DWORD, // cl -> sv, read a dword
CORE_RET_DWORD, // sv -> cl, returned dword
2009-12-22 14:19:39 -07:00
2010-03-02 21:43:38 -07:00
CORE_READ_WORD, // cl -> sv, read a word
CORE_RET_WORD, // sv -> cl, returned word
2009-12-22 14:19:39 -07:00
2010-03-02 21:43:38 -07:00
CORE_READ_BYTE, // cl -> sv, read a byte
CORE_RET_BYTE, // sv -> cl, returned byte
2009-12-22 14:19:39 -07:00
2010-03-02 21:43:38 -07:00
CORE_SV_ERROR, // there was a server error
CORE_CL_ERROR, // there was a client error
2009-12-22 14:19:39 -07:00
2010-03-02 21:43:38 -07:00
CORE_WRITE,// client writes to server
CORE_WRITE_DWORD,// client writes a DWORD to server
CORE_WRITE_WORD,// client writes a WORD to server
CORE_WRITE_BYTE,// client writes a BYTE to server
2009-12-22 14:19:39 -07:00
2010-03-02 21:43:38 -07:00
CORE_SUSPEND, // client notifies server to wait for commands (server is stalled in busy wait)
CORE_SUSPENDED, // response to WAIT, server is stalled in busy wait
2009-12-22 14:19:39 -07:00
// all strings capped at 1MB
2010-03-02 21:43:38 -07:00
CORE_READ_STL_STRING,// client requests contents of STL string at address
CORE_READ_C_STRING,// client requests contents of a C string at address, max length (0 means zero terminated)
CORE_RET_STRING, // sv -> cl length + string contents
CORE_WRITE_STL_STRING,// client wants to set STL string at address to something
2010-03-02 21:43:38 -07:00
// compare affinity and determine if using yield is required
CORE_SYNC_YIELD,// cl sends affinity to sv, sv sets yield
CORE_SYNC_YIELD_RET,// sv returns yield bool
2009-12-22 14:19:39 -07:00
2010-03-02 21:43:38 -07:00
NUM_CORE_CMDS
2009-12-22 14:19:39 -07:00
};
enum DF_ERROR
{
DFEE_INVALID_COMMAND,
DFEE_BUFFER_OVERFLOW
};
2010-03-02 21:43:38 -07:00
typedef union
2009-12-22 14:19:39 -07:00
{
2010-03-02 21:43:38 -07:00
struct
{
volatile uint16_t command;
volatile uint16_t module;
} parts;
2010-01-11 11:57:57 -07:00
volatile uint32_t pingpong;
2010-03-02 21:43:38 -07:00
inline void set(uint16_t module, uint16_t command)
{
pingpong = module + command << 16;
}
} shm_cmd;
2009-12-22 14:19:39 -07:00
typedef struct
{
2010-03-02 21:43:38 -07:00
shm_cmd cmd;
2009-12-22 14:19:39 -07:00
uint32_t address;
uint32_t value;
uint32_t length;
2010-03-02 21:43:38 -07:00
} shm_header;
void SHM_Act (void);
2010-03-02 21:43:38 -07:00
void InitModules (void);
bool isValidSHM();
2010-03-02 21:43:38 -07:00
uint32_t OS_getPID();
2010-01-04 15:20:17 -07:00
#endif