Skip to content

Commit 9b2e371

Browse files
committed
netlib
1 parent 80c7bb3 commit 9b2e371

27 files changed

+5691
-0
lines changed

premake.bat

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@echo off
2+
premake5.exe --file=premake.lua vs2019

premake.lua

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
workspace "network"
2+
configurations { "Debug", "Release" }
3+
platforms {'x32', 'x64'}
4+
--If no system is specified, Premake will identify and target the current operating system.
5+
--system { "windows", "linux" }
6+
7+
filter "system:windows"
8+
defines {'_CRT_SECURE_NO_WARNINGS'}
9+
characterset ("MBCS") --Multi-byte Character Set; currently Visual Studio only
10+
11+
12+
-- Debug and Release
13+
filter { "configurations:Debug" }
14+
defines { "DEBUG", "_DEBUG" }
15+
symbols "On"
16+
filter { "configurations:Release" }
17+
defines { "NDEBUG" }
18+
optimize "On"
19+
20+
-- x32 and x64
21+
filter { "platforms:x32" }
22+
architecture "x86"
23+
filter { "platforms:x64" }
24+
architecture "x86_64"
25+
26+
27+
project "net"
28+
location "src"
29+
language "C++"
30+
buildoptions {"-std=c++17"}
31+
cppdialect "c++17"
32+
kind "ConsoleApp"
33+
targetdir "bin"
34+
objdir "obj/net"
35+
local codedir = "src";
36+
files { codedir.."/*.h",codedir.."/*.cpp"}
37+
targetname "net"
38+
filter "system:windows"
39+
local codedir = "src/iocp";
40+
files { codedir.."/*.h",codedir.."/*.cpp"}
41+
filter "system:linux"
42+
local codedir = "src/epoll";
43+
files { codedir.."/*.h",codedir.."/*.cpp"}

premake.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
./premake5 --file=premake.lua --cc=gcc gmake
2+
../make config=release_x64

premake5

1.84 MB
Binary file not shown.

premake5.exe

1.33 MB
Binary file not shown.

src/IPAddres.h

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#pragma once
2+
#include "Platform.h"
3+
4+
namespace net {
5+
extern bool is_ipv4(const char* host);
6+
extern bool is_ipv6(const char* host);
7+
inline bool is_ipaddr(const char* host) {
8+
return is_ipv4(host) || is_ipv6(host);
9+
}
10+
11+
class IPAddres
12+
{
13+
public:
14+
IPAddres(bool isv6 = false);
15+
IPAddres(const char * addr, uint16_t port);
16+
IPAddres(const sockaddr* addr);
17+
IPAddres(const IPAddres& addr);
18+
19+
void operator = (const sockaddr* addr);
20+
void operator = (const IPAddres& addr);
21+
22+
inline void setIpv6(bool b)
23+
{
24+
mbIpv6 = b;
25+
}
26+
27+
inline bool isIpv6()const
28+
{
29+
return mbIpv6;
30+
}
31+
32+
char const* getAddr(void) const;
33+
uint16_t getPort(void) const;
34+
35+
operator sockaddr& ()
36+
{
37+
return mbIpv6 ? *(sockaddr*)&mAddr6 : *(sockaddr*)&mAddr4;
38+
}
39+
40+
operator const sockaddr& (void) const
41+
{
42+
return mbIpv6 ? *(sockaddr*)&mAddr6 : *(sockaddr*)&mAddr4;
43+
}
44+
45+
operator sockaddr* ()
46+
{
47+
return mbIpv6 ? (sockaddr*)&mAddr6 : (sockaddr*)&mAddr4;
48+
}
49+
50+
operator const sockaddr* (void) const
51+
{
52+
return mbIpv6 ? (sockaddr*)&mAddr6 : (sockaddr*)&mAddr4;
53+
}
54+
55+
int addrSizeof() const
56+
{
57+
return mbIpv6 ? sizeof(sockaddr_in6) : sizeof(sockaddr_in);
58+
}
59+
60+
private:
61+
bool mbIpv6 = false;
62+
union
63+
{
64+
sockaddr_in mAddr4;
65+
sockaddr_in6 mAddr6;
66+
};
67+
};
68+
}

src/NetDef.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
#include "Platform.h"
3+
4+
#ifdef _SET_LOG
5+
extern void writeLog(char const* const _Format, ...);
6+
#define WRITE_LOG writeLog
7+
8+
#else
9+
#define WRITE_LOG printf
10+
11+
#endif

src/Network.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#pragma once
2+
#include "Platform.h"
3+
4+
#ifdef WIN_OS
5+
#include "iocp/Accept.h"
6+
#else
7+
#include "epoll/Accept.h"
8+
#endif

src/Platform.h

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#pragma once
2+
3+
#include <stdio.h>
4+
#include <string.h>
5+
6+
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
7+
#define WIN_OS
8+
#include <ws2tcpip.h>
9+
#include <windows.h>
10+
#else
11+
#define LINUX_OS
12+
#include <errno.h>
13+
#include <sys/socket.h>
14+
#include <netinet/in.h>
15+
#include <netinet/tcp.h>
16+
#include <arpa/inet.h>
17+
#include <netdb.h>
18+
#include <sys/time.h>
19+
#include <unistd.h>
20+
#include <sys/epoll.h>
21+
#include <fcntl.h>
22+
23+
typedef int SOCKET;
24+
#define INVALID_SOCKET -1
25+
#endif
26+
27+
#include <stdint.h>
28+
#include <chrono>
29+
30+
typedef int8_t int8;
31+
typedef uint8_t uint8;
32+
33+
typedef int16_t int16;
34+
typedef uint16_t uint16;
35+
36+
typedef int32_t int32;
37+
typedef uint32_t uint32;
38+
39+
typedef int64_t int64;
40+
typedef uint64_t uint64;
41+
42+
namespace base {
43+
inline int32 getProcessPID()
44+
{
45+
#ifdef WIN_OS
46+
return (int32)GetCurrentProcessId();
47+
#else
48+
return getpid();
49+
#endif
50+
}
51+
52+
inline char* strerror(int ierrorno = 0)
53+
{
54+
#ifdef WIN_OS
55+
if (ierrorno == 0)
56+
ierrorno = GetLastError();
57+
58+
static char lpMsgBuf[256] = { 0 };
59+
60+
/*
61+
FormatMessage(
62+
FORMAT_MESSAGE_FROM_SYSTEM |
63+
FORMAT_MESSAGE_IGNORE_INSERTS,
64+
NULL,
65+
ierrorno,
66+
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
67+
(LPTSTR) &lpMsgBuf,
68+
1024,
69+
NULL
70+
);
71+
*/
72+
snprintf(lpMsgBuf, 256, "errorno=%d", ierrorno);
73+
return lpMsgBuf;
74+
#else
75+
if (ierrorno != 0)
76+
return strerror(ierrorno);
77+
return strerror(errno);
78+
#endif
79+
}
80+
81+
inline int lasterror()
82+
{
83+
#ifdef WIN_OS
84+
return GetLastError();
85+
#else
86+
return errno;
87+
#endif
88+
}
89+
90+
/** 获取系统时间(精确到毫秒) */
91+
#ifdef WIN_OS
92+
inline uint32 getSystemTime()
93+
{
94+
// 注意这个函数windows上只能正确维持49天。
95+
return ::GetTickCount();
96+
};
97+
#else
98+
inline uint32 getSystemTime()
99+
{
100+
struct timeval tv;
101+
struct timezone tz;
102+
gettimeofday(&tv, &tz);
103+
return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
104+
};
105+
#endif
106+
107+
/* get time in millisecond 64 */
108+
inline uint64 getTimeMs()
109+
{
110+
#ifdef WIN_OS
111+
auto timeNow = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch());
112+
return timeNow.count();
113+
#else
114+
timeval time;
115+
gettimeofday(&time, NULL);
116+
return (uint64)((time.tv_sec * 1000) + (time.tv_usec / 1000));
117+
#endif
118+
}
119+
120+
#ifdef WIN_OS
121+
inline void sleep(uint32 ms)
122+
{
123+
::Sleep(ms);
124+
}
125+
#else
126+
inline void sleep(uint32 ms)
127+
{
128+
struct timeval tval;
129+
tval.tv_sec = ms / 1000;
130+
tval.tv_usec = (ms * 1000) % 1000000;
131+
select(0, NULL, NULL, NULL, &tval);
132+
}
133+
#endif
134+
};//namespace

src/PostParam.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#pragma once
2+
3+
#include <tuple>
4+
5+
struct PostBase
6+
{
7+
virtual void call() = 0;
8+
};
9+
10+
template<typename FUNC>
11+
struct PostNone : public PostBase
12+
{
13+
PostNone(FUNC&& func) :
14+
m_func(std::forward<FUNC>(func)) {
15+
}
16+
virtual void call() {
17+
(m_func)();
18+
}
19+
20+
FUNC m_func;
21+
};
22+
23+
template<typename FUNC, typename... ARGS>
24+
struct PostArge : public PostBase
25+
{
26+
public:
27+
PostArge(FUNC&& func, ARGS &&...args) :
28+
m_func(std::forward<FUNC>(func)),
29+
m_tuple(std::forward<ARGS>(args)...) {
30+
}
31+
virtual void call() {
32+
std::apply(std::move(m_func), std::move(m_tuple));
33+
}
34+
35+
private:
36+
FUNC m_func;
37+
std::tuple<ARGS...> m_tuple;
38+
};
39+
40+
#if 0
41+
template<typename FUNC, typename... ARGS>
42+
void post(FUNC&& func, ARGS &&...args)
43+
{
44+
auto ptr = new PostArge<FUNC, ARGS...>(std::forward<FUNC>(func), std::forward<ARGS>(args)...);
45+
ptr->call();
46+
47+
delete ptr;
48+
}
49+
50+
template<typename FUNC>
51+
void post(FUNC&& func)
52+
{
53+
auto ptr = new PostNone<FUNC>(std::forward<FUNC>(func));
54+
ptr->call();
55+
56+
delete ptr;
57+
}
58+
59+
#endif

0 commit comments

Comments
 (0)