mirror of
https://github.com/eddyem/BTA_utils.git
synced 2025-12-06 02:35:13 +03:00
fixed for new libusefull_macros
This commit is contained in:
parent
20624adcae
commit
b77cf95e87
@ -11,7 +11,7 @@ message("VERSION: ${VERSION}")
|
|||||||
|
|
||||||
# list of options
|
# list of options
|
||||||
option(DEBUG "Compile in debug mode" OFF)
|
option(DEBUG "Compile in debug mode" OFF)
|
||||||
option(EXAMPLES "Compile also all examples" ON)
|
#option(EXAMPLES "Compile also all examples" ON)
|
||||||
|
|
||||||
# default flags
|
# default flags
|
||||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -W -Wextra -std=gnu99")
|
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -W -Wextra -std=gnu99")
|
||||||
@ -68,6 +68,6 @@ include(GNUInstallDirs)
|
|||||||
install(TARGETS ${PROJ} DESTINATION "bin")
|
install(TARGETS ${PROJ} DESTINATION "bin")
|
||||||
|
|
||||||
# EXAMPLES
|
# EXAMPLES
|
||||||
if(EXAMPLES)
|
#if(EXAMPLES)
|
||||||
add_subdirectory(examples)
|
# add_subdirectory(examples)
|
||||||
endif()
|
#endif()
|
||||||
|
|||||||
@ -26,7 +26,7 @@
|
|||||||
#include "parsecanmsgs.h"
|
#include "parsecanmsgs.h"
|
||||||
|
|
||||||
static pthread_t clientthread;
|
static pthread_t clientthread;
|
||||||
sl_sock_t *serialsock = NULL;
|
static sl_sock_t *serialsock = NULL, *serversock = NULL;
|
||||||
|
|
||||||
#define CMDIN "in"
|
#define CMDIN "in"
|
||||||
#define CMDOUT "out"
|
#define CMDOUT "out"
|
||||||
@ -56,6 +56,11 @@ static commands allcommands[] = {
|
|||||||
{NULL, 0, 0}
|
{NULL, 0, 0}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void killsockets(){
|
||||||
|
if(serialsock) sl_sock_delete(&serialsock);
|
||||||
|
if(serversock) sl_sock_delete(&serversock);
|
||||||
|
}
|
||||||
|
|
||||||
static int Relay1cmds(CAN_message *msg, char buf[BUFSIZ]){
|
static int Relay1cmds(CAN_message *msg, char buf[BUFSIZ]){
|
||||||
int L = 0;
|
int L = 0;
|
||||||
uint16_t cmd = MSGP_GET_CMD(msg);
|
uint16_t cmd = MSGP_GET_CMD(msg);
|
||||||
@ -68,6 +73,7 @@ static int Relay1cmds(CAN_message *msg, char buf[BUFSIZ]){
|
|||||||
}
|
}
|
||||||
if(!c->textcmd) return 0;
|
if(!c->textcmd) return 0;
|
||||||
DBG("found text cmd is %s (%u)", c->textcmd, data);
|
DBG("found text cmd is %s (%u)", c->textcmd, data);
|
||||||
|
//L = snprintf(buf, BUFSIZ-1, "%s=%u\n", c->textcmd, data);
|
||||||
if(par == NO_PARNO) L = snprintf(buf, BUFSIZ-1, "%s=%u\n", c->textcmd, data);
|
if(par == NO_PARNO) L = snprintf(buf, BUFSIZ-1, "%s=%u\n", c->textcmd, data);
|
||||||
else L = snprintf(buf, BUFSIZ-1, "%s[%d]=%u\n", c->textcmd, par, data);
|
else L = snprintf(buf, BUFSIZ-1, "%s[%d]=%u\n", c->textcmd, par, data);
|
||||||
return L;
|
return L;
|
||||||
@ -87,7 +93,7 @@ static void gotCANans(CAN_message *msg){
|
|||||||
}
|
}
|
||||||
if(L < 1) return;
|
if(L < 1) return;
|
||||||
DBG("BUF: %s", buf);
|
DBG("BUF: %s", buf);
|
||||||
int N = sl_sock_sendall((uint8_t*) buf, L);
|
int N = sl_sock_sendall(serversock, (uint8_t*) buf, L);
|
||||||
green("Send to %d clients\n", N);
|
green("Send to %d clients\n", N);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -204,15 +210,24 @@ static void toomuch(int fd){
|
|||||||
LOGWARN("Client fd=%d tried to connect after MAX reached", fd);
|
LOGWARN("Client fd=%d tried to connect after MAX reached", fd);
|
||||||
}
|
}
|
||||||
// new connections handler
|
// new connections handler
|
||||||
static void connected(sl_sock_t *c){
|
static int connected(sl_sock_t *c){
|
||||||
if(c->type == SOCKT_UNIX) LOGMSG("New client fd=%d connected", c->fd);
|
if(c->type == SOCKT_UNIX) LOGMSG("New client fd=%d connected", c->fd);
|
||||||
else LOGMSG("New client fd=%d, IP=%s connected", c->fd, c->IP);
|
else LOGMSG("New client fd=%d, IP=%s connected", c->fd, c->IP);
|
||||||
|
// here we can change client's IP and return FALSE to close it
|
||||||
|
return TRUE;
|
||||||
}
|
}
|
||||||
// disconnected handler
|
// disconnected handler
|
||||||
static void disconnected(sl_sock_t *c){
|
static void disconnected(sl_sock_t *c){
|
||||||
if(c->type == SOCKT_UNIX) LOGMSG("Disconnected client fd=%d", c->fd);
|
if(c->type == SOCKT_UNIX) LOGMSG("Disconnected client fd=%d", c->fd);
|
||||||
else LOGMSG("Disconnected client fd=%d, IP=%s", c->fd, c->IP);
|
else LOGMSG("Disconnected client fd=%d, IP=%s", c->fd, c->IP);
|
||||||
}
|
}
|
||||||
|
static sl_sock_hresult_e defhandler(struct sl_sock *s, const char *str){
|
||||||
|
if(!s || !str) return RESULT_FAIL;
|
||||||
|
sl_sock_sendstrmessage(s, "You entered wrong command:\n```\n");
|
||||||
|
sl_sock_sendstrmessage(s, str);
|
||||||
|
sl_sock_sendstrmessage(s, "\n```\nTry \"help\"\n");
|
||||||
|
return RESULT_SILENCE;
|
||||||
|
}
|
||||||
|
|
||||||
static sl_sock_hitem_t handlers[] = {
|
static sl_sock_hitem_t handlers[] = {
|
||||||
{dtimeh, "dtime", "get server's UNIX time for all clients connected", NULL},
|
{dtimeh, "dtime", "get server's UNIX time for all clients connected", NULL},
|
||||||
@ -228,13 +243,17 @@ static sl_sock_hitem_t handlers[] = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
sl_sock_t *RunSrv(sl_socktype_e type, const char *node){
|
sl_sock_t *RunSrv(sl_socktype_e type, const char *node){
|
||||||
sl_sock_maxclhandler(toomuch);
|
serversock = sl_sock_run_server(type, node, 4096, handlers);
|
||||||
sl_sock_connhandler(connected);
|
if(!serversock) return NULL;
|
||||||
sl_sock_dischandler(disconnected);
|
sl_sock_maxclhandler(serversock, toomuch);
|
||||||
return sl_sock_run_server(type, node, 4096, handlers);
|
sl_sock_connhandler(serversock, connected);
|
||||||
|
sl_sock_dischandler(serversock, disconnected);
|
||||||
|
sl_sock_defmsghandler(serversock, defhandler);
|
||||||
|
return serversock;
|
||||||
}
|
}
|
||||||
|
|
||||||
sl_sock_t *RunClt(sl_socktype_e type, const char *node){
|
sl_sock_t *RunClt(sl_socktype_e type, const char *node){
|
||||||
|
DBG("run client type %d node %s", type, node);
|
||||||
serialsock = sl_sock_run_client(type, node, 4096);
|
serialsock = sl_sock_run_client(type, node, 4096);
|
||||||
if(!serialsock){
|
if(!serialsock){
|
||||||
DBG("Can't run client");
|
DBG("Can't run client");
|
||||||
|
|||||||
@ -22,3 +22,4 @@
|
|||||||
|
|
||||||
sl_sock_t *RunSrv(sl_socktype_e type, const char *node);
|
sl_sock_t *RunSrv(sl_socktype_e type, const char *node);
|
||||||
sl_sock_t *RunClt(sl_socktype_e type, const char *node);
|
sl_sock_t *RunClt(sl_socktype_e type, const char *node);
|
||||||
|
void killsockets();
|
||||||
|
|||||||
@ -23,22 +23,13 @@
|
|||||||
#include "clientserver.h"
|
#include "clientserver.h"
|
||||||
#include "globopts.h"
|
#include "globopts.h"
|
||||||
|
|
||||||
static sl_sock_t *srv = NULL, *clt = NULL;
|
|
||||||
|
|
||||||
void signals(int sig){
|
void signals(int sig){
|
||||||
if(sig){
|
if(sig){
|
||||||
signal(sig, SIG_IGN);
|
signal(sig, SIG_IGN);
|
||||||
DBG("Get signal %d, quit.\n", sig);
|
DBG("Get signal %d, quit.\n", sig);
|
||||||
LOGERR("Exit with status %d", sig);
|
LOGERR("Exit with status %d", sig);
|
||||||
}else LOGERR("Exit");
|
}else LOGERR("Exit");
|
||||||
if(srv){
|
killsockets();
|
||||||
DBG("Del server");
|
|
||||||
sl_sock_delete(&srv);
|
|
||||||
}
|
|
||||||
if(clt){
|
|
||||||
DBG("Del client");
|
|
||||||
sl_sock_delete(&clt);
|
|
||||||
}
|
|
||||||
exit(sig);
|
exit(sig);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,12 +40,12 @@ int main(int argc, char **argv){
|
|||||||
if(!UserOpts.srvnode) ERRX("Point server node");
|
if(!UserOpts.srvnode) ERRX("Point server node");
|
||||||
if(!UserOpts.cltnode) ERRX("Point serial client node");
|
if(!UserOpts.cltnode) ERRX("Point serial client node");
|
||||||
sl_socktype_e type = (UserOpts.srvunix) ? SOCKT_UNIX : SOCKT_NET;
|
sl_socktype_e type = (UserOpts.srvunix) ? SOCKT_UNIX : SOCKT_NET;
|
||||||
sl_sock_changemaxclients(UserOpts.maxclients);
|
sl_sock_t *srv = RunSrv(type, UserOpts.srvnode);
|
||||||
srv = RunSrv(type, UserOpts.srvnode);
|
|
||||||
if(!srv) ERRX("Server: can't create socket and/or run threads");
|
if(!srv) ERRX("Server: can't create socket and/or run threads");
|
||||||
|
sl_sock_changemaxclients(srv, UserOpts.maxclients);
|
||||||
DBG("Server done");
|
DBG("Server done");
|
||||||
type = (UserOpts.cltunix) ? SOCKT_UNIX : SOCKT_NET;
|
type = (UserOpts.cltunix) ? SOCKT_UNIX : SOCKT_NET;
|
||||||
clt = RunClt(type, UserOpts.cltnode);
|
sl_sock_t *clt = RunClt(type, UserOpts.cltnode);
|
||||||
if(!clt) ERRX("Serial client: can't connect to socket and/or run threads");
|
if(!clt) ERRX("Serial client: can't connect to socket and/or run threads");
|
||||||
DBG("Client done");
|
DBG("Client done");
|
||||||
sl_loglevel_e lvl = UserOpts.verbose + LOGLEVEL_ERR;
|
sl_loglevel_e lvl = UserOpts.verbose + LOGLEVEL_ERR;
|
||||||
@ -75,7 +66,6 @@ int main(int argc, char **argv){
|
|||||||
}
|
}
|
||||||
LOGMSG("End");
|
LOGMSG("End");
|
||||||
DBG("Close");
|
DBG("Close");
|
||||||
if(srv) sl_sock_delete(&srv);
|
killsockets();
|
||||||
if(clt) sl_sock_delete(&clt);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user