2016-01-26 16:07:52 +00:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
Copyright (c) 2015-2016 Chung, Hyung-Hwan. All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
|
|
|
|
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WAfRRANTIES
|
|
|
|
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2016-01-28 16:44:47 +00:00
|
|
|
#include <stio.h>
|
|
|
|
#include <stio-tcp.h>
|
|
|
|
#include <stio-udp.h>
|
|
|
|
|
2016-01-26 16:02:20 +00:00
|
|
|
#include <stdlib.h>
|
2016-01-30 05:24:23 +00:00
|
|
|
#include <string.h>
|
2016-01-26 16:02:20 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
struct mmgr_stat_t
|
|
|
|
{
|
|
|
|
stio_size_t total_count;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct mmgr_stat_t mmgr_stat_t;
|
|
|
|
|
|
|
|
static mmgr_stat_t mmgr_stat;
|
|
|
|
|
2016-01-26 16:02:20 +00:00
|
|
|
static void* mmgr_alloc (stio_mmgr_t* mmgr, stio_size_t size)
|
|
|
|
{
|
2016-02-04 15:06:20 +00:00
|
|
|
if (((mmgr_stat_t*)mmgr->ctx)->total_count > 100)
|
|
|
|
{
|
|
|
|
printf ("CRITICAL ERROR ---> too many heap chunks...\n");
|
|
|
|
return STIO_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* x = malloc (size);
|
|
|
|
if (x) ((mmgr_stat_t*)mmgr->ctx)->total_count++;
|
|
|
|
return x;
|
2016-01-26 16:02:20 +00:00
|
|
|
}
|
|
|
|
|
2016-01-30 05:24:23 +00:00
|
|
|
static void* mmgr_realloc (stio_mmgr_t* mmgr, void* ptr, stio_size_t size)
|
|
|
|
{
|
|
|
|
return realloc (ptr, size);
|
|
|
|
}
|
|
|
|
|
2016-01-26 16:02:20 +00:00
|
|
|
static void mmgr_free (stio_mmgr_t* mmgr, void* ptr)
|
|
|
|
{
|
2016-02-04 15:06:20 +00:00
|
|
|
((mmgr_stat_t*)mmgr->ctx)->total_count--;
|
2016-01-26 16:02:20 +00:00
|
|
|
return free (ptr);
|
|
|
|
}
|
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
|
2016-01-26 16:02:20 +00:00
|
|
|
static stio_mmgr_t mmgr =
|
|
|
|
{
|
|
|
|
mmgr_alloc,
|
2016-01-30 05:24:23 +00:00
|
|
|
mmgr_realloc,
|
2016-01-26 16:02:20 +00:00
|
|
|
mmgr_free,
|
2016-02-04 15:06:20 +00:00
|
|
|
&mmgr_stat
|
|
|
|
};
|
|
|
|
|
|
|
|
struct tcp_server_t
|
|
|
|
{
|
|
|
|
int tally;
|
2016-01-26 16:02:20 +00:00
|
|
|
};
|
2016-02-04 15:06:20 +00:00
|
|
|
typedef struct tcp_server_t tcp_server_t;
|
2016-01-26 16:02:20 +00:00
|
|
|
|
2016-02-02 15:40:09 +00:00
|
|
|
static void tcp_on_disconnect (stio_dev_tcp_t* tcp)
|
2016-01-26 16:02:20 +00:00
|
|
|
{
|
2016-01-30 05:24:23 +00:00
|
|
|
if (tcp->state & STIO_DEV_TCP_CONNECTING)
|
|
|
|
{
|
|
|
|
printf ("TCP DISCONNECTED - FAILED TO CONNECT (%d) TO REMOTE SERVER\n", tcp->sck);
|
|
|
|
}
|
|
|
|
else if (tcp->state & STIO_DEV_TCP_LISTENING)
|
2016-01-26 16:02:20 +00:00
|
|
|
{
|
|
|
|
printf ("SHUTTING DOWN THE SERVER SOCKET(%d)...\n", tcp->sck);
|
|
|
|
}
|
|
|
|
else if (tcp->state & STIO_DEV_TCP_CONNECTED)
|
|
|
|
{
|
|
|
|
printf ("CLIENT ORIGINATING FROM HERE GOT DISCONNECTED(%d).......\n", tcp->sck);
|
|
|
|
}
|
|
|
|
else if (tcp->state & STIO_DEV_TCP_ACCEPTED)
|
|
|
|
{
|
|
|
|
printf ("CLIENT BEING SERVED GOT DISCONNECTED(%d).......\n", tcp->sck);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf ("TCP DISCONNECTED - THIS MUST NOT HAPPEN (%d)\n", tcp->sck);
|
|
|
|
}
|
|
|
|
}
|
2016-02-02 15:40:09 +00:00
|
|
|
static int tcp_on_connect (stio_dev_tcp_t* tcp)
|
2016-01-26 16:02:20 +00:00
|
|
|
{
|
2016-01-28 16:44:47 +00:00
|
|
|
|
|
|
|
if (tcp->state & STIO_DEV_TCP_CONNECTED)
|
|
|
|
{
|
|
|
|
printf ("device connected to a remote server... .asdfjkasdfkljasdlfkjasdj...\n");
|
|
|
|
}
|
|
|
|
else if (tcp->state & STIO_DEV_TCP_ACCEPTED)
|
|
|
|
{
|
|
|
|
printf ("device accepted client device... .asdfjkasdfkljasdlfkjasdj...\n");
|
|
|
|
}
|
|
|
|
|
2016-02-02 15:40:09 +00:00
|
|
|
return stio_dev_tcp_write (tcp, "hello", 5, STIO_NULL);
|
2016-01-26 16:02:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-02 15:40:09 +00:00
|
|
|
static int tcp_on_write (stio_dev_tcp_t* tcp, void* wrctx)
|
2016-01-28 16:44:47 +00:00
|
|
|
{
|
2016-02-04 15:06:20 +00:00
|
|
|
tcp_server_t* ts;
|
|
|
|
|
|
|
|
ts = (tcp_server_t*)(tcp + 1);
|
|
|
|
printf (">>> TCP SENT MESSAGE %d\n", ts->tally);
|
|
|
|
|
|
|
|
ts->tally++;
|
|
|
|
// if (ts->tally >= 2) stio_dev_tcp_halt (tcp);
|
|
|
|
|
|
|
|
//stio_dev_tcp_read (tcp);
|
|
|
|
|
|
|
|
printf ("ENABLING READING..............................\n");
|
|
|
|
stio_dev_read (tcp, 1);
|
2016-01-28 16:44:47 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-02 15:40:09 +00:00
|
|
|
static int tcp_on_read (stio_dev_tcp_t* tcp, const void* buf, stio_len_t len)
|
2016-01-28 16:44:47 +00:00
|
|
|
{
|
2016-02-04 15:06:20 +00:00
|
|
|
if (len <= 0)
|
|
|
|
{
|
|
|
|
printf ("STREAM DEVICE: EOF RECEIVED...\n");
|
|
|
|
/* no outstanding request. but EOF */
|
|
|
|
stio_dev_tcp_halt (tcp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf ("on read %d\n", (int)len);
|
|
|
|
|
2016-02-02 07:51:17 +00:00
|
|
|
int n;
|
|
|
|
static char a ='A';
|
|
|
|
char* xxx = malloc (1000000);
|
|
|
|
memset (xxx, a++ ,1000000);
|
2016-02-02 15:40:09 +00:00
|
|
|
//return stio_dev_tcp_write (tcp, "HELLO", 5, STIO_NULL);
|
|
|
|
n = stio_dev_tcp_write (tcp, xxx, 1000000, STIO_NULL);
|
2016-02-02 07:51:17 +00:00
|
|
|
free (xxx);
|
2016-02-04 15:06:20 +00:00
|
|
|
|
|
|
|
if (n <= -1) return -1;
|
|
|
|
|
|
|
|
/* post the write finisher */
|
|
|
|
n = stio_dev_tcp_write (tcp, STIO_NULL, 0, STIO_NULL);
|
|
|
|
if (n <= -1) return -1;
|
|
|
|
|
|
|
|
printf ("DISABLING READING..............................\n");
|
|
|
|
stio_dev_read (tcp, 0);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* return 1; let the main loop to read more greedily without consulint the multiplexer */
|
2016-01-28 16:44:47 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 16:02:20 +00:00
|
|
|
static stio_t* g_stio;
|
|
|
|
|
|
|
|
static void handle_signal (int sig)
|
|
|
|
{
|
|
|
|
if (g_stio) stio_stop (g_stio);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main ()
|
|
|
|
{
|
|
|
|
|
|
|
|
stio_t* stio;
|
|
|
|
stio_dev_udp_t* udp;
|
2016-01-30 05:24:23 +00:00
|
|
|
stio_dev_tcp_t* tcp[2];
|
2016-01-26 16:02:20 +00:00
|
|
|
struct sockaddr_in sin;
|
|
|
|
struct sigaction sigact;
|
|
|
|
stio_dev_tcp_connect_t tcp_conn;
|
|
|
|
stio_dev_tcp_listen_t tcp_lstn;
|
2016-01-28 16:44:47 +00:00
|
|
|
stio_dev_tcp_make_t tcp_make;
|
2016-02-04 15:06:20 +00:00
|
|
|
tcp_server_t* ts;
|
2016-01-26 16:02:20 +00:00
|
|
|
|
2016-01-30 05:24:23 +00:00
|
|
|
stio = stio_open (&mmgr, 0, 512, STIO_NULL);
|
2016-01-26 16:02:20 +00:00
|
|
|
if (!stio)
|
|
|
|
{
|
|
|
|
printf ("Cannot open stio\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_stio = stio;
|
|
|
|
|
2016-01-30 05:24:23 +00:00
|
|
|
memset (&sigact, 0, STIO_SIZEOF(sigact));
|
2016-01-26 16:02:20 +00:00
|
|
|
sigact.sa_flags = SA_RESTART;
|
|
|
|
sigact.sa_handler = handle_signal;
|
|
|
|
sigaction (SIGINT, &sigact, STIO_NULL);
|
|
|
|
|
2016-02-02 07:51:17 +00:00
|
|
|
memset (&sigact, 0, STIO_SIZEOF(sigact));
|
|
|
|
sigact.sa_handler = SIG_IGN;
|
|
|
|
sigaction (SIGPIPE, &sigact, STIO_NULL);
|
2016-01-28 17:32:58 +00:00
|
|
|
|
2016-02-02 07:51:17 +00:00
|
|
|
/*memset (&sin, 0, STIO_SIZEOF(sin));
|
2016-01-26 16:02:20 +00:00
|
|
|
sin.sin_family = AF_INET;
|
2016-02-02 07:51:17 +00:00
|
|
|
sin.sin_port = htons(1234); */
|
2016-01-26 16:02:20 +00:00
|
|
|
/*
|
|
|
|
udp = (stio_dev_udp_t*)stio_makedev (stio, STIO_SIZEOF(*udp), &udp_mth, &udp_evcb, &sin);
|
|
|
|
if (!udp)
|
|
|
|
{
|
|
|
|
printf ("Cannot make udp\n");
|
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2016-01-30 05:24:23 +00:00
|
|
|
memset (&sin, 0, STIO_SIZEOF(sin));
|
2016-01-26 16:02:20 +00:00
|
|
|
sin.sin_family = AF_INET;
|
2016-01-30 19:08:28 +00:00
|
|
|
memset (&tcp_make, 0, STIO_SIZEOF(&tcp_make));
|
2016-01-30 05:24:23 +00:00
|
|
|
memcpy (&tcp_make.addr, &sin, STIO_SIZEOF(sin));
|
2016-02-02 15:40:09 +00:00
|
|
|
tcp_make.on_write = tcp_on_write;
|
|
|
|
tcp_make.on_read = tcp_on_read;
|
2016-02-04 15:06:20 +00:00
|
|
|
tcp[0] = stio_dev_tcp_make (stio, STIO_SIZEOF(tcp_server_t), &tcp_make);
|
2016-01-30 05:24:23 +00:00
|
|
|
if (!tcp[0])
|
2016-01-26 16:02:20 +00:00
|
|
|
{
|
|
|
|
printf ("Cannot make tcp\n");
|
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
ts = (tcp_server_t*)(tcp[0] + 1);
|
|
|
|
ts->tally = 0;
|
2016-01-30 19:08:28 +00:00
|
|
|
|
|
|
|
memset (&sin, 0, STIO_SIZEOF(sin));
|
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
sin.sin_port = htons(9999);
|
2016-02-04 15:06:20 +00:00
|
|
|
//inet_pton (sin.sin_family, "192.168.1.1", &sin.sin_addr);
|
|
|
|
inet_pton (sin.sin_family, "127.0.0.1", &sin.sin_addr);
|
2016-01-30 19:08:28 +00:00
|
|
|
|
|
|
|
memset (&tcp_conn, 0, STIO_SIZEOF(tcp_conn));
|
|
|
|
memcpy (&tcp_conn.addr, &sin, STIO_SIZEOF(sin));
|
|
|
|
tcp_conn.timeout.sec = 5;
|
2016-02-02 15:40:09 +00:00
|
|
|
tcp_conn.on_connect = tcp_on_connect;
|
|
|
|
tcp_conn.on_disconnect = tcp_on_disconnect;
|
2016-01-30 05:24:23 +00:00
|
|
|
if (stio_dev_tcp_connect (tcp[0], &tcp_conn) <= -1)
|
2016-01-26 16:02:20 +00:00
|
|
|
{
|
|
|
|
printf ("stio_dev_tcp_connect() failed....\n");
|
|
|
|
goto oops;
|
|
|
|
}
|
2016-01-28 16:44:47 +00:00
|
|
|
|
2016-01-30 19:08:28 +00:00
|
|
|
memset (&sin, 0, STIO_SIZEOF(sin));
|
|
|
|
sin.sin_family = AF_INET;
|
2016-01-30 05:24:23 +00:00
|
|
|
sin.sin_port = htons(1234);
|
2016-01-30 19:08:28 +00:00
|
|
|
memset (&tcp_make, 0, STIO_SIZEOF(&tcp_make));
|
2016-01-30 05:24:23 +00:00
|
|
|
memcpy (&tcp_make.addr, &sin, STIO_SIZEOF(sin));
|
2016-02-02 15:40:09 +00:00
|
|
|
tcp_make.on_write = tcp_on_write;
|
|
|
|
tcp_make.on_read = tcp_on_read;
|
2016-01-28 16:44:47 +00:00
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
tcp[1] = stio_dev_tcp_make (stio, STIO_SIZEOF(tcp_server_t), &tcp_make);
|
2016-01-30 05:24:23 +00:00
|
|
|
if (!tcp[1])
|
2016-01-26 16:02:20 +00:00
|
|
|
{
|
|
|
|
printf ("Cannot make tcp\n");
|
|
|
|
goto oops;
|
|
|
|
}
|
2016-02-04 15:06:20 +00:00
|
|
|
ts = (tcp_server_t*)(tcp[1] + 1);
|
|
|
|
ts->tally = 0;
|
2016-01-26 16:02:20 +00:00
|
|
|
|
|
|
|
tcp_lstn.backlogs = 100;
|
2016-02-02 15:40:09 +00:00
|
|
|
tcp_lstn.on_connect = tcp_on_connect;
|
|
|
|
tcp_lstn.on_disconnect = tcp_on_disconnect;
|
2016-01-30 05:24:23 +00:00
|
|
|
if (stio_dev_tcp_listen (tcp[1], &tcp_lstn) <= -1)
|
2016-01-26 16:02:20 +00:00
|
|
|
{
|
|
|
|
printf ("stio_dev_tcp_listen() failed....\n");
|
|
|
|
goto oops;
|
|
|
|
}
|
2016-01-26 16:07:52 +00:00
|
|
|
|
2016-01-26 16:02:20 +00:00
|
|
|
|
|
|
|
stio_loop (stio);
|
|
|
|
|
|
|
|
g_stio = STIO_NULL;
|
|
|
|
stio_close (stio);
|
2016-01-30 05:24:23 +00:00
|
|
|
|
2016-01-26 16:02:20 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
oops:
|
|
|
|
g_stio = STIO_NULL;
|
|
|
|
stio_close (stio);
|
|
|
|
return -1;
|
|
|
|
}
|