2016-01-28 16:44:47 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _STIO_TCP_H_
|
|
|
|
#define _STIO_TCP_H_
|
|
|
|
|
|
|
|
#include <stio.h>
|
2016-02-02 15:40:09 +00:00
|
|
|
#include <stio-sck.h>
|
2016-01-28 16:44:47 +00:00
|
|
|
|
|
|
|
enum stio_dev_tcp_ioctl_cmd_t
|
|
|
|
{
|
|
|
|
STIO_DEV_TCP_BIND,
|
|
|
|
STIO_DEV_TCP_CONNECT,
|
|
|
|
STIO_DEV_TCP_LISTEN
|
|
|
|
};
|
|
|
|
typedef enum stio_dev_tcp_ioctl_cmd_t stio_dev_tcp_ioctl_cmd_t;
|
|
|
|
|
|
|
|
enum stio_dev_tcp_state_t
|
|
|
|
{
|
|
|
|
STIO_DEV_TCP_CONNECTING = (1 << 0),
|
|
|
|
STIO_DEV_TCP_CONNECTED = (1 << 1),
|
|
|
|
STIO_DEV_TCP_LISTENING = (1 << 2),
|
|
|
|
STIO_DEV_TCP_ACCEPTED = (1 << 3)
|
|
|
|
};
|
|
|
|
typedef enum stio_dev_tcp_state_t stio_dev_tcp_state_t;
|
|
|
|
|
|
|
|
typedef struct stio_dev_tcp_t stio_dev_tcp_t;
|
|
|
|
|
2016-02-02 15:40:09 +00:00
|
|
|
typedef int (*stio_dev_tcp_on_connect_t) (stio_dev_tcp_t* dev);
|
|
|
|
typedef void (*stio_dev_tcp_on_disconnect_t) (stio_dev_tcp_t* dev);
|
2016-01-28 16:44:47 +00:00
|
|
|
|
2016-02-02 15:40:09 +00:00
|
|
|
typedef int (*stio_dev_tcp_on_read_t) (stio_dev_tcp_t* dev, const void* data, stio_len_t len);
|
2016-02-05 13:25:59 +00:00
|
|
|
typedef int (*stio_dev_tcp_on_write_t) (stio_dev_tcp_t* dev, stio_len_t wrlen, void* wrctx);
|
2016-01-28 16:44:47 +00:00
|
|
|
|
|
|
|
struct stio_dev_tcp_t
|
|
|
|
{
|
|
|
|
STIO_DEV_HEADERS;
|
|
|
|
|
|
|
|
stio_sckhnd_t sck;
|
|
|
|
|
2016-01-30 19:08:28 +00:00
|
|
|
/* bitwised-ORed of #stio_dev_tcp_state_t enumerators */
|
|
|
|
int state;
|
2016-01-28 16:44:47 +00:00
|
|
|
|
|
|
|
/* peer address - valid if one of the followings is set:
|
|
|
|
* STIO_DEV_TCP_ACCEPTED
|
|
|
|
* STIO_DEV_TCP_CONNECTED
|
|
|
|
* STIO_DEV_TCP_CONNECTING */
|
|
|
|
stio_sckadr_t peer;
|
|
|
|
|
|
|
|
/** return 0 on succes, -1 on failure/
|
|
|
|
* called on a new tcp device for an accepted client or
|
|
|
|
* on a tcp device conntected to a remote server */
|
2016-02-02 15:40:09 +00:00
|
|
|
stio_dev_tcp_on_connect_t on_connect;
|
2016-01-28 16:44:47 +00:00
|
|
|
|
2016-02-02 15:40:09 +00:00
|
|
|
stio_dev_tcp_on_disconnect_t on_disconnect;
|
|
|
|
stio_dev_tcp_on_read_t on_read;
|
|
|
|
stio_dev_tcp_on_write_t on_write;
|
2016-01-30 19:08:28 +00:00
|
|
|
|
|
|
|
stio_tmridx_t tmridx_connect;
|
2016-01-28 16:44:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct stio_dev_tcp_make_t stio_dev_tcp_make_t;
|
|
|
|
struct stio_dev_tcp_make_t
|
|
|
|
{
|
|
|
|
/* TODO: options: REUSEADDR for binding?? */
|
|
|
|
stio_sckadr_t addr; /* binding address. */
|
2016-02-02 15:40:09 +00:00
|
|
|
stio_dev_tcp_on_write_t on_write;
|
|
|
|
stio_dev_tcp_on_read_t on_read;
|
2016-01-28 16:44:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct stio_dev_tcp_bind_t stio_dev_tcp_bind_t;
|
|
|
|
struct stio_dev_tcp_bind_t
|
|
|
|
{
|
2016-01-30 05:24:23 +00:00
|
|
|
int opts; /* TODO: REUSEADDR , TRANSPARENT, etc or someting?? */
|
2016-01-28 16:44:47 +00:00
|
|
|
stio_sckadr_t addr;
|
|
|
|
/* TODO: add device name for BIND_TO_DEVICE */
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct stio_dev_tcp_connect_t stio_dev_tcp_connect_t;
|
|
|
|
struct stio_dev_tcp_connect_t
|
|
|
|
{
|
|
|
|
stio_sckadr_t addr;
|
2016-02-05 13:25:59 +00:00
|
|
|
stio_ntime_t tmout; /* connect timeout */
|
2016-02-02 15:40:09 +00:00
|
|
|
stio_dev_tcp_on_connect_t on_connect;
|
|
|
|
stio_dev_tcp_on_disconnect_t on_disconnect;
|
2016-01-28 16:44:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct stio_dev_tcp_listen_t stio_dev_tcp_listen_t;
|
|
|
|
struct stio_dev_tcp_listen_t
|
|
|
|
{
|
|
|
|
int backlogs;
|
2016-02-02 15:40:09 +00:00
|
|
|
stio_dev_tcp_on_connect_t on_connect; /* optional, but new connections are dropped immediately without this */
|
|
|
|
stio_dev_tcp_on_disconnect_t on_disconnect; /* should on_discconneted be part of on_accept_t??? */
|
2016-01-28 16:44:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct stio_dev_tcp_accept_t stio_dev_tcp_accept_t;
|
|
|
|
struct stio_dev_tcp_accept_t
|
|
|
|
{
|
|
|
|
stio_syshnd_t sck;
|
|
|
|
/* TODO: add timeout */
|
|
|
|
stio_sckadr_t peer;
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2016-01-30 05:24:23 +00:00
|
|
|
STIO_EXPORT stio_dev_tcp_t* stio_dev_tcp_make (
|
2016-01-28 16:44:47 +00:00
|
|
|
stio_t* stio,
|
|
|
|
stio_size_t xtnsize,
|
|
|
|
const stio_dev_tcp_make_t* data
|
|
|
|
);
|
|
|
|
|
2016-01-30 05:24:23 +00:00
|
|
|
STIO_EXPORT int stio_dev_tcp_bind (
|
2016-01-28 16:44:47 +00:00
|
|
|
stio_dev_tcp_t* tcp,
|
|
|
|
stio_dev_tcp_bind_t* bind
|
|
|
|
);
|
|
|
|
|
2016-01-30 05:24:23 +00:00
|
|
|
STIO_EXPORT int stio_dev_tcp_connect (
|
2016-01-28 16:44:47 +00:00
|
|
|
stio_dev_tcp_t* tcp,
|
|
|
|
stio_dev_tcp_connect_t* conn);
|
|
|
|
|
2016-01-30 05:24:23 +00:00
|
|
|
STIO_EXPORT int stio_dev_tcp_listen (
|
2016-01-28 16:44:47 +00:00
|
|
|
stio_dev_tcp_t* tcp,
|
|
|
|
stio_dev_tcp_listen_t* lstn
|
|
|
|
);
|
|
|
|
|
|
|
|
|
2016-02-05 13:25:59 +00:00
|
|
|
#if defined(STIO_HAVE_INLINE)
|
|
|
|
|
|
|
|
static STIO_INLINE int stio_dev_tcp_read (stio_dev_tcp_t* tcp, int enabled)
|
|
|
|
{
|
|
|
|
return stio_dev_read ((stio_dev_t*)tcp, enabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
static STIO_INLINE int stio_dev_tcp_write (stio_dev_tcp_t* tcp, const void* data, stio_len_t len, void* wrctx)
|
|
|
|
{
|
2016-03-29 15:02:01 +00:00
|
|
|
return stio_dev_write ((stio_dev_t*)tcp, data, len, wrctx, STIO_NULL);
|
2016-02-05 13:25:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static STIO_INLINE int stio_dev_tcp_timedwrite (stio_dev_tcp_t* tcp, const void* data, stio_len_t len, const stio_ntime_t* tmout, void* wrctx)
|
|
|
|
{
|
2016-03-29 15:02:01 +00:00
|
|
|
return stio_dev_timedwrite ((stio_dev_t*)tcp, data, len, tmout, wrctx, STIO_NULL);
|
2016-02-05 13:25:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static STIO_INLINE void stio_dev_tcp_halt (stio_dev_tcp_t* tcp)
|
|
|
|
{
|
|
|
|
stio_dev_halt ((stio_dev_t*)tcp);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define stio_dev_tcp_read(tcp,enabled) stio_dev_read((stio_dev_t*)tcp, enabled)
|
2016-03-29 15:02:01 +00:00
|
|
|
#define stio_dev_tcp_write(tcp,data,len,wrctx) stio_dev_write((stio_dev_t*)tcp, data, len, wrctx, STIO_NULL)
|
|
|
|
#define stio_dev_tcp_timedwrite(tcp,data,len,tmout,wrctx) stio_dev_timedwrite((stio_dev_t*)tcp, data, len, tmout, wrctx, STIO_NULL)
|
2016-02-05 13:25:59 +00:00
|
|
|
#define stio_dev_tcp_halt(tcp) stio_dev_halt((stio_dev_t*)tcp)
|
|
|
|
|
|
|
|
#endif
|
2016-02-04 15:06:20 +00:00
|
|
|
|
2016-01-28 16:44:47 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|