hedgewars/uFLIPC.pas
author unc0rr
Sun, 28 Sep 2014 00:18:01 +0400
branchqmlfrontend
changeset 10426 727a154cf784
parent 10420 02c573d19224
child 10428 7c25297720f1
permissions -rw-r--r--
Some refactoring
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
10420
unc0rr
parents: 10418
diff changeset
    12
procedure ipcToEngine(p: PChar; len: byte); cdecl; export;
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
10426
727a154cf784 Some refactoring
unc0rr
parents: 10420
diff changeset
    21
procedure registerPreviewCallback(p: pointer; f: TIPCCallback); cdecl; export;
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
10420
unc0rr
parents: 10418
diff changeset
    72
procedure ipcToEngine(p: PChar; len: byte); cdecl; export;
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
10418
091d2c0216c3 Move away from passing shortstrings into C code, now IPC works
unc0rr
parents: 10416
diff changeset
    75
    writeln(stderr, len);
10420
unc0rr
parents: 10418
diff changeset
    76
    Move(p^, msg.str[1], len);
unc0rr
parents: 10418
diff changeset
    77
    msg.str[0]:= char(len);
unc0rr
parents: 10418
diff changeset
    78
    msg.buf:= nil;
unc0rr
parents: 10418
diff changeset
    79
    ipcSend(msg, msgEngine, mutEngine, condEngine)
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    80
end;
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
    81
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    82
procedure ipcToFrontend(s: shortstring);
10420
unc0rr
parents: 10418
diff changeset
    83
var msg: TIPCMessage;
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    84
begin
10420
unc0rr
parents: 10418
diff changeset
    85
    msg.str:= s;
unc0rr
parents: 10418
diff changeset
    86
    msg.buf:= nil;
unc0rr
parents: 10418
diff changeset
    87
    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
    88
end;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
    89
10420
unc0rr
parents: 10418
diff changeset
    90
procedure ipcToFrontendRaw(p: pointer; len: Longword);
unc0rr
parents: 10418
diff changeset
    91
var msg: TIPCMessage;
unc0rr
parents: 10418
diff changeset
    92
begin
unc0rr
parents: 10418
diff changeset
    93
    msg.str[0]:= #0;
unc0rr
parents: 10418
diff changeset
    94
    msg.len:= len;
unc0rr
parents: 10418
diff changeset
    95
    msg.buf:= GetMem(len);
unc0rr
parents: 10418
diff changeset
    96
    Move(p^, msg.buf^, len);
unc0rr
parents: 10418
diff changeset
    97
    ipcSend(msg, msgFrontend, mutFrontend, condFrontend)
unc0rr
parents: 10418
diff changeset
    98
end;
unc0rr
parents: 10418
diff changeset
    99
unc0rr
parents: 10418
diff changeset
   100
function ipcReadFromEngine: TIPCMessage;
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   101
begin
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   102
    ipcReadFromEngine:= ipcRead(msgFrontend, mutFrontend, condFrontend)
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   103
end;
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   104
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   105
function ipcReadFromFrontend: shortstring;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   106
begin
10420
unc0rr
parents: 10418
diff changeset
   107
    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
   108
end;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   109
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   110
function ipcCheckFromEngine: boolean;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   111
begin
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   112
    ipcCheckFromEngine:= ipcCheck(msgFrontend, mutFrontend)
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   113
end;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   114
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   115
function ipcCheckFromFrontend: boolean;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   116
begin
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   117
    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
   118
end;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   119
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   120
function  listener(p: pointer): Longint; cdecl; export;
10420
unc0rr
parents: 10418
diff changeset
   121
var msg: TIPCMessage;
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   122
begin
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   123
    listener:= 0;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   124
    repeat
10420
unc0rr
parents: 10418
diff changeset
   125
        msg:= ipcReadFromEngine();
unc0rr
parents: 10418
diff changeset
   126
        if msg.buf = nil then
unc0rr
parents: 10418
diff changeset
   127
            callbackFunction(callbackPointer, @msg.str[1], byte(msg.str[0]))
unc0rr
parents: 10418
diff changeset
   128
        else
unc0rr
parents: 10418
diff changeset
   129
        begin
unc0rr
parents: 10418
diff changeset
   130
            callbackFunction(callbackPointer, msg.buf, msg.len);
unc0rr
parents: 10418
diff changeset
   131
            FreeMem(msg.buf, msg.len)
unc0rr
parents: 10418
diff changeset
   132
        end
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   133
    until false
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   134
end;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   135
10426
727a154cf784 Some refactoring
unc0rr
parents: 10420
diff changeset
   136
procedure registerPreviewCallback(p: pointer; f: TIPCCallback); cdecl; export;
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   137
begin
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   138
    callbackPointer:= p;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   139
    callbackFunction:= f;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   140
    callbackListenerThread:= SDL_CreateThread(@listener{$IFDEF SDL2}, 'ipcListener'{$ENDIF}, nil);
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   141
end;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   142
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   143
procedure initIPC;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   144
begin
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   145
    msgFrontend.str:= '';
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   146
    msgFrontend.buf:= nil;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   147
    msgEngine.str:= '';
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   148
    msgEngine.buf:= nil;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   149
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   150
    callbackPointer:= nil;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   151
    callbackListenerThread:= nil;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   152
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   153
    mutFrontend:= SDL_CreateMutex;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   154
    mutEngine:= SDL_CreateMutex;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   155
    condFrontend:= SDL_CreateCond;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   156
    condEngine:= SDL_CreateCond;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   157
end;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   158
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   159
procedure freeIPC;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   160
begin
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   161
    SDL_KillThread(callbackListenerThread);
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   162
    SDL_DestroyMutex(mutFrontend);
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   163
    SDL_DestroyMutex(mutEngine);
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   164
    SDL_DestroyCond(condFrontend);
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   165
    SDL_DestroyCond(condEngine);
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   166
end;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   167
10406
b5fd52ac760f Basic layout of frontlib, some more sdl bindings
unc0rr
parents:
diff changeset
   168
end.