project_files/frontlib/ipc/ipcbase.c
author nemo
Fri, 12 Apr 2019 11:41:35 -0400
changeset 14797 38e66519e585
parent 10017 de822cd3df3a
permissions -rw-r--r--
ugly hacked in argument for port to remove unc0rr's excuse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
     1
/*
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
     2
 * Hedgewars, a free turn based strategy game
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
     3
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
     4
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
     5
 * This program is free software; you can redistribute it and/or
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
     6
 * modify it under the terms of the GNU General Public License
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
     7
 * as published by the Free Software Foundation; either version 2
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
     8
 * of the License, or (at your option) any later version.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
     9
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    13
 * GNU General Public License for more details.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    14
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    15
 * You should have received a copy of the GNU General Public License
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    16
 * along with this program; if not, write to the Free Software
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    18
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    19
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    20
#include "ipcbase.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    21
#include "../util/logging.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    22
#include "../util/util.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    23
#include "../socket.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    24
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    25
#include <string.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    26
#include <stdbool.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    27
#include <stdlib.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    28
#include <stdio.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    29
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    30
/*
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    31
 * The receive buffer has to be able to hold any message that might be received. Normally
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    32
 * the messages are at most 256 bytes, but the map preview contains 4097 bytes (4096 for a
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    33
 * bitmap, 1 for the number of hogs which fit on the map).
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    34
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    35
 * We don't need to worry about wasting a few kb though, and I like powers of two...
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    36
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    37
struct _flib_ipcbase {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    38
    uint8_t readBuffer[8192];
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    39
    int readBufferSize;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    40
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    41
    flib_acceptor *acceptor;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    42
    uint16_t port;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    43
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    44
    flib_tcpsocket *sock;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    45
};
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    46
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    47
flib_ipcbase *flib_ipcbase_create() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    48
    flib_ipcbase *result = flib_calloc(1, sizeof(flib_ipcbase));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    49
    flib_acceptor *acceptor = flib_acceptor_create(0);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    50
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    51
    if(!result || !acceptor) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    52
        free(result);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    53
        flib_acceptor_close(acceptor);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    54
        return NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    55
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    56
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    57
    result->acceptor = acceptor;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    58
    result->sock = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    59
    result->readBufferSize = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    60
    result->port = flib_acceptor_listenport(acceptor);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    61
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    62
    flib_log_i("Started listening for IPC connections on port %u", (unsigned)result->port);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    63
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    64
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    65
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    66
uint16_t flib_ipcbase_port(flib_ipcbase *ipc) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    67
    if(log_badargs_if(ipc==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    68
        return 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    69
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    70
    return ipc->port;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    71
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    72
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    73
void flib_ipcbase_destroy(flib_ipcbase *ipc) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    74
    if(ipc) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    75
        flib_acceptor_close(ipc->acceptor);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    76
        flib_socket_close(ipc->sock);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    77
        if(ipc->sock) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    78
            flib_log_d("IPC connection closed.");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    79
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    80
        free(ipc);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    81
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    82
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    83
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    84
IpcState flib_ipcbase_state(flib_ipcbase *ipc) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    85
    if(log_badargs_if(ipc==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    86
        return IPC_NOT_CONNECTED;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    87
    } else if(ipc->sock) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    88
        return IPC_CONNECTED;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    89
    } else if(ipc->acceptor) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    90
        return IPC_LISTENING;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    91
    } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    92
        return IPC_NOT_CONNECTED;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    93
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    94
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    95
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    96
static void receiveToBuffer(flib_ipcbase *ipc) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    97
    if(ipc->sock) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    98
        int size = flib_socket_nbrecv(ipc->sock, ipc->readBuffer+ipc->readBufferSize, sizeof(ipc->readBuffer)-ipc->readBufferSize);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
    99
        if(size>=0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   100
            ipc->readBufferSize += size;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   101
        } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   102
            flib_log_d("IPC connection lost.");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   103
            flib_socket_close(ipc->sock);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   104
            ipc->sock = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   105
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   106
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   107
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   108
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   109
static bool isMessageReady(flib_ipcbase *ipc) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   110
    return ipc->readBufferSize >= ipc->readBuffer[0]+1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   111
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   112
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   113
static void logSentMsg(const uint8_t *data, size_t len) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   114
    if(flib_log_isActive(FLIB_LOGLEVEL_DEBUG)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   115
        size_t msgStart = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   116
        while(msgStart < len) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   117
            uint8_t msglen = data[msgStart];
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   118
            if(msgStart+msglen < len) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   119
                flib_log_d("[IPC OUT][%03u]%*.*s",(unsigned)msglen, (unsigned)msglen, (unsigned)msglen, data+msgStart+1);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   120
            } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   121
                uint8_t msglen2 = len-msgStart-1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   122
                flib_log_d("[IPC OUT][%03u/%03u]%*.*s",(unsigned)msglen2, (unsigned)msglen, (unsigned)msglen2, (unsigned)msglen2, data+msgStart+1);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   123
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   124
            msgStart += (uint8_t)data[msgStart]+1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   125
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   126
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   127
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   128
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   129
static void logRecvMsg(const uint8_t *data) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   130
    if(flib_log_isActive(FLIB_LOGLEVEL_DEBUG)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   131
        uint8_t msglen = data[0];
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   132
        flib_log_d("[IPC IN][%03u]%*.*s",(unsigned)msglen, (unsigned)msglen, (unsigned)msglen, data+1);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   133
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   134
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   135
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   136
static void popFromReadBuffer(flib_ipcbase *ipc, uint8_t *outbuf, size_t size) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   137
    memcpy(outbuf, ipc->readBuffer, size);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   138
    memmove(ipc->readBuffer, ipc->readBuffer+size, ipc->readBufferSize-size);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   139
    ipc->readBufferSize -= size;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   140
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   141
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   142
int flib_ipcbase_recv_message(flib_ipcbase *ipc, void *data) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   143
    if(log_badargs_if2(ipc==NULL, data==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   144
        return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   145
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   146
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   147
    if(!isMessageReady(ipc)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   148
        receiveToBuffer(ipc);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   149
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   150
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   151
    if(isMessageReady(ipc)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   152
        int msgsize = ipc->readBuffer[0]+1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   153
        popFromReadBuffer(ipc, data, msgsize);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   154
        logRecvMsg(data);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   155
        return msgsize;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   156
    } else if(!ipc->sock && ipc->readBufferSize>0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   157
        flib_log_w("Last message from engine data stream is incomplete (received %u of %u bytes)", (unsigned)ipc->readBufferSize, (unsigned)(ipc->readBuffer[0])+1);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   158
        ipc->readBufferSize = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   159
        return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   160
    } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   161
        return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   162
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   163
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   164
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   165
int flib_ipcbase_recv_map(flib_ipcbase *ipc, void *data) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   166
    if(log_badargs_if2(ipc==NULL, data==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   167
        return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   168
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   169
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   170
    receiveToBuffer(ipc);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   171
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   172
    if(ipc->readBufferSize >= IPCBASE_MAPMSG_BYTES) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   173
        popFromReadBuffer(ipc, data, IPCBASE_MAPMSG_BYTES);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   174
        return IPCBASE_MAPMSG_BYTES;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   175
    } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   176
        return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   177
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   178
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   179
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   180
int flib_ipcbase_send_raw(flib_ipcbase *ipc, const void *data, size_t len) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   181
    if(log_badargs_if2(ipc==NULL, data==NULL && len>0)
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   182
            || log_w_if(!ipc->sock, "flib_ipcbase_send_raw: Not connected.")) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   183
        return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   184
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   185
    if(flib_socket_send(ipc->sock, data, len) == len) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   186
        logSentMsg(data, len);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   187
        return 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   188
    } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   189
        flib_log_w("Failed or incomplete IPC write: engine connection lost.");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   190
        flib_socket_close(ipc->sock);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   191
        ipc->sock = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   192
        return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   193
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   194
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   195
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   196
int flib_ipcbase_send_message(flib_ipcbase *ipc, void *data, size_t len) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   197
    if(log_badargs_if3(ipc==NULL, data==NULL && len>0, len>255)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   198
        return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   199
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   200
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   201
    uint8_t sendbuf[256];
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   202
    sendbuf[0] = len;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   203
    memcpy(sendbuf+1, data, len);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   204
    return flib_ipcbase_send_raw(ipc, sendbuf, len+1);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   205
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   206
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   207
void flib_ipcbase_accept(flib_ipcbase *ipc) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   208
    if(!log_badargs_if(ipc==NULL) && !ipc->sock && ipc->acceptor) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   209
        ipc->sock = flib_socket_accept(ipc->acceptor, true);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   210
        if(ipc->sock) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   211
            flib_log_d("IPC connection accepted.");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   212
            flib_acceptor_close(ipc->acceptor);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   213
            ipc->acceptor = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   214
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   215
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 8071
diff changeset
   216
}