Skip to content

Commit

Permalink
update code
Browse files Browse the repository at this point in the history
  • Loading branch information
Administrator1 committed Jan 1, 2019
1 parent 0331eb5 commit 0585f01
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 36 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ if (NOT DISABLE_HOOK)
if (UNIX)
aux_source_directory(${PROJECT_SOURCE_DIR}/libgo/netio/unix CO_SRC_LIST)
elseif (WIN32)
include_directories(${PROJECT_SOURCE_DIR}/libgo/netio/windows)
aux_source_directory(${PROJECT_SOURCE_DIR}/libgo/netio/windows CO_SRC_LIST)
endif()
else()
Expand Down
9 changes: 9 additions & 0 deletions libgo/common/clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace co
{

#if defined(LIBGO_SYS_Unix)
class FastSteadyClock
{
public:
Expand Down Expand Up @@ -80,5 +81,13 @@ class FastSteadyClock
return ((uint64_t)high << 32) | low;
}
};
#else
class FastSteadyClock
: public std::chrono::steady_clock
{
public:
static void ThreadRun() {}
};
#endif

} // namespace co
4 changes: 4 additions & 0 deletions libgo/common/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ const char* PollEvent2Str(short int event)
}
unsigned long NativeThreadID()
{
#if defined(LIBGO_SYS_Unix)
return reinterpret_cast<unsigned long>(pthread_self());
#else
return (unsigned long)GetCurrentThreadId();
#endif
}

std::string Format(const char* fmt, ...)
Expand Down
19 changes: 15 additions & 4 deletions libgo/common/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,13 @@
# define ALWAYS_INLINE inline
#endif

#define LIKELY(x) __builtin_expect(!!(x), 1)
#define UNLIKELY(x) __builtin_expect(!!(x), 0)
#if defined(LIBGO_SYS_Unix)
# define LIKELY(x) __builtin_expect(!!(x), 1)
# define UNLIKELY(x) __builtin_expect(!!(x), 0)
#else
# define LIKELY(x) x
# define UNLIKELY(x) x
#endif

#if defined(LIBGO_SYS_Linux)
# define ATTRIBUTE_WEAK __attribute__((weak))
Expand Down Expand Up @@ -160,8 +165,14 @@ std::string GetCurrentTime();
const char* BaseFile(const char* file);
const char* PollEvent2Str(short int event);
unsigned long NativeThreadID();
std::string Format(const char* fmt, ...) __attribute__((format(printf,1,2)));
std::string P(const char* fmt, ...) __attribute__((format(printf,1,2)));

#if defined(LIBGO_SYS_Unix)
# define GCC_FORMAT_CHECK __attribute__((format(printf,1,2)))
#else
# define GCC_FORMAT_CHECK
#endif
std::string Format(const char* fmt, ...) GCC_FORMAT_CHECK;
std::string P(const char* fmt, ...) GCC_FORMAT_CHECK;
std::string P();

class ErrnoStore {
Expand Down
2 changes: 2 additions & 0 deletions libgo/netio/windows/poll.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#include <WinSock2.h>
#include <Windows.h>
19 changes: 9 additions & 10 deletions libgo/netio/windows/win_vc_hook.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <WinSock2.h>
#include <Windows.h>
#include <xhook.h>
#include "xhook/xhook.h"
#include "../../scheduler/scheduler.h"

namespace co {
Expand Down Expand Up @@ -55,9 +55,8 @@ namespace co {
{
int ret = WSAIoctl_f(s, dwIoControlCode, lpvInBuffer, cbInBuffer, lpvOutBuffer, cbOutBuffer, lpcbBytesReturned, lpOverlapped, lpCompletionRoutine);
int err = WSAGetLastError();
if (ret == 0 && cmd == FIONBIO) {
int v = *argp;
setsockopt(s, SOL_SOCKET, SO_GROUP_PRIORITY, (const char*)&v, sizeof(v));
if (ret == 0 && dwIoControlCode == FIONBIO) {
setsockopt(s, SOL_SOCKET, SO_GROUP_PRIORITY, (const char*)lpvInBuffer, cbInBuffer);
}
WSASetLastError(err);
return ret;
Expand Down Expand Up @@ -91,7 +90,7 @@ namespace co {

static inline int WINAPI safe_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds)
{
//Task *tk = g_Scheduler.GetCurrentTask();
//Task *tk = Processer::GetCurrentTask();
//DebugPrint(dbg_hook, "task(%s) safe_select(nfds=%d, rfds=%p, wfds=%p, efds=%p).",
// tk ? tk->DebugInfo() : "nil", (int)nfds, readfds, writefds, exceptfds);
static const struct timeval zero_tmv { 0, 0 };
Expand Down Expand Up @@ -128,7 +127,7 @@ namespace co {
{
static const struct timeval zero_tmv{0, 0};
int timeout_ms = timeout ? (timeout->tv_sec * 1000 + timeout->tv_usec / 1000) : -1;
Task *tk = g_Scheduler.GetCurrentTask();
Task *tk = Processer::GetCurrentTask();
DebugPrint(dbg_hook, "task(%s) Hook select(nfds=%d, rfds=%p, wfds=%p, efds=%p, timeout=%d).",
tk ? tk->DebugInfo() : "nil", (int)nfds, readfds, writefds, exceptfds, timeout_ms);

Expand Down Expand Up @@ -170,7 +169,7 @@ namespace co {
}
}

g_Scheduler.SleepSwitch(delta_time);
::Sleep(delta_time);
if (delta_time < 16)
delta_time <<= 2;
}
Expand All @@ -184,7 +183,7 @@ namespace co {
template <typename OriginF, typename ... Args>
static int connect_mode_hook(OriginF fn, const char* fn_name, SOCKET s, Args && ... args)
{
Task *tk = g_Scheduler.GetCurrentTask();
Task *tk = Processer::GetCurrentTask();
bool is_nonblocking = IsNonblocking(s);
DebugPrint(dbg_hook, "task(%s) Hook %s(s=%d)(nonblocking:%d).",
tk ? tk->DebugInfo() : "nil", fn_name, (int)s, (int)is_nonblocking);
Expand Down Expand Up @@ -275,7 +274,7 @@ namespace co {
template <typename R, typename OriginF, typename ... Args>
static R read_mode_hook(OriginF fn, const char* fn_name, int flags, SOCKET s, Args && ... args)
{
Task *tk = g_Scheduler.GetCurrentTask();
Task *tk = Processer::GetCurrentTask();
bool is_nonblocking = IsNonblocking(s);
DebugPrint(dbg_hook, "task(%s) Hook %s(s=%d)(nonblocking:%d)(flags:%d).",
tk ? tk->DebugInfo() : "nil", fn_name, (int)s, (int)is_nonblocking, (int)flags);
Expand Down Expand Up @@ -474,7 +473,7 @@ namespace co {
template <typename R, typename OriginF, typename ... Args>
static R write_mode_hook(OriginF fn, const char* fn_name, int flags, SOCKET s, Args && ... args)
{
Task *tk = g_Scheduler.GetCurrentTask();
Task *tk = Processer::GetCurrentTask();
bool is_nonblocking = IsNonblocking(s);
DebugPrint(dbg_hook, "task(%s) Hook %s(s=%d)(nonblocking:%d)(flags:%d).",
tk ? tk->DebugInfo() : "nil", fn_name, (int)s, (int)is_nonblocking, (int)flags);
Expand Down
6 changes: 3 additions & 3 deletions vs_proj/README.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
生成Visual Studio 2013/2015工程方法:
生成Visual Studio 2017/2015工程方法:

1.安装git
2.安装cmake, 并将cmake可执行文件加入系统目录
3.启动git bash, 并切换到你正在阅读的这个README文件所在目录
4.执行脚本 ./make_vs_projs.sh
5.在当前目录下会生成不同版本的vs工程文件, 直接使用即可

另外, 如果你想运行测试代码和教程代码, 需要安装boost,
另外, 如果你想运行测试代码和教程代码, 需要安装boost(编译x64的lib),
并且在执行脚本时给出boost的安装目录, 例如:
$ ./make_vs_projs.sh -DBOOST_ROOT="E:\\boost_1_61_0"
$ ./make_vs_projs.sh -DBOOST_ROOT="E:\\boost_1_68_0"
33 changes: 14 additions & 19 deletions vs_proj/make_vs_projs.sh
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
#!/bin/sh

git submodule update --init --recursive
mkdir vs2017/x64 vs2015/x86 vs2015/x64 -p

mkdir vs2013/x86 vs2013/x64 vs2015/x86 vs2015/x64 -p
PATH=$PATH:/c/Program\ Files/CMake/bin/

rm vs2013/x86/* -rf
cd vs2013/x86
cmake ../../.. -G"Visual Studio 12 2013" $@
rm vs2017/x64/* -rf
cd vs2017/x64
cmake.exe ../../.. -G"Visual Studio 15 2017 Win64" $@
cd ../..

rm vs2013/x64/* -rf
cd vs2013/x64
cmake ../../.. -G"Visual Studio 12 2013 Win64" $@
cd ../..

rm vs2015/x86/* -rf
cd vs2015/x86
cmake ../../.. -G"Visual Studio 14 2015" $@
cd ../..

rm vs2015/x64/* -rf
cd vs2015/x64
cmake ../../.. -G"Visual Studio 14 2015 Win64" $@
cd ../..
#rm vs2015/x86/* -rf
#cd vs2015/x86
#cmake ../../.. -G"Visual Studio 14 2015" $@
#cd ../..
#
#rm vs2015/x64/* -rf
#cd vs2015/x64
#cmake ../../.. -G"Visual Studio 14 2015 Win64" $@
#cd ../..

0 comments on commit 0585f01

Please sign in to comment.