hedgewars/uFLIPC.pas
author unc0rr
Sun, 28 Sep 2014 01:17:55 +0400
branchqmlfrontend
changeset 10430 899a30018ede
parent 10428 7c25297720f1
child 10898 f373838129c2
permissions -rw-r--r--
Getter and setter for seed
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
10428
7c25297720f1 More refactoring: move PoC preview getting code into flib
unc0rr
parents: 10426
diff changeset
    12
procedure ipcToEngine(s: shortstring);
10420
unc0rr
parents: 10418
diff changeset
    13
//function  ipcReadFromEngine: shortstring;
unc0rr
parents: 10418
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);
10420
unc0rr
parents: 10418
diff changeset
    17
procedure ipcToFrontendRaw(p: pointer; len: Longword);
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    18
function ipcReadFromFrontend: shortstring;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    19
function ipcCheckFromFrontend: boolean;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    20
10428
7c25297720f1 More refactoring: move PoC preview getting code into flib
unc0rr
parents: 10426
diff changeset
    21
procedure registerIPCCallback(p: pointer; f: TIPCCallback);
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
    22
10406
b5fd52ac760f Basic layout of frontlib, some more sdl bindings
unc0rr
parents:
diff changeset
    23
implementation
b5fd52ac760f Basic layout of frontlib, some more sdl bindings
unc0rr
parents:
diff changeset
    24
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
    25
var callbackPointer: pointer;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
    26
    callbackFunction: TIPCCallback;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
    27
    callbackListenerThread: PSDL_Thread;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
    28
10420
unc0rr
parents: 10418
diff changeset
    29
procedure ipcSend(var s: TIPCMessage; var msg: TIPCMessage; mut: PSDL_mutex; cond: PSDL_cond);
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    30
begin
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    31
    SDL_LockMutex(mut);
10418
091d2c0216c3 Move away from passing shortstrings into C code, now IPC works
unc0rr
parents: 10416
diff changeset
    32
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    33
    while (msg.str[0] > #0) or (msg.buf <> nil) do
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    34
        SDL_CondWait(cond, mut);
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    35
10420
unc0rr
parents: 10418
diff changeset
    36
    msg:= s;
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    37
    SDL_CondSignal(cond);
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
    38
    SDL_UnlockMutex(mut);
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    39
end;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    40
10420
unc0rr
parents: 10418
diff changeset
    41
function ipcRead(var msg: TIPCMessage; mut: PSDL_mutex; cond: PSDL_cond): TIPCMessage;
unc0rr
parents: 10418
diff changeset
    42
var tmp: pointer;
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    43
begin
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    44
    SDL_LockMutex(mut);
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    45
    while (msg.str[0] = #0) and (msg.buf = nil) do
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    46
        SDL_CondWait(cond, mut);
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    47
10420
unc0rr
parents: 10418
diff changeset
    48
    if msg.buf <> nil then
unc0rr
parents: 10418
diff changeset
    49
    begin
unc0rr
parents: 10418
diff changeset
    50
        tmp:= msg.buf;
unc0rr
parents: 10418
diff changeset
    51
        msg.buf:= GetMem(msg.len);
unc0rr
parents: 10418
diff changeset
    52
        Move(tmp^, msg.buf^, msg.len);
unc0rr
parents: 10418
diff changeset
    53
        FreeMem(tmp, msg.len)
unc0rr
parents: 10418
diff changeset
    54
    end;
unc0rr
parents: 10418
diff changeset
    55
unc0rr
parents: 10418
diff changeset
    56
    ipcRead:= msg;
10418
091d2c0216c3 Move away from passing shortstrings into C code, now IPC works
unc0rr
parents: 10416
diff changeset
    57
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    58
    msg.str[0]:= #0;
10420
unc0rr
parents: 10418
diff changeset
    59
    msg.buf:= nil;
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    60
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    61
    SDL_CondSignal(cond);
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    62
    SDL_UnlockMutex(mut)
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    63
end;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    64
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    65
function ipcCheck(var msg: TIPCMessage; mut: PSDL_mutex): boolean;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    66
begin
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    67
    SDL_LockMutex(mut);
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    68
    ipcCheck:= (msg.str[0] > #0) or (msg.buf <> nil);
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    69
    SDL_UnlockMutex(mut)
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    70
end;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    71
10428
7c25297720f1 More refactoring: move PoC preview getting code into flib
unc0rr
parents: 10426
diff changeset
    72
procedure ipcToEngine(s: shortstring);
10420
unc0rr
parents: 10418
diff changeset
    73
var msg: TIPCMessage;
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
    74
begin
10428
7c25297720f1 More refactoring: move PoC preview getting code into flib
unc0rr
parents: 10426
diff changeset
    75
    msg.str:= s;
10420
unc0rr
parents: 10418
diff changeset
    76
    msg.buf:= nil;
unc0rr
parents: 10418
diff changeset
    77
    ipcSend(msg, msgEngine, mutEngine, condEngine)
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    78
end;
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
    79
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    80
procedure ipcToFrontend(s: shortstring);
10420
unc0rr
parents: 10418
diff changeset
    81
var msg: TIPCMessage;
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    82
begin
10420
unc0rr
parents: 10418
diff changeset
    83
    msg.str:= s;
unc0rr
parents: 10418
diff changeset
    84
    msg.buf:= nil;
unc0rr
parents: 10418
diff changeset
    85
    ipcSend(msg, msgFrontend, mutFrontend, condFrontend)
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
    86
end;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
    87
10420
unc0rr
parents: 10418
diff changeset
    88
procedure ipcToFrontendRaw(p: pointer; len: Longword);
unc0rr
parents: 10418
diff changeset
    89
var msg: TIPCMessage;
unc0rr
parents: 10418
diff changeset
    90
begin
unc0rr
parents: 10418
diff changeset
    91
    msg.str[0]:= #0;
unc0rr
parents: 10418
diff changeset
    92
    msg.len:= len;
unc0rr
parents: 10418
diff changeset
    93
    msg.buf:= GetMem(len);
unc0rr
parents: 10418
diff changeset
    94
    Move(p^, msg.buf^, len);
unc0rr
parents: 10418
diff changeset
    95
    ipcSend(msg, msgFrontend, mutFrontend, condFrontend)
unc0rr
parents: 10418
diff changeset
    96
end;
unc0rr
parents: 10418
diff changeset
    97
unc0rr
parents: 10418
diff changeset
    98
function ipcReadFromEngine: TIPCMessage;
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
    99
begin
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   100
    ipcReadFromEngine:= ipcRead(msgFrontend, mutFrontend, condFrontend)
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   101
end;
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   102
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   103
function ipcReadFromFrontend: shortstring;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   104
begin
10420
unc0rr
parents: 10418
diff changeset
   105
    ipcReadFromFrontend:= ipcRead(msgEngine, mutEngine, condEngine).str
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   106
end;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   107
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   108
function ipcCheckFromEngine: boolean;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   109
begin
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   110
    ipcCheckFromEngine:= ipcCheck(msgFrontend, mutFrontend)
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   111
end;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   112
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   113
function ipcCheckFromFrontend: boolean;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   114
begin
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   115
    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
   116
end;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   117
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   118
function  listener(p: pointer): Longint; cdecl; export;
10420
unc0rr
parents: 10418
diff changeset
   119
var msg: TIPCMessage;
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   120
begin
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   121
    listener:= 0;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   122
    repeat
10420
unc0rr
parents: 10418
diff changeset
   123
        msg:= ipcReadFromEngine();
unc0rr
parents: 10418
diff changeset
   124
        if msg.buf = nil then
unc0rr
parents: 10418
diff changeset
   125
            callbackFunction(callbackPointer, @msg.str[1], byte(msg.str[0]))
unc0rr
parents: 10418
diff changeset
   126
        else
unc0rr
parents: 10418
diff changeset
   127
        begin
unc0rr
parents: 10418
diff changeset
   128
            callbackFunction(callbackPointer, msg.buf, msg.len);
unc0rr
parents: 10418
diff changeset
   129
            FreeMem(msg.buf, msg.len)
unc0rr
parents: 10418
diff changeset
   130
        end
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   131
    until false
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   132
end;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   133
10428
7c25297720f1 More refactoring: move PoC preview getting code into flib
unc0rr
parents: 10426
diff changeset
   134
procedure registerIPCCallback(p: pointer; f: TIPCCallback);
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   135
begin
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   136
    callbackPointer:= p;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   137
    callbackFunction:= f;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   138
    callbackListenerThread:= SDL_CreateThread(@listener{$IFDEF SDL2}, 'ipcListener'{$ENDIF}, nil);
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   139
end;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   140
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   141
procedure initIPC;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   142
begin
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   143
    msgFrontend.str:= '';
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   144
    msgFrontend.buf:= nil;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   145
    msgEngine.str:= '';
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   146
    msgEngine.buf:= nil;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   147
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   148
    callbackPointer:= nil;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   149
    callbackListenerThread:= nil;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   150
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   151
    mutFrontend:= SDL_CreateMutex;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   152
    mutEngine:= SDL_CreateMutex;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   153
    condFrontend:= SDL_CreateCond;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   154
    condEngine:= SDL_CreateCond;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   155
end;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   156
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   157
procedure freeIPC;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   158
begin
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   159
    SDL_KillThread(callbackListenerThread);
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   160
    SDL_DestroyMutex(mutFrontend);
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   161
    SDL_DestroyMutex(mutEngine);
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   162
    SDL_DestroyCond(condFrontend);
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   163
    SDL_DestroyCond(condEngine);
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   164
end;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   165
10406
b5fd52ac760f Basic layout of frontlib, some more sdl bindings
unc0rr
parents:
diff changeset
   166
end.