diff --git a/app/wizard_demo/httpd_proxy/Makefile b/app/wizard_demo/httpd_proxy/Makefile new file mode 100644 index 000000000..baa0526af --- /dev/null +++ b/app/wizard_demo/httpd_proxy/Makefile @@ -0,0 +1,2 @@ +include ./Makefile.in +PROG = httpd_proxy diff --git a/app/wizard_demo/httpd_proxy/Makefile.in b/app/wizard_demo/httpd_proxy/Makefile.in new file mode 100644 index 000000000..406fc6470 --- /dev/null +++ b/app/wizard_demo/httpd_proxy/Makefile.in @@ -0,0 +1,148 @@ +CC = g++ + +CFLAGS = -c -g -W \ +-Wall \ +-Wcast-qual \ +-Wcast-align \ +-Wno-long-long \ +-Wpointer-arith \ +-Werror \ +-Wshadow \ +-O3 \ +-D_REENTRANT \ +-D_POSIX_PTHREAD_SEMANTICS \ +-D_USE_FAST_MACRO + +########################################################### +#Check system: +# Linux, SunOS, Solaris, BSD variants, AIX, HP-UX +SYSLIB = -lpthread -lz -ldl +CHECKSYSRES = @echo "Unknow system type!";exit 1 +UNIXNAME = $(shell uname -s) +OSTYPE = $(shell uname -m) +RPATH = linux64 + +ifeq ($(CC),) + CC = gcc +endif + +# For FreeBSD +ifeq ($(findstring FreeBSD, $(UNIXNAME)), FreeBSD) + ifeq ($(findstring gcc, $(CC)), gcc) + CFLAGS += -Wstrict-prototypes + endif + CFLAGS += -DFREEBSD -D_REENTRANT + SYSLIB = -lcrypt -lpthread -lz + RPATH = freebsd +endif + +# For Darwin +ifeq ($(findstring Darwin, $(UNIXNAME)), Darwin) + CFLAGS += -DMACOSX -Wno-invalid-source-encoding \ + -Wno-extended-offsetof + UNIXTYPE = MACOSX + SYSLIB += -liconv -rdynamic + RPATH = macos +endif + +#Path for Linux +ifeq ($(findstring Linux, $(UNIXNAME)), Linux) + ifeq ($CC, "gcc") + CFLAGS += -Wstrict-prototypes + endif + ifeq ($(findstring i686, $(OSTYPE)), i686) + RPATH = linux32 + endif + ifeq ($(findstring x86_64, $(OSTYPE)), x86_64) + RPATH = linux64 + endif + CFLAGS += -DLINUX2 -D_REENTRANT + SYSLIB += -lcrypt +endif + +# For MINGW +ifeq ($(findstring MINGW, $(UNIXNAME)), MINGW) + SYSLIB = -lpthread-2 -liconv + CFLAGS += -DLINUX2 -DMINGW + UNIXTYPE = LINUX +endif + +# For MSYS +ifeq ($(findstring MSYS, $(UNIXNAME)), MSYS) + SYSLIB = -lpthread-2 -liconv + CFLAGS += -DLINUX2 -DMINGW + UNIXTYPE = LINUX +endif + +#Path for SunOS +ifeq ($(findstring SunOS, $(UNIXNAME)), SunOS) + ifeq ($(findstring 86, $(UNIXNAME)), 86) + SYSLIB += -lsocket -lnsl -lrt + endif + ifeq ($(findstring sun4u, $(UNIXNAME)), sun4u) + SYSLIB += -lsocket -lnsl -lrt + endif + ifeq ($CC, "gcc") + CFLAGS += -Wstrict-prototypes + endif + CFLAGS += -DSUNOS5 -D_REENTRANT + RPATH = sunos_x86 +endif + +#Path for HP-UX +ifeq ($(findstring HP-UX, $(UNIXNAME)), HP-UX) + ifeq ($CC, "gcc") + CFLAGS += -Wstrict-prototypes + endif + CFLAGS += -DHP_UX -DHPUX11 + PLAT_NAME=hp-ux +endif + +#Find system type. +ifneq ($(SYSPATH),) + CHECKSYSRES = @echo "System is $(shell uname -sm)" +endif +########################################################### + +ACL_PATH = ../../.. +CFLAGS += -I$(ACL_PATH)/lib_acl/include \ + -I$(ACL_PATH)/lib_protocol/include \ + -I$(ACL_PATH)/lib_acl_cpp/include \ + -I$(ACL_PATH)/lib_fiber/c/include \ + -I$(ACL_PATH)/lib_fiber/cpp/include +EXTLIBS = +LDFLAGS = -L$(ACL_PATH)/lib_fiber/lib -lfiber_cpp \ + -L$(ACL_PATH)/lib_acl_cpp/lib -lacl_cpp \ + -L$(ACL_PATH)/lib_protocol/lib -lprotocol \ + -L$(ACL_PATH)/lib_acl/lib -lacl -lfiber \ + $(EXTLIBS) $(SYSLIB) + +COMPILE = $(CC) $(CFLAGS) +LINK = $(CC) $(OBJ) $(LDFLAGS) +########################################################### +OBJ_PATH = . + +#Project's objs +SRC = $(wildcard *.cpp) +OBJ = $(patsubst %.cpp, $(OBJ_PATH)/%.o, $(notdir $(SRC))) + +$(shell mkdir -p ../../../dist/master/libexec/$(RPATH)/) + +$(OBJ_PATH)/%.o: %.cpp + $(COMPILE) $< -o $@ + +.PHONY = all clean +all: RM $(OBJ) + $(LINK) -o $(PROG) + @echo "" + @echo "All ok! Output:$(PROG)" + @echo "" +RM: + rm -f $(PROG) +clean: + rm -f $(PROG) + rm -f $(OBJ) +install: + cp $(PROG) ../../../dist/master/libexec/$(RPATH)/ + cp $(PROG).cf ../../../dist/master/conf/service/ +########################################################### diff --git a/app/wizard_demo/httpd_proxy/fiber_transfer.cpp b/app/wizard_demo/httpd_proxy/fiber_transfer.cpp new file mode 100644 index 000000000..0511d251e --- /dev/null +++ b/app/wizard_demo/httpd_proxy/fiber_transfer.cpp @@ -0,0 +1,42 @@ +#include "stdafx.h" +#include "fiber_transfer.h" + +fiber_transfer::fiber_transfer(acl::socket_stream& in, acl::socket_stream& out) +: in_(in), out_(out), peer_(NULL) +{ +} + +void fiber_transfer::set_peer(fiber_transfer& peer) +{ + peer_ = &peer; +} + +fiber_transfer::~fiber_transfer(void) +{ +} + +void fiber_transfer::wait(void) +{ + (void) box_.pop(); +} + +void fiber_transfer::run(void) +{ + char buf[8192]; + while (true) { + int ret = in_.read(buf, sizeof(buf) - 1, false); + if (ret == -1) { + break; + } + + if (out_.write(buf, ret) == -1) { + break; + } + } + + if (peer_) { + peer_->kill(); + } + + box_.push(NULL); +} diff --git a/app/wizard_demo/httpd_proxy/fiber_transfer.h b/app/wizard_demo/httpd_proxy/fiber_transfer.h new file mode 100644 index 000000000..05c9a8046 --- /dev/null +++ b/app/wizard_demo/httpd_proxy/fiber_transfer.h @@ -0,0 +1,32 @@ +#pragma once + +class fiber_transfer : public acl::fiber +{ +public: + fiber_transfer(acl::socket_stream& in, acl::socket_stream& out); + ~fiber_transfer(void); + + void set_peer(fiber_transfer& peer); + void wait(void); + + acl::socket_stream& get_input(void) const + { + return in_; + } + + acl::socket_stream& get_output(void) const + { + return out_; + } + +protected: + // @override + void run(void); + +private: + acl::fiber_tbox box_; + acl::socket_stream& in_; + acl::socket_stream& out_; + fiber_transfer* peer_; +}; + diff --git a/app/wizard_demo/httpd_proxy/http_servlet.cpp b/app/wizard_demo/httpd_proxy/http_servlet.cpp new file mode 100644 index 000000000..f4a76f2b8 --- /dev/null +++ b/app/wizard_demo/httpd_proxy/http_servlet.cpp @@ -0,0 +1,163 @@ +#include "stdafx.h" +#include "fiber_transfer.h" +#include "http_servlet.h" + +http_servlet::http_servlet(acl::socket_stream* stream, acl::session* session) +: acl::HttpServlet(stream, session) +{ + handlers_["/hello"] = &http_servlet::on_hello; +} + +http_servlet::~http_servlet(void) +{ +} + +bool http_servlet::doError(request_t&, response_t& res) +{ + res.setStatus(400); + res.setContentType("text/xml; charset=utf-8"); + + // 发送 http 响应体 + acl::string buf; + buf.format("\r\n"); + res.write(buf); + res.write(NULL, 0); + return false; +} + +bool http_servlet::doOther(request_t&, response_t& res, const char* method) +{ + res.setStatus(400); + res.setContentType("text/xml; charset=utf-8"); + // 发送 http 响应体 + acl::string buf; + buf.format("\r\n", method); + res.write(buf); + res.write(NULL, 0); + return false; +} + +bool http_servlet::doGet(request_t& req, response_t& res) +{ + return doPost(req, res); +} + +bool http_servlet::doPost(request_t& req, response_t& res) +{ + // 如果需要 http session 控制,请打开下面注释,且需要保证 + // 在 master_service.cpp 的函数 thread_on_read 中设置的 + // memcached 服务正常工作 + /* + const char* sid = req.getSession().getAttribute("sid"); + if (*sid == 0) + req.getSession().setAttribute("sid", "xxxxxx"); + sid = req.getSession().getAttribute("sid"); + */ + + // 如果需要取得浏览器 cookie 请打开下面注释 + /* + + */ + + const char* path = req.getPathInfo(); + handler_t handler = path && *path ? handlers_[path] : NULL; + return handler ? (this->*handler)(req, res) : on_default(req, res); +} + +bool http_servlet::on_default(request_t& req, response_t& res) +{ + return on_hello(req, res); +} + +bool http_servlet::on_hello(request_t& req, response_t& res) +{ + res.setContentType("text/html; charset=utf-8") // 设置响应字符集 + .setKeepAlive(req.isKeepAlive()) // 设置是否保持长连接 + .setContentEncoding(true) // 自动支持压缩传输 + .setChunkedTransferEncoding(true); // 采用 chunk 传输方式 + + acl::string buf; + buf.format("xxxxxxx
\r\n"); + if (res.write(buf) == false) { + printf("write error\r\n"); + return false; + } + + acl::json* json = req.getJson(); + if (json == NULL) { + printf("json null\r\n"); + } else { + printf("json is [%s]\r\n", json->to_string().c_str()); + } + for (size_t i = 0; i < 1; i++) + { + buf.format("hello world=%d
\r\n", (int) i); + if (res.write(buf) == false) { + printf("write error\r\n"); + return false; + } + + if (i % 10000 == 0) + { + sleep(1); + printf("i=%d\n", (int) i); + } + } + + buf = "
\r\n"; + printf("write ok\n"); + + return res.write(buf) && res.write(NULL, 0); +} + +bool http_servlet::doConnect(request_t& req, response_t& res) +{ + // CONNECT 127.0.0.1:22 HTTP/1.0 + // HTTP/1.1 200 Connection Established + + const char* host = req.getRemoteHost(); + if (host == NULL || *host == 0) { + printf("getRemoteHost null\r\n"); + return false; + } + printf("remote host=%s\r\n", host); + + acl::socket_stream peer; + if (peer.open(host, 0, 0) == false) { + printf("connect %s error %s\r\n", host, acl::last_serror()); + return false; + } + + //const char* ok = "HTTP/1.1 200 Connection Established\r\n"; + //acl::ostream& out = res.getOutputStream(); + + const char* ok = ""; + res.setContentLength(0); + if (res.write(ok) == false) { + return false; + } + + acl::socket_stream& local = req.getSocketStream(); + doProxy(local, peer); + return false; +} + +bool http_servlet::doProxy(acl::socket_stream& local, acl::socket_stream& peer) +{ + fiber_transfer fiber_local(local, peer); + fiber_transfer fiber_peer(peer, local); + + fiber_local.set_peer(fiber_peer); + fiber_peer.set_peer(fiber_local); + + fiber_local.start(); + fiber_peer.start(); + + fiber_local.wait(); + fiber_peer.wait(); + + printf("doProxy finished, local fd=%d, peer fd=%d\r\n", + fiber_local.get_input().sock_handle(), + fiber_local.get_output().sock_handle()); + return true; +} diff --git a/app/wizard_demo/httpd_proxy/http_servlet.h b/app/wizard_demo/httpd_proxy/http_servlet.h new file mode 100644 index 000000000..4fcea7e8d --- /dev/null +++ b/app/wizard_demo/httpd_proxy/http_servlet.h @@ -0,0 +1,32 @@ +#pragma once + +class http_servlet : public acl::HttpServlet +{ +public: + http_servlet(acl::socket_stream*, acl::session*); + ~http_servlet(); + +protected: + // @override + bool doGet(request_t&, response_t&); + + // @override + bool doPost(request_t&, response_t&); + + // @override + bool doError(request_t&, response_t&); + + // @override + bool doOther(request_t&, response_t&, const char* method); + + bool doConnect(request_t&, response_t&); + +private: + typedef bool (http_servlet::*handler_t)(request_t&,response_t&); + std::map handlers_; + + bool on_default(request_t&, response_t&); + bool on_hello(request_t&, response_t&); + + bool doProxy(acl::socket_stream& local, acl::socket_stream& peer); +}; diff --git a/app/wizard_demo/httpd_proxy/httpd_proxy.cf b/app/wizard_demo/httpd_proxy/httpd_proxy.cf new file mode 100644 index 000000000..4ac0c2d11 --- /dev/null +++ b/app/wizard_demo/httpd_proxy/httpd_proxy.cf @@ -0,0 +1,130 @@ + +service httpd_proxy +{ +# 杩涚▼鏄惁绂佹杩愯 + master_disable = no +# 鏈嶅姟鍦板潃鍙婄鍙e彿 +# for master_type = inet +# master_service = 127.0.0.1:5001 +# for master_type = unix +# master_service = echo.sock +# for master_type = sock + master_service = 8288 + +# 鏈嶅姟鐩戝惉涓哄煙濂楁帴鍙 +# master_service = aio_echo.sock +# 鏈嶅姟绫诲瀷 +# master_type = inet +# master_type = unix + master_type = sock + +# 鍋滄瀛愯繘绋嬫椂鏄惁閲囩敤寮鸿鍋滄鐨勬柟寮(鍗崇粰瀛愯繘绋嬪彂閫 SIGTERM 淇″彿) + master_stop_kill = false +# 褰 master_stop_kill 涓 true 鏃讹紝璇ラ厤缃喅瀹氭槸鍚﹁绛夊緟瀛愯繘绋嬮鍑 + master_stop_wait = false + +# 褰撶郴缁熸敮鎸 SO_REUSEPORT 鏃讹紝鏄惁鍚敤璇ュ姛鑳 + master_reuseport = yes +# 鏄惁閽堝鐩戝惉濂楁帴鍙h瀹氫负闈為樆濉炴柟寮 + master_nonblock = yes +# 褰撶郴缁熸敮鎸 TCP_FASTOPEN 鏃讹紝鏄惁鍚敤璇ュ姛鑳 + master_fastopen = no + +# 褰撳瓙杩涚▼寮傚父閫鍑烘椂锛屽鏋滆鍊奸潪绌猴紝鍒欏皢瀛愯繘绋嬪紓甯搁鍑虹殑娑堟伅閫氱煡璇ユ湇鍔 +# master_notify_addr = 127.0.0.1:5801 +# 閭欢閫氱煡鎺ユ敹鑰 +# master_notify_recipients = zhengshuxin@hotmail.com + +# 鏄惁鍏佽寤惰繜鎺ュ彈瀹㈡埛绔繛鎺ワ紝濡傛灉涓0鍒欒〃绀哄叧闂鍔熻兘锛屽鏋滃ぇ浜0鍒欒〃绀烘墦寮姝ゅ姛鑳 +# 骞朵笖姝ゅ间唬琛ㄥ欢杩熸帴鍙楄繛鎺ョ殑瓒呮椂鍊硷紝瓒呰繃姝ゅ兼椂濡傛灉瀹㈡埛绔緷鐒舵病鏈夊彂鏉ユ暟鎹紝鍒欐搷浣 +# 绯荤粺浼氬湪绯荤粺灞傜洿鎺ュ叧闂杩炴帴 +# master_defer_accept = 0 +# 鏄惁鍙厑璁哥鏈夎闂, 濡傛灉涓 y, 鍒欏煙濂楁帴鍙e垱寤哄湪 {install_path}/var/log/private/ 鐩綍涓, +# 濡傛灉涓 n, 鍒欏煙濂楁帴鍙e垱寤哄湪 {install_path}/var/log/public/ 鐩綍涓, + master_private = n + master_unpriv = n +# 鏄惁闇瑕 chroot: n -- no, y -- yes + master_chroot = n +# 姣忛殧澶氶暱鏃堕棿瑙﹀彂涓娆★紝鍗曚綅涓虹(浠呭 trigger 妯″紡鏈夋晥) + master_wakeup = - +# 鏈澶ц繘绋嬫暟 + master_maxproc = 1 +# 棰勫惎鍔ㄨ繘绋嬫暟锛岃鍊间笉寰楀ぇ浜 master_maxproc + master_prefork = 1 +# 杩涚▼绋嬪簭鍚 + master_command = httpd_proxy +# 杩涚▼鏃ュ織璁板綍鏂囦欢 + master_log = {install_path}/var/log/httpd_proxy +# 璋冭瘯鏃ュ織鏂瑰紡锛屾牸寮忥細tag:level; tag:level; tab:level, 濡傦細all:1; 101:2 +# master_debug = +# 杩涚▼鍚姩鍙傛暟锛屽彧鑳戒负: -u [鏄惁鍏佽浠ユ煇鏅氱敤鎴风殑韬唤杩愯] +# master_args = +# 浼犻掔粰鏈嶅姟瀛愯繘绋嬬殑鐜鍙橀噺, 鍙互閫氳繃 getenv("SERVICE_ENV") 鑾峰緱姝ゅ +# master_env = mempool_limit:512000000 +# 褰撳惎鍔ㄥ涓瓙杩涚▼瀹炰緥鏃讹紝璇ュ紑鍏虫帶鍒跺涓瓙杩涚▼鍦ㄦ帴鏀惰繛鎺ユ椂鏄惁鍚 acl_master 鍙戦佹秷鎭姤鍛婅嚜宸辩殑鐘舵 +# master_status_notify = 1 +# 鏄惁鍏佽浜х敓 core 鏂囦欢 +# fiber_enable_core = 1 +# core 鏂囦欢澶у皬闄愬埗锛-1 琛ㄧず涓嶉檺鍒 core 鏂囦欢澶у皬锛0 琛ㄧず绂佹浜х敓 core锛> 0 琛ㄧず core 鏂囦欢鏈澶уぇ灏 +# fiber_core_limit = -1 +# 杩涚▼閫鍑烘椂鏄惁绂佹浜х敓 core 鏂囦欢 +# fiber_disable_core_onexit = 1 +# 姣忎釜杩涚▼瀹炰緥澶勭悊杩炴帴鏁扮殑鏈澶ф鏁帮紝瓒呰繃姝ゅ煎悗杩涚▼瀹炰緥涓诲姩閫鍑 + fiber_use_limit = 0 +# 姣忎釜杩涚▼瀹炰緥鐨勭┖闂茶秴鏃舵椂闂达紝瓒呰繃姝ゅ煎悗杩涚▼瀹炰緥涓诲姩閫鍑 + fiber_idle_limit = 0 +# 姣忎釜杩涚▼鍚姩鐨勭嚎绋嬫暟 + fiber_threads = 1 +# 杩涚▼杩愯鏃舵墍鍦ㄧ殑璺緞 + fiber_queue_dir = {install_path}/var +# 璇诲啓瓒呮椂鏃堕棿, 鍗曚綅涓虹 + fiber_rw_timeout = 120 +# 璇荤紦鍐插尯鐨勭紦鍐插尯澶у皬 + fiber_buf_size = 8192 +# 杩涚▼杩愯鏃剁殑鐢ㄦ埛韬唤 + fiber_owner = root + +# 褰撳惎鐢 master_dispatch 杩炴帴鍒嗗紑鏈嶅姟鍚庯紝璇ラ厤缃寚瀹 master_dispatch 鎵鐩戝惉鐨 +# 鍩熷鎺ュ彛鐨勫叏璺緞锛岃繖鏍锋湰瀛愯繘绋嬪氨鍙互浠 master_dispatch 鑾峰緱瀹㈡埛绔繛鎺 +# fiber_dispatch_addr = {install_path}/var/private/dispatch.sock +# 褰 fiber_dispatch_addr 寮鍚悗锛屼笅闈㈠弬鏁版帶鍒舵湰鏈嶅姟杩涚▼鍙戠粰鍓嶇 master_dispatch 鐨勬湇鍔℃爣璇嗕俊鎭 +# fiber_dispatch_type = default + +# 绾跨▼鐨勫爢鏍堢┖闂村ぇ灏忥紝鍗曚綅涓哄瓧鑺 + fiber_stack_size = 64000 +# 鍏佽璁块棶 udserver 鐨勫鎴风IP鍦板潃鑼冨洿 +# fiber_access_allow = 127.0.0.1:255.255.255.255, 127.0.0.1:127.0.0.1 + fiber_access_allow = all + +# 褰 acl_master 閫鍑烘椂锛屽鏋滆鍊肩疆1鍒欒绋嬪簭涓嶇瓑鎵鏈夎繛鎺ュ鐞嗗畬姣曚究绔嬪嵆閫鍑 + fiber_quick_abort = 1 + +# 褰 fiber_quick_abort 涓 0 涓旀湰閰嶇疆椤瑰ぇ浜 0 鏃讹紝璇ラ厤缃」鎵嶆湁鏁堬紝鎸囧畾浜 +# 鏈繘绋嬪湪鎵鏈夎繛鎺ラ鍑哄墠鐨勬渶澶х瓑寰呮椂闂(绉) + fiber_wait_limit = 0 + +############################################################################ +# 搴旂敤鑷繁鐨勯厤缃夐」 + +# mysql 鏈嶅姟鍦板潃 +# mysql_dbaddr = /tmp/mysql.sock +# mysql_dbaddr = 10.0.250.199:3306 +# 杩炴帴 mysql 鏁版嵁搴撶殑杩炴帴姹犵殑鏈澶у +# mysql_dbmax = 200 +# ping mysql 杩炴帴鐨勯棿闅旀椂闂, 浠ョ涓哄崟浣 +# mysql_dbping = 10 +# mysql 杩炴帴绌洪棽鐨勬椂闂撮棿闅, 浠ョ涓哄崟浣 +# mysql_dbtimeout = 30 + +# 鏁版嵁搴撳悕绉 +# mysql_dbname = fiber_db +# 鏁版嵁搴撹闂敤鎴 +# mysql_dbuser = fiber_user +# 鏁版嵁搴撶敤鎴疯闂瘑鐮 +# mysql_dbpass = 111111 + +# 鏄惁杈撳嚭褰撳墠鍐呭瓨鐨勭姸鎬佷俊鎭 +# debug_mem = 1 +# 鏄惁鍦ㄤ竴涓嚎绋嬩腑杩炴帴璇 +# loop_read = 1 +} diff --git a/app/wizard_demo/httpd_proxy/main.cpp b/app/wizard_demo/httpd_proxy/main.cpp new file mode 100644 index 000000000..43b1f3709 --- /dev/null +++ b/app/wizard_demo/httpd_proxy/main.cpp @@ -0,0 +1,31 @@ +#include "stdafx.h" +#include "master_service.h" + +int main(int argc, char *argv[]) +{ + acl::acl_cpp_init(); + master_service& ms = acl::singleton2::get_instance(); + + // 璁剧疆閰嶇疆鍙傛暟琛 + ms.set_cfg_int(var_conf_int_tab); + ms.set_cfg_int64(var_conf_int64_tab); + ms.set_cfg_str(var_conf_str_tab); + ms.set_cfg_bool(var_conf_bool_tab); + + if (argc >= 2 && strcasecmp(argv[1], "alone") == 0) + { + const char* addr = ":8288"; + + acl::log::stdout_open(true); + + if (argc >= 4) + addr = argv[3]; + printf("listen: %s\r\n", addr); + + ms.run_alone(addr, argc >= 3 ? argv[2] : NULL); + } + else + ms.run_daemon(argc, argv); + + return 0; +} diff --git a/app/wizard_demo/httpd_proxy/master_service.cpp b/app/wizard_demo/httpd_proxy/master_service.cpp new file mode 100644 index 000000000..93b0dabf0 --- /dev/null +++ b/app/wizard_demo/httpd_proxy/master_service.cpp @@ -0,0 +1,85 @@ +#include "stdafx.h" +#include "http_servlet.h" +#include "master_service.h" + +static char *var_cfg_str; + +acl::master_str_tbl var_conf_str_tab[] = { + { "str", "test_msg", &var_cfg_str }, + + { 0, 0, 0 } +}; + +static int var_cfg_debug_enable; + +acl::master_bool_tbl var_conf_bool_tab[] = { + { "debug_enable", 1, &var_cfg_debug_enable }, + + { 0, 0, 0 } +}; + +static int var_cfg_io_timeout; + +acl::master_int_tbl var_conf_int_tab[] = { + { "io_timeout", 120, &var_cfg_io_timeout, 0, 0 }, + + { 0, 0 , 0 , 0, 0 } +}; + +acl::master_int64_tbl var_conf_int64_tab[] = { + { 0, 0 , 0 , 0, 0 } +}; + + +////////////////////////////////////////////////////////////////////////// + +master_service::master_service(void) +{ +} + +master_service::~master_service(void) +{ +} + +void master_service::on_accept(acl::socket_stream& conn) +{ + logger("connect from %s, fd %d", conn.get_peer(), conn.sock_handle()); + + conn.set_rw_timeout(120); + + acl::memcache_session session("127.0.0.1:11211"); + http_servlet servlet(&conn, &session); + + // charset: big5, gb2312, gb18030, gbk, utf-8 + servlet.setLocalCharset("utf-8"); + + while(servlet.doRun()) {} + + logger("disconnect from %s", conn.get_peer()); +} + +void master_service::proc_pre_jail(void) +{ + logger(">>>proc_pre_jail<<<"); +} + +void master_service::proc_on_listen(acl::server_socket& ss) +{ + logger(">>>listen %s ok<<<", ss.get_addr()); +} + +void master_service::proc_on_init(void) +{ + logger(">>>proc_on_init<<<"); +} + +void master_service::proc_on_exit(void) +{ + logger(">>>proc_on_exit<<<"); +} + +bool master_service::proc_on_sighup(acl::string&) +{ + logger(">>>proc_on_sighup<<<"); + return true; +} diff --git a/app/wizard_demo/httpd_proxy/master_service.h b/app/wizard_demo/httpd_proxy/master_service.h new file mode 100644 index 000000000..ca5675e29 --- /dev/null +++ b/app/wizard_demo/httpd_proxy/master_service.h @@ -0,0 +1,36 @@ +#pragma once + +extern acl::master_str_tbl var_conf_str_tab[]; +extern acl::master_bool_tbl var_conf_bool_tab[]; +extern acl::master_int_tbl var_conf_int_tab[]; +extern acl::master_int64_tbl var_conf_int64_tab[]; + +////////////////////////////////////////////////////////////////////////// + +class master_service : public acl::master_fiber +{ +public: + master_service(void); + ~master_service(void); + +protected: + // @override + void on_accept(acl::socket_stream& conn); + + // @override + void proc_pre_jail(void); + + // @override + void proc_on_listen(acl::server_socket& ss); + + // @override + void proc_on_init(void); + + // @override + void proc_on_exit(void); + + // @override + bool proc_on_sighup(acl::string&); + +private: +}; diff --git a/app/wizard_demo/httpd_proxy/stdafx.cpp b/app/wizard_demo/httpd_proxy/stdafx.cpp new file mode 100644 index 000000000..f01a2ff42 --- /dev/null +++ b/app/wizard_demo/httpd_proxy/stdafx.cpp @@ -0,0 +1,8 @@ +// stdafx.cpp : 只包括标准包含文件的源文件 +// master_threads.pch 将成为预编译头 +// stdafx.obj 将包含预编译类型信息 + +#include "stdafx.h" + +// TODO: 在 STDAFX.H 中 +//引用任何所需的附加头文件,而不是在此文件中引用 diff --git a/app/wizard_demo/httpd_proxy/stdafx.h b/app/wizard_demo/httpd_proxy/stdafx.h new file mode 100644 index 000000000..a127ef2d8 --- /dev/null +++ b/app/wizard_demo/httpd_proxy/stdafx.h @@ -0,0 +1,63 @@ +// stdafx.h : 标准系统包含文件的包含文件, +// 或是常用但不常更改的项目特定的包含文件 +// + +#pragma once + + +//#include +//#include + +// TODO: 在此处引用程序要求的附加头文件 + +#include "lib_acl.h" +#include "lib_protocol.h" +#include "acl_cpp/lib_acl.hpp" +#include "fiber/lib_fiber.h" +#include "fiber/lib_fiber.hpp" + +#ifdef WIN32 +#define snprintf _snprintf +#endif + +typedef acl::HttpServletRequest request_t; +typedef acl::HttpServletResponse response_t; + +#undef logger +#undef logger_warn +#undef logger_error +#undef logger_fatal +#undef logger_debug + +#if defined(_WIN32) || defined(_WIN64) + +# if _MSC_VER >= 1500 +# define logger(fmt, ...) \ + acl::log::msg4(__FILE__, __LINE__, __FUNCTION__, fmt, __VA_ARGS__) +# define logger_warn(fmt, ...) \ + acl::log::warn4(__FILE__, __LINE__, __FUNCTION__, fmt, __VA_ARGS__) +# define logger_error(fmt, ...) \ + acl::log::error4(__FILE__, __LINE__, __FUNCTION__, fmt, __VA_ARGS__) +# define logger_fatal(fmt, ...) \ + acl::log::fatal4(__FILE__, __LINE__, __FUNCTION__, fmt, __VA_ARGS__) +# define logger_debug(section, level, fmt, ...) \ + acl::log::msg6(section, level, __FILE__, __LINE__, __FUNCTION__, fmt, __VA_ARGS__) +# else +# define logger acl::log::msg1 +# define logger_warn acl::log::warn1 +# define logger_error acl::log::error1 +# define logger_fatal acl::log::fatal1 +# define logger_debug acl::log::msg3 +# endif +#else +# define logger(fmt, args...) \ + acl::log::msg4(__FILE__, __LINE__, __FUNCTION__, fmt, ##args) +# define logger_warn(fmt, args...) \ + acl::log::warn4(__FILE__, __LINE__, __FUNCTION__, fmt, ##args) +# define logger_error(fmt, args...) \ + acl::log::error4(__FILE__, __LINE__, __FUNCTION__, fmt, ##args) +# define logger_fatal(fmt, args...) \ + acl::log::fatal4(__FILE__, __LINE__, __FUNCTION__, fmt, ##args) +# define logger_debug(section, level, fmt, args...) \ + acl::log::msg6(section, level, __FILE__, __LINE__, __FUNCTION__, fmt, ##args) +#endif // !_WIN32 && !_WIN64 diff --git a/app/wizard_demo/httpd_proxy/valgrind.sh b/app/wizard_demo/httpd_proxy/valgrind.sh new file mode 100755 index 000000000..625e1f210 --- /dev/null +++ b/app/wizard_demo/httpd_proxy/valgrind.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +valgrind --tool=memcheck --leak-check=yes -v ./httpd_proxy alone httpd_proxy.cf