#ifndef _UPD_T_H_
#define _UPD_T_H_
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <ctype.h>
#ifdef WIN32
#include <winsock2.h>
#include <windows.h>
#else
#include <pthread.h>
#endif

#include <ctype.h>
#ifndef WIN32
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/time.h>
#ifndef __USE_MISC
#define __USE_MISC
#endif
#ifndef CRTSCTS
#define CRTSCTS  020000000000
#endif
#include <errno.h>
#include <termios.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>
#endif

#include <functional>

#ifdef __cplusplus
extern "C" {
#endif

#ifdef WIN_DLL
#define EXPORT __declspec(dllexport) /* for Windows DLL */
#else
#define EXPORT
#endif

#ifdef WIN32
#define dev_t               HANDLE
#define socket_t            SOCKET
typedef int socklen_t;
#else
#define dev_t               int
#define socket_t            int
#define closesocket         close
#endif

#ifndef MAXSTRPATH
#define MAXSTRPATH  1024                /* max length of stream path */
#endif

#ifndef UDPBUFSIZE
#define UDPBUFSIZE  4096                /* max length of stream path */
#endif


typedef struct {             /* udp type */
    int state;               /* state (0:close,1:open) */
    int type;                /* type (0:server,1:client) */
    int port;                /* port */
    char saddr[256];         /* address (server:filter,client:server) */
    struct sockaddr_in addr; /* address resolved */
    socket_t sock;           /* socket descriptor */
} udp_t;

typedef struct thread_udpsvr_arg{
    char    path[MAXSTRPATH];      // path
    std::function<void(const unsigned char*, const int)>	handle;

} thread_udpsvr_arg;

// udp sever
extern udp_t *openudpsvr(const char *path, char *msg);
EXPORT void closeudpsvr(udp_t *udpsvr);
EXPORT int readudpsvr(udp_t *udpsvr, unsigned char *buff, int n, char *msg);
EXPORT int stateudpsvr(udp_t *udpsvr);
EXPORT int statexudpsvr(udp_t *udpsvr, char *msg);

// udp client
extern udp_t *openudpcli(const char *path, char *msg);
EXPORT void closeudpcli(udp_t *udpcli);
EXPORT int writeudpcli(udp_t *udpcli, unsigned char *buff, int n, char *msg);
EXPORT int stateudpcli(udp_t *udpcli);
EXPORT int statexudpcli(udp_t *udpcli, char *msg);

#ifdef __cplusplus
}
#endif

#endif // _UPD_T_H_