-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 'src/mc/model/' from commit '840bb2833d887e30a77fefc474b50d070615…
…0ac0' git-subtree-dir: src/mc/model git-subtree-mainline: aea6ade2546352819c4005a1e0a51d3a2b213d8f git-subtree-split: 840bb28
- Loading branch information
Showing
47 changed files
with
5,027 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Prerequisites | ||
*.d | ||
|
||
# Compiled Object files | ||
*.slo | ||
*.lo | ||
*.o | ||
*.obj | ||
|
||
# Precompiled Headers | ||
*.gch | ||
*.pch | ||
|
||
# Compiled Dynamic libraries | ||
*.so | ||
*.dylib | ||
*.dll | ||
|
||
# Fortran module files | ||
*.mod | ||
*.smod | ||
|
||
# Compiled Static libraries | ||
*.lai | ||
*.la | ||
*.a | ||
*.lib | ||
|
||
# Executables | ||
*.exe | ||
*.out | ||
*.app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
include $(all-subdir-makefiles) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#Makefile | ||
# bushaofeng. 2011.8.27 | ||
# ver=1.0.0 | ||
|
||
OUTPUT=./output | ||
OUTPUT_INC=$(OUTPUT)/include | ||
OUTPUT_LIB=$(OUTPUT)/lib | ||
|
||
ifneq ($(sys), ios) | ||
SUBDIRS = db | ||
endif | ||
|
||
SUBDIRS +=async push | ||
|
||
ifneq ($(sys), ios) | ||
SUBDIRS+= utp p2p | ||
endif | ||
|
||
all:: | ||
mkdir -p $(OUTPUT_INC) | ||
mkdir -p $(OUTPUT_LIB) | ||
@for MODEL in $(SUBDIRS);\ | ||
do \ | ||
( cp model.mk $$MODEL/Makefile && cd $$MODEL && make clean sys=$(sys) && make sys=$(sys) target=$$MODEL );\ | ||
done | ||
|
||
clean: | ||
rm -rf $(OUTPUT) | ||
@for MODEL in $(SUBDIRS);\ | ||
do \ | ||
( cd $$MODEL && make clean sys=$(sys) );\ | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# model | ||
一个包含异步http框架、websocket、可靠udp传输的框架库 | ||
|
||
依赖 rexbu/bs rexbu/basic |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
LOCAL_PATH := $(call my-dir) | ||
include $(CLEAR_VARS) | ||
|
||
LOCAL_CFLAGS := -D__ANDROID__ -D__MAIN_LOOP__ | ||
#APP_PLATFORM := android-8 | ||
|
||
LOCAL_C_INCLUDES += \ | ||
$(LOCAL_PATH)/../../bs/ \ | ||
$(LOCAL_PATH)/../../basic/ | ||
|
||
LOCAL_SRC_FILES += \ | ||
AsyncFrame.cpp \ | ||
AsyncSocket.cpp \ | ||
AsyncQueue.cpp \ | ||
SocketFrame.cpp | ||
|
||
#LOCAL_LDLIBS += -L$(SYSROOT)/usr/lib -llog | ||
LOCAL_STATIC_LIBRARIES := \ | ||
libbs \ | ||
libbasic | ||
|
||
LOCAL_MODULE := async | ||
|
||
include $(BUILD_STATIC_LIBRARY) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* file : AsyncFrame.cpp | ||
* author : bushaofeng | ||
* create : 2015-03-23 16:59 | ||
* func : | ||
* history: | ||
*/ | ||
|
||
#include "AsyncFrame.h" | ||
|
||
AsyncFrame::AsyncFrame(uint32_t timeout, bool ispipe){ | ||
m_ispipe = ispipe; | ||
m_frame_state = FRAME_STATE_IDLE; | ||
if (ispipe) { | ||
// m_fd用于控制select的立刻返回 | ||
int optval = 0; | ||
uint32_t len = sizeof(unsigned int); | ||
assert(pipe(m_pipe)==0); | ||
//禁用NAGLE算法 | ||
setsockopt(m_pipe[0], IPPROTO_TCP, TCP_NODELAY, &optval, len); | ||
setsockopt(m_pipe[1], IPPROTO_TCP, TCP_NODELAY, &optval, len); | ||
|
||
m_max_sock = m_pipe[0]>m_pipe[1] ? (m_pipe[0]+1):(m_pipe[1]+1); | ||
FD_ZERO(&m_read_set); | ||
FD_ZERO(&m_write_set); | ||
FD_ZERO(&m_error_set); | ||
FD_SET(m_pipe[0], &m_read_set); | ||
|
||
fprintf(stdout, "pipe[%d/%d] max_sock[%d]\n", m_pipe[0], m_pipe[1], m_max_sock); | ||
} | ||
|
||
if (timeout==0) { | ||
m_timeout = NULL; | ||
} | ||
else{ | ||
m_timeout = &m_time_entity; | ||
BS_SET_TIMEVAL(m_timeout, timeout); | ||
} | ||
} | ||
|
||
void AsyncFrame::stop(){ | ||
if (m_running) { | ||
LoopThread::stop(); | ||
interrupt(); | ||
} | ||
} | ||
void AsyncFrame::interrupt(){ | ||
write(m_pipe[1], "a", 1); | ||
} | ||
|
||
void AsyncFrame::loop(){ | ||
char buffer[8] = {0}; | ||
|
||
m_frame_state = FRAME_STATE_LISTEN; | ||
m_select_rv = select(m_max_sock, &m_read_set, &m_write_set, &m_error_set, m_timeout); | ||
m_frame_state = FRAME_STATE_HANDLE; | ||
if (m_select_rv>0) { | ||
if (!m_ispipe) { | ||
return; | ||
} | ||
|
||
if (FD_ISSET(m_pipe[0], &m_read_set)) { | ||
read(m_pipe[0], buffer, sizeof(buffer)); | ||
interruptHandle(); | ||
} | ||
else{ | ||
FD_SET(m_pipe[0], &m_read_set); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* file : AsyncFrame.h | ||
* author : bushaofeng | ||
* create : 2015-03-23 16:55 | ||
* func : 异步框架 | ||
* history: | ||
*/ | ||
|
||
#ifndef __ASYNCFRAME_H_ | ||
#define __ASYNCFRAME_H_ | ||
|
||
#include "bs.h" | ||
#include "Thread.h" | ||
|
||
class AsyncFrame: public LoopThread{ | ||
public: | ||
AsyncFrame(uint32_t time=0, bool ispipe=true); | ||
~AsyncFrame(){} | ||
// Override | ||
void stop(); | ||
void loop(); | ||
// 让select立刻返回 | ||
void interrupt(); | ||
|
||
const static int FRAME_STATE_IDLE = 0; | ||
const static int FRAME_STATE_LISTEN = 1; | ||
const static int FRAME_STATE_HANDLE = 2; | ||
|
||
protected: | ||
// select立刻返回后的处理函数 | ||
virtual void interruptHandle(){}; | ||
|
||
protected: | ||
// 管道,用于控制select结束 | ||
int m_pipe[2]; | ||
int m_max_sock; | ||
// 0代表超时或者select interrupt,无socket准备就绪 | ||
int m_select_rv; | ||
int m_frame_state; | ||
|
||
bool m_ispipe; | ||
fd_set m_read_set; | ||
fd_set m_write_set; | ||
fd_set m_error_set; | ||
timeval* m_timeout; | ||
timeval m_time_entity; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* file : AsyncSocket.cpp | ||
* author : bushaofeng | ||
* create : 2014-10-16 12:35 | ||
* func : | ||
* history: | ||
*/ | ||
|
||
#include "AsyncSocket.h" | ||
#include "ThreadPool.h" | ||
|
||
void* sock_msg_init(void* p){ | ||
sock_msg_t* msg = (sock_msg_t*)p; | ||
|
||
msg->buf = msg->temp; | ||
return msg; | ||
} | ||
|
||
void sock_msg_destroy(void* p){ | ||
sock_msg_t* msg = (sock_msg_t*)p; | ||
// 如果buf是额外申请的空间 | ||
if (msg->buf != msg->temp && msg->buf!=NULL) { | ||
free(msg->buf); | ||
} | ||
} | ||
|
||
// msg异步执行 | ||
void* sock_on_message(void* arg){ | ||
sock_msg_t* msg = (sock_msg_t*)arg; | ||
AsyncSocket* sock = (AsyncSocket*)msg->arg; | ||
sock->onMessage(msg); | ||
// 如果msg的buf是另外申请的 | ||
bs_delete(msg); | ||
return NULL; | ||
} | ||
|
||
AsyncSocket::AsyncSocket(int sock, int sock_type): | ||
m_sock(sock),m_type(sock_type){} | ||
|
||
AsyncSocket::AsyncSocket(){ | ||
m_sock = 0; | ||
m_type = SOCK_STREAM; | ||
} | ||
|
||
void AsyncSocket::onRead(){ | ||
sock_msg_t* msg = bs_new(sock_msg); | ||
if (msg!=NULL){ | ||
msg->size = (int)read(m_sock, msg->temp, sizeof(msg->temp)); | ||
debug_log("socket[%d] recv [%d]", m_sock, msg->size); | ||
if (msg->size <= 0) { | ||
err_log("socket[%d] recv[%d] error", m_sock, msg->size); | ||
onError(errno); | ||
return; | ||
} | ||
msg->buf = msg->temp; | ||
msg->sock = m_sock; | ||
msg->arg = this; | ||
async_run(sock_on_message, msg); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* file : AsyncSocket.h | ||
* author : bushaofeng | ||
* create : 2014-10-04 22:39 | ||
* func : 异步Socket, 一次发包不能超过mtu大小 | ||
* history: | ||
*/ | ||
|
||
#ifndef __ASYNCSOCKET_H_ | ||
#define __ASYNCSOCKET_H_ | ||
|
||
#include "bs.h" | ||
#include "Thread.h" | ||
#ifdef __ANDROID__ | ||
#include <jni.h> | ||
#endif | ||
|
||
typedef struct sock_msg_t{ | ||
object_t base; | ||
int sock; | ||
int size; | ||
char* buf; | ||
struct sockaddr_in addr; | ||
// 额外参数,目前用法指向AsyncSocket,用于AsyncQueue的回调 | ||
void* arg; | ||
char temp[SOCKET_UDP_MTU]; | ||
} sock_msg_t; | ||
|
||
void* sock_msg_init(void* p); | ||
void sock_msg_destroy(void* p); | ||
|
||
// message的异步执行线程 | ||
void* sock_on_message(void* arg); | ||
|
||
class AsyncSocket{ | ||
public: | ||
AsyncSocket(int sock, int sock_type = SOCK_STREAM); | ||
AsyncSocket(); | ||
virtual ~AsyncSocket(){} | ||
|
||
int getSocket() { | ||
return m_sock; | ||
} | ||
|
||
virtual void onRead(); | ||
virtual void onWrite() = 0; | ||
virtual void onError(int error) = 0; | ||
virtual void onMessage(sock_msg_t* msg) = 0; | ||
|
||
protected: | ||
int m_sock; | ||
int m_type; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* file : EventAsyncSocket.h | ||
* author : bushaofeng | ||
* create : 2014-10-05 22:48 | ||
* func : | ||
* history: | ||
*/ | ||
|
||
#ifndef __EVENTASYNCSOCKET_H_ | ||
#define __EVENTASYNCSOCKET_H_ | ||
|
||
#include <event.h> | ||
#include "AsyncSocket.h" | ||
|
||
class EventAsyncSocket:virtual public AsyncSocket{ | ||
public: | ||
EventAsyncSocket(int sock, int sock_type):AsyncSocket(sock, sock_type){} | ||
virtual ~EventAsyncSocket(){} | ||
|
||
struct event* getReadEvent() { return &m_read_event; } | ||
struct event* getWriteEvent() { return &m_write_event; } | ||
|
||
protected: | ||
struct event m_read_event; | ||
struct event m_write_event; | ||
}; | ||
|
||
#endif |
Oops, something went wrong.