hedgewars/uFLIPC.pas
author unc0rr
Sun, 21 Sep 2014 00:37:50 +0400
branchqmlfrontend
changeset 10418 091d2c0216c3
parent 10416 1c301054694d
child 10420 02c573d19224
permissions -rw-r--r--
Move away from passing shortstrings into C code, now IPC works
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10406
b5fd52ac760f Basic layout of frontlib, some more sdl bindings
unc0rr
parents:
diff changeset
     1
unit uFLIPC;
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
     2
interface
10418
091d2c0216c3 Move away from passing shortstrings into C code, now IPC works
unc0rr
parents: 10416
diff changeset
     3
uses SDLh, uFLTypes;
10406
b5fd52ac760f Basic layout of frontlib, some more sdl bindings
unc0rr
parents:
diff changeset
     4
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
     5
var msgFrontend, msgEngine: TIPCMessage;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
     6
    mutFrontend, mutEngine: PSDL_mutex;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
     7
    condFrontend, condEngine: PSDL_cond;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
     8
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
     9
procedure initIPC;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
    10
procedure freeIPC;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
    11
10418
091d2c0216c3 Move away from passing shortstrings into C code, now IPC works
unc0rr
parents: 10416
diff changeset
    12
procedure ipcToEngine(len: byte; msg: PChar); cdecl; export;
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
    13
function  ipcReadFromEngine: shortstring;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
    14
function  ipcCheckFromEngine: boolean;
10406
b5fd52ac760f Basic layout of frontlib, some more sdl bindings
unc0rr
parents:
diff changeset
    15
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    16
procedure ipcToFrontend(s: shortstring);
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    17
function ipcReadFromFrontend: shortstring;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    18
function ipcCheckFromFrontend: boolean;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    19
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
    20
procedure registerIPCCallback(p: pointer; f: TIPCCallback); cdecl; export;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
    21
10406
b5fd52ac760f Basic layout of frontlib, some more sdl bindings
unc0rr
parents:
diff changeset
    22
implementation
b5fd52ac760f Basic layout of frontlib, some more sdl bindings
unc0rr
parents:
diff changeset
    23
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
    24
var callbackPointer: pointer;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
    25
    callbackFunction: TIPCCallback;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
    26
    callbackListenerThread: PSDL_Thread;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
    27
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    28
procedure ipcSend(var s: shortstring; var msg: TIPCMessage; mut: PSDL_mutex; cond: PSDL_cond);
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    29
begin
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    30
    SDL_LockMutex(mut);
10418
091d2c0216c3 Move away from passing shortstrings into C code, now IPC works
unc0rr
parents: 10416
diff changeset
    31
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    32
    while (msg.str[0] > #0) or (msg.buf <> nil) do
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    33
        SDL_CondWait(cond, mut);
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    34
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    35
    msg.str:= s;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    36
    SDL_CondSignal(cond);
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
    37
    SDL_UnlockMutex(mut);
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    38
end;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    39
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    40
function ipcRead(var msg: TIPCMessage; mut: PSDL_mutex; cond: PSDL_cond): shortstring;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    41
begin
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    42
    SDL_LockMutex(mut);
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    43
    while (msg.str[0] = #0) and (msg.buf = nil) do
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    44
        SDL_CondWait(cond, mut);
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    45
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    46
    ipcRead:= msg.str;
10418
091d2c0216c3 Move away from passing shortstrings into C code, now IPC works
unc0rr
parents: 10416
diff changeset
    47
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    48
    msg.str[0]:= #0;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    49
    if msg.buf <> nil then
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    50
    begin
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    51
        FreeMem(msg.buf, msg.len);
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    52
        msg.buf:= nil
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    53
    end;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    54
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    55
    SDL_CondSignal(cond);
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    56
    SDL_UnlockMutex(mut)
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    57
end;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    58
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    59
function ipcCheck(var msg: TIPCMessage; mut: PSDL_mutex): boolean;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    60
begin
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    61
    SDL_LockMutex(mut);
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    62
    ipcCheck:= (msg.str[0] > #0) or (msg.buf <> nil);
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    63
    SDL_UnlockMutex(mut)
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    64
end;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    65
10418
091d2c0216c3 Move away from passing shortstrings into C code, now IPC works
unc0rr
parents: 10416
diff changeset
    66
procedure ipcToEngine(len: byte; msg: PChar); cdecl; export;
091d2c0216c3 Move away from passing shortstrings into C code, now IPC works
unc0rr
parents: 10416
diff changeset
    67
var s: shortstring;
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
    68
begin
10418
091d2c0216c3 Move away from passing shortstrings into C code, now IPC works
unc0rr
parents: 10416
diff changeset
    69
    writeln(stderr, len);
091d2c0216c3 Move away from passing shortstrings into C code, now IPC works
unc0rr
parents: 10416
diff changeset
    70
    Move(msg^, s[1], len);
091d2c0216c3 Move away from passing shortstrings into C code, now IPC works
unc0rr
parents: 10416
diff changeset
    71
    s[0]:= char(len);
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    72
    ipcSend(s, msgEngine, mutEngine, condEngine)
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    73
end;
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
    74
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    75
procedure ipcToFrontend(s: shortstring);
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    76
begin
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    77
    ipcSend(s, msgFrontend, mutFrontend, condFrontend)
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
    78
end;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
    79
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
    80
function ipcReadFromEngine: shortstring;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
    81
begin
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    82
    ipcReadFromEngine:= ipcRead(msgFrontend, mutFrontend, condFrontend)
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    83
end;
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
    84
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    85
function ipcReadFromFrontend: shortstring;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    86
begin
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    87
    ipcReadFromFrontend:= ipcRead(msgEngine, mutEngine, condEngine)
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
    88
end;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
    89
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
    90
function ipcCheckFromEngine: boolean;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
    91
begin
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    92
    ipcCheckFromEngine:= ipcCheck(msgFrontend, mutFrontend)
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    93
end;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    94
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    95
function ipcCheckFromFrontend: boolean;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    96
begin
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    97
    ipcCheckFromFrontend:= ipcCheck(msgEngine, mutEngine)
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
    98
end;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
    99
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   100
function  listener(p: pointer): Longint; cdecl; export;
10418
091d2c0216c3 Move away from passing shortstrings into C code, now IPC works
unc0rr
parents: 10416
diff changeset
   101
var s: shortstring;
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   102
begin
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   103
    listener:= 0;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   104
    repeat
10418
091d2c0216c3 Move away from passing shortstrings into C code, now IPC works
unc0rr
parents: 10416
diff changeset
   105
        s:= ipcReadFromEngine();
091d2c0216c3 Move away from passing shortstrings into C code, now IPC works
unc0rr
parents: 10416
diff changeset
   106
        callbackFunction(callbackPointer, byte(s[0]), @s[1])
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   107
    until false
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   108
end;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   109
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   110
procedure registerIPCCallback(p: pointer; f: TIPCCallback); cdecl; export;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   111
begin
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   112
    callbackPointer:= p;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   113
    callbackFunction:= f;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   114
    callbackListenerThread:= SDL_CreateThread(@listener{$IFDEF SDL2}, 'ipcListener'{$ENDIF}, nil);
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   115
end;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   116
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   117
procedure initIPC;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   118
begin
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   119
    msgFrontend.str:= '';
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   120
    msgFrontend.buf:= nil;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   121
    msgEngine.str:= '';
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   122
    msgEngine.buf:= nil;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   123
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   124
    callbackPointer:= nil;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   125
    callbackListenerThread:= nil;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   126
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   127
    mutFrontend:= SDL_CreateMutex;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   128
    mutEngine:= SDL_CreateMutex;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   129
    condFrontend:= SDL_CreateCond;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   130
    condEngine:= SDL_CreateCond;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   131
end;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   132
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   133
procedure freeIPC;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   134
begin
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   135
    SDL_KillThread(callbackListenerThread);
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   136
    SDL_DestroyMutex(mutFrontend);
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   137
    SDL_DestroyMutex(mutEngine);
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   138
    SDL_DestroyCond(condFrontend);
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   139
    SDL_DestroyCond(condEngine);
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   140
end;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   141
10406
b5fd52ac760f Basic layout of frontlib, some more sdl bindings
unc0rr
parents:
diff changeset
   142
end.