You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
124 lines
2.8 KiB
124 lines
2.8 KiB
/////////////////////////////////////////////////////////////////////////////////
|
|
//
|
|
// tcp.h is part of TestModbus-Server.
|
|
//
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
#ifndef _TCP_H_
|
|
#define _TCP_H_
|
|
|
|
#if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#define _NTDDI_VERSION_FROM_WIN32_WINNT2(ver) ver##0000
|
|
#define _NTDDI_VERSION_FROM_WIN32_WINNT(ver) _NTDDI_VERSION_FROM_WIN32_WINNT2(ver)
|
|
|
|
#ifndef _WIN32_WINNT
|
|
# define _WIN32_WINNT 0x501
|
|
#endif
|
|
#ifndef NTDDI_VERSION
|
|
# define NTDDI_VERSION _NTDDI_VERSION_FROM_WIN32_WINNT(_WIN32_WINNT)
|
|
#endif
|
|
|
|
// #include <winsock.h>
|
|
#include <winsock2.h>
|
|
#include <io.h>
|
|
#include <ws2tcpip.h>
|
|
#include <string.h>
|
|
|
|
#define socklen_t size_t
|
|
|
|
#ifndef bzero
|
|
#define bzero(__z__, __x__) memset (__z__, 0x0, __x__)
|
|
#endif
|
|
|
|
#ifndef MSG_NOSIGNAL
|
|
# define MSG_NOSIGNAL 0
|
|
#endif
|
|
|
|
#else
|
|
|
|
#include <sys/types.h>
|
|
#include <sys/socket.h>
|
|
#include <netinet/in.h>
|
|
#include <arpa/inet.h>
|
|
#include <netdb.h>
|
|
#include <sys/un.h>
|
|
|
|
#endif
|
|
|
|
#include <string>
|
|
|
|
using namespace std;
|
|
|
|
#define SOCKET int
|
|
|
|
#define NET_HOSTLEN 256
|
|
#define NET_PORTLEN 6
|
|
#define NET_BUFFERSIZE 4096
|
|
#define NET_MAX_RETRY 5 // retry to send data EINTR
|
|
#define NET_MAX_TIMEOUT 30000 // timeout in ms
|
|
|
|
|
|
/************************************************************************
|
|
*
|
|
* global functions needed for networking
|
|
*
|
|
*/
|
|
int dns_filladdr (string host, string port, int ai_family,
|
|
struct sockaddr_storage *sAddr);
|
|
char *itoa(char* buffer, int number, int size);
|
|
void UDPTCPNetwork_Startup();
|
|
extern int UDPTCPNetwork_init;
|
|
|
|
|
|
/************************************************************************
|
|
* tcp related functions
|
|
*/
|
|
class TCP {
|
|
private:
|
|
SOCKET sock;
|
|
struct sockaddr_storage peeraddr;
|
|
string remote_host;
|
|
string remote_port;
|
|
int readcnt;
|
|
int writecnt;
|
|
int islisten;
|
|
|
|
int WebHeaderGetParamValue(std::string line, std::string *parm, std::string *value);
|
|
public:
|
|
TCP();
|
|
TCP(SOCKET s);
|
|
TCP(string h, string p);
|
|
TCP(string hostport, int defaultport);
|
|
~TCP();
|
|
|
|
int Connect();
|
|
int Connect(string h, string p);
|
|
int Connect(string hostport, int defaultport);
|
|
long int ReadPop (char *buffer, long int pktlen, long int bufferlen);
|
|
long int ReadTimeout(char *buffer, long int len, int timeout);
|
|
long int Read(char *buffer, long int len);
|
|
long int Write(char *buffer, long int len);
|
|
void Close();
|
|
int IsConnected();
|
|
int IsData(int timeout); // timeout in ms;
|
|
int IsListen() { return islisten; };
|
|
|
|
int Listen(int port);
|
|
TCP* Accept();
|
|
|
|
SOCKET GetSocket() { return sock; };
|
|
void SetSocket(SOCKET s, struct sockaddr_storage *saddr, socklen_t saddrlen);
|
|
|
|
const string GetRemoteAddr();
|
|
const string GetLocalAddr();
|
|
|
|
int WebGetURLElements (string url, string *host, string *port, string *file);
|
|
int WebGetFile (string url, char *buffer, int maxsize, char *formdata);
|
|
};
|
|
|
|
#endif
|
|
|