Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/swoole/swoole-src
Browse files Browse the repository at this point in the history
  • Loading branch information
郭新华 committed May 21, 2020
2 parents 8dbf506 + 400d855 commit d5a1cb5
Show file tree
Hide file tree
Showing 74 changed files with 791 additions and 262 deletions.
8 changes: 4 additions & 4 deletions config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,8 @@ if test "$PHP_SWOOLE" != "no"; then
src/core/heap.cc \
src/core/log.cc \
src/core/ring_queue.cc \
src/core/socket.cc \
src/core/string.cc \
src/core/timer.cc \
src/coroutine/base.cc \
src/coroutine/channel.cc \
src/coroutine/context.cc \
Expand All @@ -441,15 +441,15 @@ if test "$PHP_SWOOLE" != "no"; then
src/memory/table.cc \
src/network/client.cc \
src/network/dns.cc \
src/network/process_pool.cc \
src/network/socket.cc \
src/network/stream.cc \
src/network/thread_pool.cc \
src/network/timer.cc \
src/os/async_thread.cc \
src/os/base.cc \
src/os/msg_queue.cc \
src/os/process_pool.cc \
src/os/sendfile.cc \
src/os/signal.cc \
src/os/thread_pool.cc \
src/os/timer.cc \
src/os/wait.cc \
src/pipe/base.cc \
Expand Down
4 changes: 2 additions & 2 deletions core-tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
cmake_minimum_required(VERSION 2.8)
cmake_minimum_required(VERSION 2.8.1)

project(core_tests)

#set(CMAKE_BUILD_TYPE Released)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -g")

file(GLOB_RECURSE SOURCE_FILES FOLLOW_SYMLINKS src/*.cpp)

Expand Down
32 changes: 32 additions & 0 deletions core-tests/src/core/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ TEST(log, date_format)
ASSERT_TRUE(n);
}

TEST(log, date_format_long_string)
{
swLog_reset();
swLog_set_level(SW_LOG_ERROR);
swoole::String content(swString_new(256));
auto str = content.get();

swString_repeat(str, "x", 1, 120);
swString_append_ptr(str, SW_STRL("day %d of %B in the year %Y. Time: %I:%S %p"));

int retval = swLog_set_date_format(str->str);

ASSERT_EQ(retval, SW_ERR);
ASSERT_EQ(swoole_get_last_error(), SW_ERROR_INVALID_PARAMS);
}

TEST(log, date_with_microseconds)
{
swLog_reset();
Expand All @@ -60,3 +76,19 @@ TEST(log, date_with_microseconds)
std::regex e("\\[\\S+\\s\\d{2}:\\d{2}:\\d{2}\\<\\.(\\d+)\\>\\s@\\d+\\.\\d+\\]\tWARNING\thello world");
ASSERT_TRUE(std::regex_search(content.value(), e));
}

TEST(log, rotation)
{
swLog_reset();
swLog_set_rotation(SW_LOG_ROTATION_DAILY);
swLog_open(file);

swLog_put(SW_LOG_WARNING, SW_STRL("hello world"));

ASSERT_EQ(access(swLog_get_file(), R_OK), -1);
ASSERT_EQ(errno, ENOENT);
ASSERT_EQ(access(swLog_get_real_file(), R_OK), 0);

swLog_close();
unlink(swLog_get_real_file());
}
26 changes: 26 additions & 0 deletions core-tests/src/core/time.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "tests.h"

TEST(time, get_ms)
{
const int us = 3000;
long ms1 = swoole::time<std::chrono::milliseconds>();
usleep(us);
long ms2 = swoole::time<std::chrono::milliseconds>();
EXPECT_GE(ms2 - ms1, us / 1000);
}

TEST(time, get_ms_steady)
{
const int us = 3000;
long ms1 = swoole::time<std::chrono::milliseconds>(true);
usleep(us);
long ms2 = swoole::time<std::chrono::milliseconds>(true);
EXPECT_GE(ms2 - ms1, us / 1000);
}

TEST(time, get_seconds)
{
long sec1 = swoole::time<std::chrono::seconds>();
time_t sec2 = time(NULL);
EXPECT_EQ(sec1, sec2);
}
4 changes: 2 additions & 2 deletions examples/cpp/test_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ int main(int argc, char **argv)
ret = swServer_create(&serv);
if (ret < 0)
{
swWarn("create server fail[error=%d]", SwooleG.error);
swWarn("create server fail[error=%d]", swoole_get_last_error());
exit(1);
}

swListenPort *port = swServer_add_port(&serv, SW_SOCK_TCP, "127.0.0.1", 9501);
if (!port)
{
swWarn("listen failed, [error=%d]", SwooleG.error);
swWarn("listen failed, [error=%d]", swoole_get_last_error());
exit(2);
}

Expand Down
8 changes: 4 additions & 4 deletions include/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ typedef struct _swClient

void (*onConnect)(struct _swClient *cli);
void (*onError)(struct _swClient *cli);
void (*onReceive)(struct _swClient *cli, char *data, uint32_t length);
void (*onReceive)(struct _swClient *cli, const char *data, uint32_t length);
void (*onClose)(struct _swClient *cli);
void (*onBufferFull)(struct _swClient *cli);
void (*onBufferEmpty)(struct _swClient *cli);
Expand Down Expand Up @@ -166,12 +166,12 @@ typedef struct _swStream
swString *buffer;
uint8_t cancel;
void *private_data;
void (*response)(struct _swStream *stream, char *data, uint32_t length);
void (*response)(struct _swStream *stream, const char *data, uint32_t length);
swClient client;
} swStream;

swStream* swStream_new(char *dst_host, int dst_port, enum swSocket_type type);
int swStream_send(swStream *stream, char *data, size_t length);
swStream* swStream_new(const char *dst_host, int dst_port, enum swSocket_type type);
int swStream_send(swStream *stream, const char *data, size_t length);
void swStream_set_protocol(swProtocol *protocol);
void swStream_set_max_length(swStream *stream, uint32_t max_length);
int swStream_recv_blocking(swSocket *sock, void *__buf, size_t __len);
Expand Down
7 changes: 7 additions & 0 deletions include/coroutine_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,11 @@ class Socket
return write_buffer;
}

inline void set_zero_copy(bool enable)
{
zero_copy = enable;
}

#ifdef SW_USE_OPENSSL
inline bool is_ssl_enable()
{
Expand Down Expand Up @@ -392,6 +397,8 @@ class Socket
bool shutdown_write = false;
bool closed = false;

bool zero_copy = false;

static void timer_callback(swTimer *timer, swTimer_node *tnode);
static int readable_event_callback(swReactor *reactor, swEvent *event);
static int writable_event_callback(swReactor *reactor, swEvent *event);
Expand Down
3 changes: 0 additions & 3 deletions include/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,4 @@ enum swErrorCode
SW_ERROR_END
};

const char* swoole_strerror(int code);
void swoole_throw_error(int code);

#endif /* SW_ERRNO_H_ */
4 changes: 2 additions & 2 deletions include/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ int swHttpRequest_has_expect_header(swHttpRequest *request);
#endif

#ifdef SW_USE_HTTP2
ssize_t swHttpMix_get_package_length(swProtocol *protocol, swSocket *conn, char *data, uint32_t length);
ssize_t swHttpMix_get_package_length(swProtocol *protocol, swSocket *conn, const char *data, uint32_t length);
uint8_t swHttpMix_get_package_length_size(swSocket *conn);
int swHttpMix_dispatch_frame(swProtocol *protocol, swSocket *conn, char *data, uint32_t length);
int swHttpMix_dispatch_frame(swProtocol *protocol, swSocket *conn, const char *data, uint32_t length);
#endif

SW_EXTERN_C_END
Expand Down
2 changes: 1 addition & 1 deletion include/http2.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ static sw_inline ssize_t swHttp2_get_length(const char *buf)
return (((uint8_t) buf[0]) << 16) + (((uint8_t) buf[1]) << 8) + (uint8_t) buf[2];
}

ssize_t swHttp2_get_frame_length(swProtocol *protocol, swSocket *conn, char *buf, uint32_t length);
ssize_t swHttp2_get_frame_length(swProtocol *protocol, swSocket *conn, const char *buf, uint32_t length);
int swHttp2_send_setting_frame(swProtocol *protocol, swSocket *conn);
const char* swHttp2_get_type(int type);
int swHttp2_get_type_color(int type);
Expand Down
4 changes: 2 additions & 2 deletions include/lru_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class LRUCache
return nullptr;
}

if (iter->second->second.first < time(nullptr) && iter->second->second.first > 0)
if (iter->second->second.first < ::time(nullptr) && iter->second->second.first > 0)
{
return nullptr;
}
Expand All @@ -70,7 +70,7 @@ class LRUCache
}
else
{
expire_time = time(nullptr) + expire;
expire_time = ::time(nullptr) + expire;
}

auto iter = cache_map.find(key);
Expand Down
2 changes: 1 addition & 1 deletion include/mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ typedef struct
#define SETQOS(HDR, Q) (HDR | ((Q) << 1))
#define SETDUP(HDR, D) (HDR | ((D) << 3))

ssize_t swMqtt_get_package_length(swProtocol *protocol, swSocket *conn, char *data, uint32_t size);
ssize_t swMqtt_get_package_length(swProtocol *protocol, swSocket *conn, const char *data, uint32_t size);

SW_EXTERN_C_END

Expand Down
7 changes: 5 additions & 2 deletions include/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,9 @@ void swServer_call_hook(swServer *serv, enum swServer_hook_type type, void *arg)
void swServer_clear_timer(swServer *serv);
int swServer_create(swServer *serv);

int swServer_worker_idle_num(swServer *serv);
int swServer_task_worker_idle_num(swServer *serv);

static inline bool swServer_if_require_receive_callback(swServer *serv, swListenPort *port, bool isset)
{
#ifdef SW_USE_OPENSSL
Expand Down Expand Up @@ -1061,8 +1064,8 @@ void swReactorThread_set_protocol(swServer *serv, swReactor *reactor);
void swReactorThread_join(swServer *serv);
void swReactorThread_free(swServer *serv);
int swReactorThread_close(swReactor *reactor, swSocket *_socket);
int swReactorThread_dispatch(swProtocol *proto, swSocket *_socket, char *data, uint32_t length);
int swReactorThread_send2worker(swServer *serv, swWorker *worker, void *data, size_t len);
int swReactorThread_dispatch(swProtocol *proto, swSocket *_socket, const char *data, uint32_t length);
int swReactorThread_send2worker(swServer *serv, swWorker *worker, const void *data, size_t len);

int swReactorProcess_create(swServer *serv);
int swReactorProcess_start(swServer *serv);
Expand Down
Loading

0 comments on commit d5a1cb5

Please sign in to comment.