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