great cleaning

This commit is contained in:
2017-10-16 00:29:43 +02:00
parent 87d1841fc8
commit 4ad1f09f73
9 changed files with 161 additions and 69 deletions
+46 -6
View File
@@ -2,10 +2,9 @@
// Created by MightyPork on 2017/10/15.
//
// http://www.thegeekstuff.com/2011/12/c-socket-programming/?utm_source=feedburner
#include "demo.h"
// those magic defines are needed so we can use clone()
#define _GNU_SOURCE
#define __USE_GNU
#include <sched.h>
@@ -20,15 +19,24 @@
volatile int sockfd = -1;
volatile bool conn_disband = false;
/**
* Close socket
*/
void demo_disconn(void)
{
conn_disband = true;
if (sockfd >= 0) close(sockfd);
}
/**
* Demo WriteImpl - send stuff to our peer
*
* @param buff
* @param len
*/
void TF_WriteImpl(const uint8_t *buff, size_t len)
{
printf("\033[32m--- TX %ld bytes ---\033[0m\n", len);
printf("\033[32mTF_WriteImpl - sending frame:\033[0m\n");
dumpFrame(buff, len);
usleep(1000);
@@ -39,6 +47,13 @@ void TF_WriteImpl(const uint8_t *buff, size_t len)
}
}
/**
* Client bg thread
*
* @param unused
* @return unused
*/
static int demo_client(void* unused)
{
(void)unused;
@@ -81,6 +96,12 @@ static int demo_client(void* unused)
return 0;
}
/**
* Server bg thread
*
* @param unused
* @return unused
*/
static int demo_server(void* unused)
{
(void)unused;
@@ -136,20 +157,36 @@ static int demo_server(void* unused)
return 0;
}
void signal_handler(int sig)
/**
* Trap - clean up
*
* @param sig - signal that caused this
*/
static void signal_handler(int sig)
{
(void)sig;
printf("Shutting down...");
demo_disconn();
exit(sig);
exit(sig); // pass the signal through - this is nonstandard behavior but useful for debugging
}
/**
* Sleaping Beauty's fave function
*/
void demo_sleep(void)
{
while(1) usleep(10);
}
void demo_init(TF_PEER peer)
/**
* Start the background thread
*
* Slave is started first and doesn't normally init transactions - but it could
*
* @param peer what peer we are
*/
void demo_init(TF_Peer peer)
{
signal(SIGTERM, signal_handler);
signal(SIGINT, signal_handler);
@@ -163,6 +200,9 @@ void demo_init(TF_PEER peer)
}
printf("Starting %s...\n", peer == TF_MASTER ? "MASTER" : "SLAVE");
// CLONE_VM --- share heap
// CLONE_FILES --- share stdout and stderr
if (peer == TF_MASTER) {
retc = clone(&demo_client, (char *)stack+8192, CLONE_VM|CLONE_FILES, 0);
} else {
+2 -2
View File
@@ -15,9 +15,9 @@
void demo_sleep(void);
/** Init server - DOES NOT init TinyFrame! */
void demo_init(TF_PEER peer);
void demo_init(TF_Peer peer);
/** Disconnect client from the server - claled by a server-side callback */
/** Disconnect client from the server - can be called by a server-side callback */
void demo_disconn(void);
#endif //TF_DEMO_H
+4 -4
View File
@@ -5,18 +5,18 @@
#include <stdio.h>
#include "../demo.h"
bool testIdListener(TF_MSG *msg)
TF_Result testIdListener(TF_Msg *msg)
{
printf("testIdListener()\n");
dumpFrameInfo(msg);
return true;
return TF_CLOSE;
}
bool testGenericListener(TF_MSG *msg)
TF_Result testGenericListener(TF_Msg *msg)
{
printf("testGenericListener()\n");
dumpFrameInfo(msg);
return true;
return TF_STAY;
}
int main(void)
+10 -4
View File
@@ -6,14 +6,14 @@
#include "../demo.h"
#include <memory.h>
bool helloListener(TF_MSG *msg)
TF_Result helloListener(TF_Msg *msg)
{
printf("helloListener()\n");
dumpFrameInfo(msg);
return true;
return TF_STAY;
}
bool replyListener(TF_MSG *msg)
TF_Result replyListener(TF_Msg *msg)
{
printf("replyListener()\n");
dumpFrameInfo(msg);
@@ -21,8 +21,14 @@ bool replyListener(TF_MSG *msg)
msg->len = (TF_LEN) strlen((const char *) msg->data);
TF_Respond(msg);
// unsolicted reply - will not be handled
msg->data = (const uint8_t *) "SPAM";
msg->len = 5;
TF_Respond(msg);
// unrelated message
TF_SendSimple(77, (const uint8_t *) "NAZDAR", 7);
return true;
return TF_STAY;
}
int main(void)
+5 -5
View File
@@ -19,22 +19,22 @@ void TF_WriteImpl(const uint8_t *buff, size_t len)
}
/** An example listener function */
bool myListener(TF_MSG *msg)
TF_Result myListener(TF_Msg *msg)
{
dumpFrameInfo(msg);
return true;
return TF_STAY;
}
bool testIdListener(TF_MSG *msg)
TF_Result testIdListener(TF_Msg *msg)
{
printf("OK - ID Listener triggered for msg!\n");
dumpFrameInfo(msg);
return true;
return TF_CLOSE;
}
void main(void)
{
TF_MSG msg;
TF_Msg msg;
const char *longstr = "Lorem ipsum dolor sit amet.";
// Set up the TinyFrame library
+1 -1
View File
@@ -21,7 +21,7 @@ void dumpFrame(const uint8_t *buff, size_t len)
printf("--- end of frame ---\n\n");
}
void dumpFrameInfo(TF_MSG *msg)
void dumpFrameInfo(TF_Msg *msg)
{
printf("\033[33mFrame info\n"
" type: %02Xh\n"
+12 -1
View File
@@ -8,8 +8,19 @@
#include <stdio.h>
#include "../TinyFrame.h"
/** pointer to unsigned char */
typedef unsigned char* pu8;
/**
* Dump a binary frame as hex, dec and ASCII
*/
void dumpFrame(const uint8_t *buff, size_t len);
void dumpFrameInfo(TF_MSG *msg);
/**
* Dump message metadata (not the content)
*
* @param msg
*/
void dumpFrameInfo(TF_Msg *msg);
#endif //TF_UTILS_H