project_files/frontlib/net/netbase.c
author antonc27 <antonc27@mail.ru>
Sun, 09 Aug 2015 15:37:29 +0200
branchios-revival
changeset 11099 c51e4c942525
parent 10017 de822cd3df3a
permissions -rw-r--r--
- Fix for wrong start paramIndex in case of Hwlibrary
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     1
/*
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     2
 * Hedgewars, a free turn based strategy game
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     3
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     4
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     5
 * This program is free software; you can redistribute it and/or
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     6
 * modify it under the terms of the GNU General Public License
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     7
 * as published by the Free Software Foundation; either version 2
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     8
 * of the License, or (at your option) any later version.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     9
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    13
 * GNU General Public License for more details.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    14
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    15
 * You should have received a copy of the GNU General Public License
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    16
 * along with this program; if not, write to the Free Software
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    18
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    19
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    20
#include "netbase.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    21
#include "../util/buffer.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    22
#include "../util/logging.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    23
#include "../util/util.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    24
#include "../socket.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    25
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    26
#include <string.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    27
#include <stdbool.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    28
#include <stdlib.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    29
#include <stdio.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    30
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    31
#define NET_READBUFFER_LIMIT (1024*1024)
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    32
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    33
struct _flib_netbase {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    34
    flib_vector *readBuffer;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    35
    flib_tcpsocket *sock;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    36
};
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    37
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    38
flib_netbase *flib_netbase_create(const char *server, uint16_t port) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    39
    if(log_badargs_if2(server==NULL, port==0)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    40
        return NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    41
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    42
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    43
    flib_netbase *result = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    44
    flib_netbase *newNet =  flib_calloc(1, sizeof(flib_netbase));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    45
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    46
    if(newNet) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    47
        newNet->readBuffer = flib_vector_create();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    48
        newNet->sock = flib_socket_connect(server, port);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    49
        if(newNet->readBuffer && newNet->sock) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    50
            flib_log_i("Connected to server %s:%u", server, (unsigned)port);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    51
            result = newNet;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    52
            newNet = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    53
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    54
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    55
    flib_netbase_destroy(newNet);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    56
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    57
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    58
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    59
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    60
void flib_netbase_destroy(flib_netbase *net) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    61
    if(net) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    62
        flib_socket_close(net->sock);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    63
        flib_vector_destroy(net->readBuffer);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    64
        free(net);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    65
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    66
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    67
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    68
bool flib_netbase_connected(flib_netbase *net) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    69
    if(!log_badargs_if(net==NULL) && net->sock) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    70
        return true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    71
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    72
    return false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    73
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    74
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    75
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    76
 * Parses and returns a message, and removes it from the vector.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    77
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    78
static flib_netmsg *parseMessage(flib_vector *vec) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    79
    const uint8_t *partStart = flib_vector_data(vec);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    80
    const uint8_t *end = partStart+flib_vector_size(vec);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    81
    flib_netmsg *result = flib_netmsg_create();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    82
    if(!result) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    83
        return NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    84
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    85
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    86
    while(1) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    87
        const uint8_t *partEnd = memchr(partStart, '\n', end-partStart);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    88
        if(!partEnd) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    89
            // message incomplete
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    90
            flib_netmsg_destroy(result);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    91
            return NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    92
        } else if(partEnd-partStart == 0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    93
            // Zero-length part, message end marker. Remove the message from the vector.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    94
            uint8_t *vectorStart = flib_vector_data(vec);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    95
            size_t msgLen = partEnd+1-vectorStart;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    96
            memmove(vectorStart, partEnd+1, flib_vector_size(vec)-msgLen);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    97
            flib_vector_resize(vec, flib_vector_size(vec)-msgLen);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    98
            return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    99
        } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   100
            if(flib_netmsg_append_part(result, partStart, partEnd-partStart)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   101
                flib_netmsg_destroy(result);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   102
                return NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   103
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   104
            partStart = partEnd+1; // Skip the '\n'
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   105
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   106
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   107
    return NULL; // Never reached
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   108
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   109
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   110
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   111
 * Receive some bytes and add them to the buffer.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   112
 * Returns the number of bytes received.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   113
 * Automatically closes the socket if an error occurs
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   114
 * and sets sock=NULL.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   115
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   116
static int receiveToBuffer(flib_netbase *net) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   117
    uint8_t buffer[256];
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   118
    if(!net->sock) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   119
        return 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   120
    } else if(flib_vector_size(net->readBuffer) > NET_READBUFFER_LIMIT) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   121
        flib_log_e("Net connection closed: Net message too big");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   122
        flib_socket_close(net->sock);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   123
        net->sock = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   124
        return 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   125
    } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   126
        int size = flib_socket_nbrecv(net->sock, buffer, sizeof(buffer));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   127
        if(size>=0 && !flib_vector_append(net->readBuffer, buffer, size)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   128
            return size;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   129
        } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   130
            flib_socket_close(net->sock);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   131
            net->sock = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   132
            return 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   133
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   134
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   135
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   136
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   137
flib_netmsg *flib_netbase_recv_message(flib_netbase *net) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   138
    if(log_badargs_if(net==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   139
        return NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   140
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   141
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   142
    flib_netmsg *msg;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   143
    while(!(msg=parseMessage(net->readBuffer))
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   144
            && receiveToBuffer(net)) {}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   145
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   146
    if(msg) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   147
        return msg;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   148
    } else if(!net->sock && flib_vector_size(net->readBuffer)>0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   149
        // Connection is down and we didn't get a complete message, just flush the rest.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   150
        flib_vector_resize(net->readBuffer, 0);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   151
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   152
    return NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   153
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   154
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   155
static void logSentMsg(const uint8_t *data, size_t len) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   156
    if(flib_log_isActive(FLIB_LOGLEVEL_DEBUG)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   157
        flib_log_d("[NET OUT][%03u]%*.*s",(unsigned)len, (unsigned)len, (unsigned)len, data);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   158
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   159
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   160
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   161
int flib_netbase_send_raw(flib_netbase *net, const void *data, size_t len) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   162
    if(log_badargs_if2(net==NULL, data==NULL && len>0)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   163
        return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   164
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   165
    if(!net->sock) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   166
        flib_log_w("flib_netbase_send_raw: Not connected.");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   167
        return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   168
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   169
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   170
    if(flib_socket_send(net->sock, data, len) == len) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   171
        logSentMsg(data, len);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   172
        return 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   173
    } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   174
        flib_log_w("Failed or incomplete write: net connection lost.");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   175
        flib_socket_close(net->sock);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   176
        net->sock = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   177
        return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   178
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   179
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   180
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   181
int flib_netbase_send_message(flib_netbase *net, const flib_netmsg *msg) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   182
    if(log_badargs_if2(net==NULL, msg==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   183
        return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   184
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   185
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   186
    size_t totalSize = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   187
    for(int i=0; i<msg->partCount; i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   188
        totalSize += strlen(msg->parts[i]) + 1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   189
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   190
    totalSize++; // Last part ends in two '\n' instead of one
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   191
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   192
    uint8_t *buffer = flib_malloc(totalSize);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   193
    if(!buffer) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   194
        return -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   195
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   196
    size_t pos = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   197
    for(int i=0; i<msg->partCount; i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   198
        size_t partsize = strlen(msg->parts[i]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   199
        memcpy(buffer+pos, msg->parts[i], partsize);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   200
        pos += partsize;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   201
        buffer[pos++] = '\n';
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   202
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   203
    buffer[pos++] = '\n';
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   204
    return flib_netbase_send_raw(net, buffer, pos);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   205
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   206
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   207
int flib_netbase_sendf(flib_netbase *net, const char *format, ...) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   208
    int result = -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   209
    if(!log_badargs_if2(net==NULL, format==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   210
        va_list argp;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   211
        va_start(argp, format);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   212
        char *buffer = flib_vasprintf(format, argp);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   213
        if(buffer) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   214
            result = flib_netbase_send_raw(net, buffer, strlen(buffer));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   215
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   216
        free(buffer);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   217
        va_end(argp);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   218
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   219
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   220
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   221
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   222
flib_netmsg *flib_netmsg_create() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   223
    flib_netmsg *result = flib_calloc(1, sizeof(flib_netmsg));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   224
    if(result) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   225
        result->partCount = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   226
        result->parts = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   227
        return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   228
    } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   229
        return NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   230
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   231
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   232
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   233
void flib_netmsg_destroy(flib_netmsg *msg) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   234
    if(msg) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   235
        for(int i=0; i<msg->partCount; i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   236
            free(msg->parts[i]);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   237
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   238
        free(msg->parts);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   239
        free(msg);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   240
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   241
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   242
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   243
int flib_netmsg_append_part(flib_netmsg *msg, const void *part, size_t partlen) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   244
    int result = -1;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   245
    if(!log_badargs_if2(msg==NULL, part==NULL && partlen>0)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   246
        char **newParts = realloc(msg->parts, (msg->partCount+1)*sizeof(*msg->parts));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   247
        if(newParts) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   248
            msg->parts = newParts;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   249
            msg->parts[msg->partCount] = flib_malloc(partlen+1);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   250
            if(msg->parts[msg->partCount]) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   251
                memcpy(msg->parts[msg->partCount], part, partlen);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   252
                msg->parts[msg->partCount][partlen] = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   253
                msg->partCount++;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   254
                result = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   255
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   256
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   257
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   258
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
   259
}