#ifndef _OSSDK_TYPE_H_
#define _OSSDK_TYPE_H_

#include "util_type.h"

#include <stdio.h>
#include <stdlib.h>


#if defined(__WIN__)
typedef struct {
	HANDLE hthread;
} LThread1;

typedef struct {
	CRITICAL_SECTION section;
} LMutex1; 
#elif defined(IOS) || defined(ANDROID)
#include <pthread.h>
#include <errno.h>
#include <stddef.h>
#include <stdarg.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <dirent.h>
typedef struct {
    pthread_t thr;
} LThread1;

typedef struct {
    pthread_mutex_t mutex;
} LMutex1;
#else
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/epoll.h>
#include <sys/termios.h>
#include <pthread.h>
#include <errno.h>
#include <stddef.h>
#include <stdarg.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <linux/unistd.h>
#include <netdb.h>
#include <dirent.h>
#include <iconv.h>
typedef struct {
    pthread_t thr;
} LThread1;

typedef struct {
    pthread_mutex_t mutex;
} LMutex1;
#endif


typedef struct _DList DList;            /**< 双向链表 */
typedef struct _DListNode DListNode;    /**< 双向链表节点 */

#ifdef __WIN__
// C4200b.cpp
// compile by using: cl /W4 c4200a.cpp
#pragma warning(disable : 4200)
#endif

struct _DListNode {
	DListNode* next, * prev;
	char data[0];
};

struct _DList {
	DListNode* head;
	DListNode* end;
	uint32 node_size;
	uint32 len;
	LMutex1 mutex;
};

#define LINK_TYPE_UNKNOW		0
#define LINK_TYPE_NVSSDK		1
#define LINK_TYPE_PROXY			2
#define LINK_TYPE_REG_NVS		3
#define LINK_TYPE_REG_PROXY		4
#define LINK_TYPE_REG_CLIENT	5

#define DATA_TYPE_COMMAND		0 //NetRecvProtocolFunc type
#define DATA_TYPE_DATA			1

#define OBJ_TYPE_SERVER			0	//类型TcpServer
#define OBJ_TYPE_CLIENT			1	//类型TcpClient
#define OBJ_TYPE_TIMER			2	//类型Timer
#define OBJ_TYPE_POST			3	//类型Post
#define OBJ_TYPE_UDP            4   //类型Udp

#define LINK_REPEATE			999			

#ifndef MAX_IP_LEN
#define MAX_IP_LEN 16	/**< 最大IP地址长度 */
#endif

#define LINK_TIMEOUT 30 //link层超时30s

#define TCP_SERVER_STATUS_DISLISTEN	0   /**< TcpServer状态-没有监听 */
#define TCP_SERVER_STATUS_LISTEN	1   /**< TcpServer状态-监听 */

#define TCP_CLIENT_STATUS_DISCONN		0       /**< TcpClient状态-断开连接 */
#define TCP_CLIENT_STATUS_CONNECTING	1       /**< TcpClient状态-连接中 */
#define TCP_CLIENT_STATUS_CONNECT		2       /**< TcpClient状态-连接 */
#define TIMER_MIN						1000    /**< Timer最短间隔 1000毫秒 */

typedef struct _NetTcpServer NetTcpServer;		/**< Tcp服务器 */
typedef struct _NetTcpClient NetTcpClient;		/**< Tcp客户端 */
typedef void (CALLBACK* NetClientConnectFunc)(NetTcpClient* client, void* udata);
typedef void (CALLBACK* NetClientDisconnectFunc)(NetTcpClient* client, void* udata);
typedef int32 (CALLBACK* NetClientReadFunc)(NetTcpClient* client, void* udata);
typedef int32 (CALLBACK* NetClientWriteFunc)(NetTcpClient* client, void* udata);
typedef void (CALLBACK* NetServerAcceptFunc)(NetTcpServer* server, NetTcpClient* client, void* udata);

typedef struct _NetLink NetLink;				/**< Sdk连接 */
typedef int (CALLBACK* NetRecvProtocolFunc)(NetLink* link, void* udata, int type, void* package, int len);
typedef void (CALLBACK* NetSendProtocolFunc)(NetLink* link, void* udata);
typedef void (CALLBACK* NetDisconnectProtocolFunc)(NetLink* link, void* udata);
typedef void (CALLBACK* PostFunc)(void* context);
typedef void (CALLBACK* OsTimerFunc)(void* context);
struct _LTimer {
	OsTimerFunc func;
	uint32 tm;
	void* cxt;
	uint32 index;
	DListNode* node;
};
typedef struct _LTimer LTimer;          /**< 定时器 */

typedef struct {
	PostFunc func;
	void* context;
} LMainThreadFunc;

#ifndef DATAPACKHEAD
#define  DATAPACKHEAD			0xF9EAF5F1    //数据包包头 最后4位为版本号
#define  COMMPACKHEAD			0xF5EAF5F1    //命令包包头
#define STREAMSIZE              1440
#define COMMANDSIZE             1440
#endif


#define MAX_NET_PROTOCOL_ARRAY	256
#define MAX_PACKAGE_LEN			512
#define MAX_PROTOCOL_LEN		(MAX_NET_PROTOCOL_ARRAY*MAX_PACKAGE_LEN+20)
#define FRAME_FEOF_LEN	8

//数据通道
typedef struct _NetStreamHead
{
	uint32    head;			//数据包包头
	uint32    serial;       //序列号
	uint32	  source;	    //数据包源地址
	uint16    channel;      //通道号
	uint16    size;			//数据长度
	uint32    stream_type;  //预留--流类型: 0对讲,1视频
} NetStreamHead;

//命令包包头
typedef struct _NetCommandHead
{
	uint32     head;		//命令包包头
	uint16	   serial;	    //序列号,用来判断是否丢包
	uint16     size;        //命令总长度
	uint32	   target;		//命令包目的地，如果为全1则是广播包
	uint32	   source;	    //命令包源地址
	uint16	   order;		//命令码
	uint16	   para_count;	//参数的长度
} NetCommandHead;

typedef struct _NetPackageHead
{
	union
	{
		NetStreamHead stream;
		NetCommandHead command;
	};
} NetPackageHead;

#endif

