Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
74025fb
#reactor
stsfang Sep 14, 2020
ad72a56
add logger(#9.15) and fullfill
stsfang Sep 15, 2020
c40efd5
fix compile error#one error remained
stsfang Sep 16, 2020
b2a9046
fix compile error&&run the test
stsfang Sep 17, 2020
c2110c2
test timerqueue#poller is ::poll
stsfang Sep 17, 2020
04f05d0
add epollpoller&&change default poller to epollpoller
stsfang Sep 18, 2020
33bcc87
add cancel timer logic
stsfang Sep 18, 2020
f6b4d0a
add chive logger
stsfang Sep 21, 2020
91c0978
add inetaddr && some optimization for clog
stsfang Sep 21, 2020
9ff665a
update
Sep 23, 2020
9a73b50
update
stsfang Sep 23, 2020
f942939
fix compile err && test acceptor
stsfang Sep 23, 2020
54809c3
fix bug#1 strlen(host) != sizeof(host)
stsfang Sep 23, 2020
cda9f8c
fix bug#memcpy didn't copy the delimiter '\0'
stsfang Sep 23, 2020
414235a
fix bug# std::string::c_str() to get C string data
stsfang Sep 23, 2020
8d70979
change stream style Logger to ChiveLogger
stsfang Sep 23, 2020
2b62fd3
fix bug#compatible console log and file log
stsfang Sep 23, 2020
bff82c0
test acceptor ok
stsfang Sep 23, 2020
6eed2d3
add trace log for epoller
stsfang Sep 23, 2020
7cff421
add tcpconnection && tcpserver
stsfang Sep 23, 2020
5e0f1d3
add tcpconnection test
stsfang Sep 23, 2020
cb8da42
filepath to filename&&adjust format
stsfang Sep 24, 2020
22edadc
update tcpconnection&tcpserver
stsfang Sep 24, 2020
bbd69e5
test tcpconnection&tcpserver ok
stsfang Sep 24, 2020
61a4b96
add disconnect for tcpconnection
stsfang Sep 25, 2020
7dcfb0d
test tcpconn disconnect actively ok
stsfang Sep 25, 2020
ffd9aa6
add Buffer R-W data
stsfang Sep 25, 2020
f5f3ed9
test Buffer
stsfang Sep 25, 2020
ea24c11
add tcpconn 'send data'
stsfang Sep 26, 2020
b3e9871
test echo-server ok
stsfang Sep 26, 2020
df406f1
test write complate callback
stsfang Sep 26, 2020
c31cb17
handle with high-water-mark
stsfang Sep 28, 2020
ad3b9b8
ignore sigpipe#define it to enable
stsfang Sep 28, 2020
cdba9f3
add evtthreadpool for tcpserver
stsfang Sep 28, 2020
21a703e
test find out a bug#subthread return loop(nil)
stsfang Sep 28, 2020
4482dcb
temp fix#sleep seconds to wait util Evtloop finishes init
stsfang Sep 28, 2020
ef53a46
fix bug#if Assert() not effetive, some core function-calls were shieled
stsfang Sep 29, 2020
96db241
update#reactor core components
stsfang Sep 29, 2020
bf3bda4
remained bug#should remove channel while ~TimerQueue
stsfang Sep 29, 2020
dc9b3a6
remove unuseful debug log
stsfang Sep 29, 2020
6087fec
test evtloopthread ok
stsfang Sep 29, 2020
8632886
http parser
stsfang Sep 29, 2020
16c9cef
update http-module
stsfang Oct 2, 2020
474c027
update BUILD
stsfang Oct 2, 2020
88e8fec
test httpserver#ok
stsfang Oct 2, 2020
329434b
add tcpclient
stsfang Oct 9, 2020
99bfaaf
add forceClose for TcpConnection
stsfang Oct 10, 2020
7189c0a
add socketops
stsfang Oct 10, 2020
106b11a
add tcpclient
stsfang Oct 10, 2020
c46a6e0
ed
stsfang Oct 10, 2020
d8d23e4
add test tcpclient
stsfang Oct 10, 2020
72a403b
move default callbacks' definition from TcpServer to TcpConnection
stsfang Oct 12, 2020
c60342d
add HttpBody compatiable for more content type
stsfang Oct 12, 2020
8b623d5
add HttpBody compatible with more content type
stsfang Oct 12, 2020
9b6adc2
Merge branch 'tmp_chive_net_lib_v2' of https://github.com/stsfang/chi…
stsfang Oct 12, 2020
7e9e5b4
Merge branch 'tmp_chive_net_lib_v2' of https://github.com/stsfang/chi…
stsfang Oct 12, 2020
ed1c2b9
Merge branch 'tmp_chive_net_lib_v2' of https://github.com/stsfang/chi…
stsfang Oct 12, 2020
93ad68a
update
stsfang Oct 13, 2020
ed31675
add http request decoder reference from netty4.x
stsfang Oct 15, 2020
47b3abd
update http body
stsfang Oct 15, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,10 @@
*.exe
*.out
*.app

# blade build
blade-bin/
build64_release/

#vs code
.vscode/
Empty file added chive/base/AsynLogging.cc
Empty file.
Empty file added chive/base/AsynLogging.h
Empty file.
65 changes: 65 additions & 0 deletions chive/base/Atomic.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#ifndef CHIVE_BASE_ATOMIC_H
#define CHIVE_BASE_ATOMIC_H

#include "chive/base/noncopyable.h"
#include <cstdint>

namespace chive
{
namespace base
{
template<typename T>
class AtomicIntegerT : noncopyable
{
public:
AtomicIntegerT():value_(0){}

T get() {
return __atomic_load_n(&value_, __ATOMIC_SEQ_CST);
}

T getAndAdd(T x) {
return __atomic_fetch_add(&value_, x, __ATOMIC_SEQ_CST);
}

T addAndGet(T x) {
return getAndAdd(x) + x;
}

T incrementAndGet() {
return addAndGet(1);
}

T decrementAndGet() {
return addAndGet(-1);
}

void add(T x) {
getAndAdd(x);
}

void increment() {
incrementAndGet();
}

void decrement() {
decrementAndGet();
}

T getAndSet(T newVal) {
return __atomic_exchange_n(&value_, newVal, __ATOMIC_SEQ_CST);
}

private:
volatile T value_;
};
} // namespace base

/**
* 提供给chive::net namespace下直接使用
*/
using AtomicInt32 = base::AtomicIntegerT<int32_t>;
using AtomicInt64 = base::AtomicIntegerT<int64_t>;
} // namespace chive

#endif
32 changes: 32 additions & 0 deletions chive/base/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
cc_library(
name = 'chive_base',
srcs = [
'CurrentThread.cc',
'CountDownLatch.cc',
'Thread.cc',
'FileUtil.cc',
],
deps = [
'#pthread',
'//chive/base/clog/:chive_log'
],
extra_cppflags = [
'-std=c++17',
# '-Wall', # show warning as error
'-w', # no warning
'-g',
]
)

cc_library(
name = 'chive_fileutil',
srcs = [
'FileUtil.cc'
],
extra_cppflags = [
'-std=c++17',
# '-Wall', # show warning as error
'-w', # no warning
'-g',
]
)
57 changes: 57 additions & 0 deletions chive/base/Condition.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#ifndef CHIVE_BASE_CONDITION_H
#define CHIVE_BASE_CONDITION_H

#include "chive/base/MutexLock.h"

#include <pthread.h>
#include <cassert>
#include <errno.h> /*provide ETIMEDOUT */

namespace chive
{
class Condition : noncopyable {
public:
explicit Condition(MutexLock& mutex) : mutex_(mutex) {
int flag = pthread_cond_init(&cond_, nullptr);
assert(flag == 0); (void)flag;
}

~Condition() {
int flag = pthread_cond_destroy(&cond_);
assert(flag == 0); (void)flag;
}

void wait() {
/// FIXME: need lock guard or not?
int flag = pthread_cond_wait(&cond_, mutex_.getPthreadMutexPtr());
assert(flag == 0); (void)flag;
}

bool waitForSecond(int second) {
struct timespec timeout{};
// CLOCK_REALTIME 和 CLOCK_MONOTONIC 的区别
// clock_getres 和 clock_gettime的区别
/*clock_getres(CLOCK_REALTIME, &timeout);*/
clock_getres(CLOCK_MONOTONIC, &timeout);
timeout.tv_sec += second;
return pthread_cond_timedwait(
&cond_, mutex_.getPthreadMutexPtr(), &timeout) == ETIMEDOUT;
}

void notify() {
int flag = pthread_cond_signal(&cond_);
assert(flag == 0); (void)flag;
}

void notifyall() {
int flag = pthread_cond_broadcast(&cond_);
assert(flag == 0); (void)flag;
}

private:
MutexLock& mutex_;
pthread_cond_t cond_;
};
} // namespace chive

#endif
39 changes: 39 additions & 0 deletions chive/base/ContentTypes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef CHIVE_NET_CONTENT_TYPES_H
#define CHIVE_NET_CONTENT_TYPES_H

namespace chive
{

/**
* 主流的body content type
*
* application/x-www-form-urlencoded: 不属于http content-type规范,通常用于浏览器表单提交,
* 格式:name1=value1&name2=value2, POST会放入http body,GET则显示在在URL
* urlencoded格式如 URL中出现 %E4%BD%A0,与unicode(\uxxxx) 区分
*
*
*/
enum class ContentType
{
ApplicationJson,
ApplicationXml,
ApplicationBase64,
ApplicationXW3FormUrlEncoded, /**/
ApplicationOctetStream, /*二进制流或字节数组*/
MultipartFormdata, /*表单*/
TextPlain,
TextCss,
TextHtml,
ApplicationJavascript,
OtherType
};

enum class MimeType
{
TextXml,
TextHtml,
};

} // namespace chive

#endif
31 changes: 31 additions & 0 deletions chive/base/CountDownLatch.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "chive/base/CountDownLatch.h"

using namespace chive;

CountDownLatch::CountDownLatch(int count)
: mutex_{},
condition_(mutex_),
count_(count)
{
}

void CountDownLatch::wait() {
MutexLockGuard lock(mutex_);
while(count_ > 0) {
condition_.wait();
}
}

void CountDownLatch::countDown() {
MutexLockGuard lock(mutex_);
--count_;
if(count_ == 0) {
condition_.notifyall();
}
}

int CountDownLatch::getCount() const {
MutexLockGuard lock(mutex_);
return count_;
}

30 changes: 30 additions & 0 deletions chive/base/CountDownLatch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef CHIVE_BASE_COUNTDOWNLATCH_H
#define CHIVE_BASE_COUNTDOWNLATCH_H

#include "chive/base/noncopyable.h"
#include "chive/base/Condition.h"
#include "chive/base/MutexLock.h"

namespace chive
{
class CountDownLatch : noncopyable {
public:

explicit CountDownLatch(int count);

void wait();

void countDown();

int getCount() const;
private:
//
mutable MutexLock mutex_;
// 相比muduo源码,这里省去了clang线程安全检查注解 guard_by
/// FIXME:
Condition condition_;
int count_;
};
} // namespace chive

#endif
30 changes: 30 additions & 0 deletions chive/base/CurrentThread.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "chive/base/CurrentThread.h"

// using namespace chive;

#include <cstdio>
#include <sys/syscall.h> /* For SYS_xxx definitions */

namespace chive
{
namespace CurrentThread
{
// 必须在与 .h 相同的namespace 下定义,否则导致二义性
// 即,编译器认不出是 .cc 的 还是 .h 的
// 定义 extern __thread
__thread int t_cachedTid = 0;
__thread char t_tidString[32];
__thread int t_tidStringLength = 6;
__thread const char* t_threadName = "unknown";
static_assert(std::is_same<int, pid_t>::value, "pid_t should be int");

void cachedTid() {
if(t_cachedTid == 0) {
t_cachedTid = static_cast<pid_t>(::syscall(SYS_gettid));
t_tidStringLength = snprintf(t_tidString, sizeof t_tidString, "%5d ", t_cachedTid);
}
}
} // namespace CurrentThread

} // namespace chive

41 changes: 41 additions & 0 deletions chive/base/CurrentThread.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#ifndef V_CURRENTTHREAD_H
#define V_CURRENTTHREAD_H

#include <unistd.h>
#include <type_traits>
namespace chive
{
namespace CurrentThread {
// 使用extern告诉编译器这些__thread修饰的变量定义在
// 另一个文件,只能初始化为编译器常量
extern __thread int t_cachedTid;
extern __thread char t_tidString[32];
extern __thread int t_tidStringLength;
extern __thread const char* t_threadName;

void cachedTid();

inline int tid() {
// __builtin_expect是GCC的內建函数
//作用:编译器分支预测,减少跳转指令,从而优化性能
if(__builtin_expect(t_cachedTid == 0, 0)) {
cachedTid();
}
return t_cachedTid;
}

inline int tidStringLength() {
return t_tidStringLength;
}
// 未实现设置线程名
inline const char* name() {
return t_threadName;
}
//根据主线程的pid == tid
inline bool isMainThread() {
return tid() == ::getpid();
}
};
} // namespace chive

#endif
36 changes: 36 additions & 0 deletions chive/base/FileUtil.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "chive/base/FileUtil.h"
#include "chive/base/clog/chiveLog.h"
#include <iostream>

using namespace chive;
using namespace chive::FileUtil;

const char* FILE_PATH = "/data/chive/upload/";

File::File(std::string filename, ContentType type)
: filename_(filename),
contentType_(type),
writtenBytes_(0)
{

}

File::~File()
{

}

void File::output(const char* begin, const char* end)
{
fout_.open(FILE_PATH + filename_, std::ios::out|std::ios::binary);
if (!fout_.is_open())
{
// CHIVE_LOG_ERROR("Cannot open file %s", FILE_PATH + filename_);
std::cout << "err" << std::endl;
}
else
{
fout_ << std::string(begin, end);
fout_.close();
}
}
Loading