├── .gitignore
├── LICENSE
├── Makefile
├── README.md
├── doc
├── rpc.doc
├── rpc_inspecting.doc
└── rpc_message.txt
├── gen_env_conf.sh
├── samples
├── Makefile
├── async_client
│ ├── ReadMe.txt
│ ├── async_client.cpp
│ ├── async_client.vcproj
│ ├── stdafx.cpp
│ └── stdafx.h
├── async_server
│ ├── ReadMe.txt
│ ├── async_server.cpp
│ ├── async_server.vcproj
│ ├── stdafx.cpp
│ └── stdafx.h
├── echo.h
├── echo_client
│ ├── ReadMe.txt
│ ├── echo_client.cpp
│ ├── echo_client.vcproj
│ ├── echo_client.vcproj.leipeng-PC.leipeng.user
│ ├── stdafx.cpp
│ └── stdafx.h
├── echo_server
│ ├── echo_server.cpp
│ ├── echo_server.vcproj
│ ├── echo_server.vcproj.leipeng-PC.leipeng.user
│ ├── stdafx.cpp
│ └── stdafx.h
├── employee.h
├── employee_client
│ ├── ReadMe.txt
│ ├── employee_client.cpp
│ ├── employee_client.vcproj
│ ├── employee_client.vcproj.leipeng-PC.leipeng.user
│ ├── stdafx.cpp
│ └── stdafx.h
├── employee_server
│ ├── ReadMe.txt
│ ├── employee_server.cpp
│ ├── employee_server.vcproj
│ ├── employee_server.vcproj.leipeng-PC.leipeng.user
│ ├── stdafx.cpp
│ └── stdafx.h
├── file_client
│ ├── ReadMe.txt
│ ├── file_client.cpp
│ ├── file_client.vcproj
│ ├── stdafx.cpp
│ └── stdafx.h
├── file_server
│ ├── file_server.cpp
│ ├── file_server.vcproj
│ ├── stdafx.cpp
│ ├── stdafx.h
│ └── test.txt
├── ifile.h
├── test.h
└── test_rpc.sln
└── src
└── nark
├── inet
├── MessageInputStream.cpp
├── MessageInputStream.hpp
├── ReactAcceptor.hpp
├── SocketStream.cpp
├── SocketStream.hpp
├── epoll_reactor.hpp
└── reactor.hpp
├── io
├── access_byid.cpp
└── access_byid.hpp
└── rpc
├── arg_traits.hpp
├── client.cpp
├── client.hpp
├── client_io.hpp
├── doxygen_doc.hpp
├── pp_arglist_type.hpp
├── pp_client_stub.hpp
├── pp_server_stub.hpp
├── rpc_basic.cpp
├── rpc_basic.hpp
├── rpc_interface.hpp
├── server.cpp
├── server.hpp
├── server_io.cpp
└── server_io.hpp
/.gitignore:
--------------------------------------------------------------------------------
1 | # Compiled Object files
2 | *.slo
3 | *.lo
4 | *.o
5 | *.obj
6 |
7 | # Precompiled Headers
8 | *.gch
9 | *.pch
10 |
11 | # Compiled Dynamic libraries
12 | *.so
13 | *.dylib
14 | *.dll
15 |
16 | # Fortran module files
17 | *.mod
18 |
19 | # Compiled Static libraries
20 | *.lai
21 | *.la
22 | *.a
23 | *.lib
24 |
25 | # Executables
26 | *.exe
27 | *.out
28 | *.app
29 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | DBG_FLAGS ?= -g3 -D_DEBUG
2 | RLS_FLAGS ?= -O3 -DNDEBUG
3 |
4 | ifeq "$(origin LD)" "default"
5 | LD := ${CXX}
6 | endif
7 |
8 | COMPILER := $(shell ${CXX} --version | head -1 | awk '{split($$3, Ver, "."); printf("%s-%d.%d", $$1, Ver[1], Ver[2]);}')
9 | #$(error COMPILER=${COMPILER})
10 | UNAME_MachineSystem := $(shell uname -m -s | sed 's:[ /]:-:g')
11 | BUILD_ROOT := build/${COMPILER}-${UNAME_MachineSystem}
12 | ddir:=${BUILD_ROOT}/dbg
13 | rdir:=${BUILD_ROOT}/rls
14 |
15 | gen_sh := $(dir $(lastword ${MAKEFILE_LIST}))gen_env_conf.sh
16 |
17 | err := $(shell env BDB_HOME=${BDB_HOME} BOOST_INC=${BOOST_INC} bash ${gen_sh} "${CXX}" ${COMPILER} ${BUILD_ROOT}/env.mk; echo $$?)
18 | ifneq "${err}" "0"
19 | $(error err = ${err} MAKEFILE_LIST = ${MAKEFILE_LIST}, PWD = ${PWD}, gen_sh = ${gen_sh} "${CXX}" ${COMPILER} ${BUILD_ROOT}/env.mk)
20 | endif
21 |
22 | NARK_INC := -Isrc
23 | NARK_INC += -I../nark-bone/src
24 | NARK_INC += -I../nark-hashmap/src
25 | NARK_INC += -I../nark-serialization/src
26 |
27 | include ${BUILD_ROOT}/env.mk
28 |
29 | UNAME_System := $(shell uname | sed 's/^\([0-9a-zA-Z]*\).*/\1/')
30 | ifeq (CYGWIN, ${UNAME_System})
31 | FPIC =
32 | # lazy expansion
33 | CYGWIN_LDFLAGS = -Wl,--out-implib=$@ \
34 | -Wl,--export-all-symbols \
35 | -Wl,--enable-auto-import
36 | DLL_SUFFIX = .dll.a
37 | CYG_DLL_FILE = $(shell echo $@ | sed 's:\(.*\)/lib\([^/]*\)\.a$$:\1/cyg\2:')
38 | else
39 | FPIC = -fPIC
40 | DLL_SUFFIX = .so
41 | CYG_DLL_FILE = $@
42 | endif
43 | override CFLAGS += ${FPIC}
44 | override CXXFLAGS += ${FPIC}
45 | override LDFLAGS += ${FPIC}
46 |
47 | ifeq "$(shell expr substr ${COMPILER} 1 3)" "g++"
48 | override LDFLAGS += -rdynamic
49 | override CXXFLAGS += -time
50 | ifeq "$(shell echo ${COMPILER} | awk -F- '{if ($$2 >= 4.8) print 1;}')" "1"
51 | CXX_STD := -std=gnu++1y
52 | endif
53 | endif
54 |
55 | ifeq "${CXX_STD}" ""
56 | CXX_STD := -std=gnu++11
57 | endif
58 |
59 | # icc or icpc
60 | ifeq "$(shell expr substr ${COMPILER} 1 2)" "ic"
61 | override CXXFLAGS += -xHost -fasm-blocks
62 | CPU = -xHost
63 | else
64 | CPU = -march=native
65 | COMMON_C_FLAGS += -Wno-deprecated-declarations
66 | COMMON_C_FLAGS += -fstrict-aliasing
67 | COMMON_C_FLAGS += -Wstrict-aliasing=3
68 | endif
69 |
70 | COMMON_C_FLAGS = -Wformat=2 -Wcomment
71 | COMMON_C_FLAGS += -Wall -Wextra
72 | COMMON_C_FLAGS += -Wno-unused-parameter
73 | COMMON_C_FLAGS += -D_GNU_SOURCE
74 |
75 | #-v #-Wall -Wparentheses
76 | #COMMON_C_FLAGS += ${COMMON_C_FLAGS} -Wpacked -Wpadded -v
77 | #COMMON_C_FLAGS += ${COMMON_C_FLAGS} -Winvalid-pch
78 | #COMMON_C_FLAGS += ${COMMON_C_FLAGS} -fmem-report
79 |
80 | ifeq "$(shell expr substr ${COMPILER} 1 5)" "clang"
81 | COMMON_C_FLAGS += -fcolor-diagnostics
82 | endif
83 |
84 | #CXXFLAGS +=
85 | #CXXFLAGS += -fpermissive
86 | #CXXFLAGS += -fexceptions
87 | #CXXFLAGS += -fdump-translation-unit -fdump-class-hierarchy
88 |
89 | override CFLAGS += ${COMMON_C_FLAGS}
90 | override CXXFLAGS += ${COMMON_C_FLAGS}
91 |
92 | DEFS := -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
93 | override CFLAGS += ${DEFS}
94 | override CXXFLAGS += ${DEFS}
95 |
96 | override INCS := ${NARK_INC} ${INCS}
97 |
98 | ifeq (, $(findstring ${BOOST_INC}, ${INCS} /usr/include /usr/local/include))
99 | override INCS += -I${BOOST_INC}
100 | endif
101 |
102 | ifeq (, $(findstring ${BOOST_LIB}, /usr/lib64 /usr/lib /usr/local/lib))
103 | override LIBS += -L${BOOST_LIB}
104 | endif
105 |
106 | LIBS += -L../nark-bone/lib
107 | LIBS += -L../nark-serialization/lib
108 |
109 | #override INCS += -I/usr/include
110 |
111 | LIBBOOST ?= \
112 | -lboost_thread${BOOST_SUFFIX} \
113 | -lboost_date_time${BOOST_SUFFIX} \
114 | -lboost_system${BOOST_SUFFIX}
115 |
116 | LIBS += -L/usr/local/lib64 -L/usr/local/lib
117 | LIBS += -L/usr/lib64 -L/usr/lib
118 |
119 | extf = -pie -fno-stack-protector
120 | #extf+=-fno-stack-protector-all
121 | override CFLAGS += ${extf}
122 | #override CFLAGS += -g3
123 | override CXXFLAGS += ${extf}
124 | #override CXXFLAGS += -g3
125 | #CXXFLAGS += -fnothrow-opt
126 |
127 | override INCS += -I${BDB_HOME}/include
128 | LIBS += -L${BDB_HOME}/lib
129 |
130 | ifeq (, ${prefix})
131 | ifeq (root, ${USER})
132 | prefix := /usr
133 | else
134 | prefix := /home/${USER}
135 | endif
136 | endif
137 |
138 | #$(warning prefix=${prefix} BDB_HOME=${BDB_HOME}: LIBS=${LIBS})
139 |
140 | rpc_src := \
141 | $(wildcard src/nark/util/*.cpp) \
142 | $(wildcard src/nark/inet/*.cpp) \
143 | $(wildcard src/nark/io/*.cpp) \
144 | $(wildcard src/nark/rpc/*.cpp)
145 |
146 | #function definition
147 | #@param:${1} -- targets var prefix, such as bdb_util | bone
148 | #@param:${2} -- build type: d | r
149 | objs = $(addprefix ${${2}dir}/, $(addsuffix .o, $(basename ${${1}_src})))
150 |
151 | rpc_d_o := $(call objs,rpc,d)
152 | rpc_r_o := $(call objs,rpc,r)
153 | rpc_d := lib/libnark-rpc-${COMPILER}-d${DLL_SUFFIX}
154 | rpc_r := lib/libnark-rpc-${COMPILER}-r${DLL_SUFFIX}
155 | static_rpc_d := lib/libnark-rpc-${COMPILER}-d.a
156 | static_rpc_r := lib/libnark-rpc-${COMPILER}-r.a
157 |
158 | ALL_TARGETS = rpc
159 | DBG_TARGETS = ${rpc_d}
160 | RLS_TARGETS = ${rpc_r}
161 |
162 | .PHONY : rpc
163 | rpc: ${rpc_d} ${rpc_r} ${static_rpc_d} ${static_rpc_r}
164 |
165 | allsrc = ${rpc_src}
166 | alldep = $(addprefix ${rdir}/, $(addsuffix .dep, $(basename ${allsrc}))) \
167 | $(addprefix ${ddir}/, $(addsuffix .dep, $(basename ${allsrc})))
168 |
169 | .PHONY : dbg rls
170 | dbg: ${DBG_TARGETS}
171 | rls: ${RLS_TARGETS}
172 |
173 | ${rpc_d} ${rpc_r} : LIBS += ${LIBBOOST} -lpthread
174 | ${rpc_d} : LIBS += -lnark-serialization-${COMPILER}-d -lnark-bone-${COMPILER}-d
175 | ${rpc_r} : LIBS += -lnark-serialization-${COMPILER}-r -lnark-bone-${COMPILER}-r
176 |
177 | ${rpc_d} : $(call objs,rpc,d)
178 | ${rpc_r} : $(call objs,rpc,r)
179 | ${static_rpc_d} : $(call objs,rpc,d)
180 | ${static_rpc_r} : $(call objs,rpc,r)
181 |
182 | %${DLL_SUFFIX}:
183 | @echo "----------------------------------------------------------------------------------"
184 | @echo "Creating dynamic library: $@"
185 | @echo BOOST_INC=${BOOST_INC} BOOST_SUFFIX=${BOOST_SUFFIX}
186 | @echo -e "OBJS:" $(addprefix "\n ",$(sort $(filter %.o,$^)))
187 | @echo -e "LIBS:" $(addprefix "\n ",${LIBS})
188 | @mkdir -p lib
189 | @ln -sfT $(notdir $@) $(subst -${COMPILER},, $@)
190 | @${LD} -shared $(sort $(filter %.o,$^)) ${LDFLAGS} ${LIBS} -o ${CYG_DLL_FILE} ${CYGWIN_LDFLAGS}
191 | ifeq (CYGWIN, ${UNAME_System})
192 | @cp -l -f ${CYG_DLL_FILE} /usr/bin
193 | endif
194 |
195 | %.a:
196 | @echo "----------------------------------------------------------------------------------"
197 | @echo "Creating static library: $@"
198 | @echo BOOST_INC=${BOOST_INC} BOOST_SUFFIX=${BOOST_SUFFIX}
199 | @echo -e "OBJS:" $(addprefix "\n ",$(sort $(filter %.o,$^)))
200 | @echo -e "LIBS:" $(addprefix "\n ",${LIBS})
201 | @mkdir -p lib
202 | @ln -sfT $(notdir $@) $(subst -${COMPILER},, $@)
203 | @${AR} rcs $@ $(filter %.o,$^)
204 |
205 | .PHONY : clean
206 | clean:
207 | -rm -rf lib/libnark* ${BUILD_ROOT} ${PRECOMPILED_HEADER_GCH}
208 |
209 | .PHONY : depends
210 | depends : ${alldep}
211 |
212 | -include ${alldep}
213 |
214 | ${ddir}/%.o: %.cpp
215 | @echo file: $< "->" $@
216 | @echo NARK_INC=${NARK_INC}
217 | @echo BOOST_INC=${BOOST_INC} BOOST_SUFFIX=${BOOST_SUFFIX}
218 | mkdir -p $(dir $@)
219 | ${CXX} ${CXX_STD} ${CPU} -c ${DBG_FLAGS} ${CXXFLAGS} ${INCS} $< -o $@
220 |
221 | ${rdir}/%.o: %.cpp
222 | @echo file: $< "->" $@
223 | @echo NARK_INC=${NARK_INC}
224 | @echo BOOST_INC=${BOOST_INC} BOOST_SUFFIX=${BOOST_SUFFIX}
225 | mkdir -p $(dir $@)
226 | ${CXX} ${CXX_STD} ${CPU} -c ${RLS_FLAGS} ${CXXFLAGS} ${INCS} $< -o $@
227 |
228 | ${ddir}/%.o: %.cc
229 | @echo file: $< "->" $@
230 | @echo NARK_INC=${NARK_INC}
231 | @echo BOOST_INC=${BOOST_INC} BOOST_SUFFIX=${BOOST_SUFFIX}
232 | mkdir -p $(dir $@)
233 | ${CXX} ${CXX_STD} ${CPU} -c ${DBG_FLAGS} ${CXXFLAGS} ${INCS} $< -o $@
234 |
235 | ${rdir}/%.o: %.cc
236 | @echo file: $< "->" $@
237 | @echo NARK_INC=${NARK_INC}
238 | @echo BOOST_INC=${BOOST_INC} BOOST_SUFFIX=${BOOST_SUFFIX}
239 | mkdir -p $(dir $@)
240 | ${CXX} ${CXX_STD} ${CPU} -c ${RLS_FLAGS} ${CXXFLAGS} ${INCS} $< -o $@
241 |
242 | ${ddir}/%.o : %.c
243 | @echo file: $< "->" $@
244 | mkdir -p $(dir $@)
245 | ${CC} -c ${CPU} ${DBG_FLAGS} ${CFLAGS} ${INCS} $< -o $@
246 |
247 | ${rdir}/%.o : %.c
248 | @echo file: $< "->" $@
249 | mkdir -p $(dir $@)
250 | ${CC} -c ${CPU} ${RLS_FLAGS} ${CFLAGS} ${INCS} $< -o $@
251 |
252 | ${ddir}/%.s : %.cpp ${PRECOMPILED_HEADER_GCH}
253 | @echo file: $< "->" $@
254 | ${CXX} -S ${CPU} ${DBG_FLAGS} ${CXXFLAGS} ${INCS} $< -o $@
255 |
256 | ${rdir}/%.s : %.cpp ${PRECOMPILED_HEADER_GCH}
257 | @echo file: $< "->" $@
258 | ${CXX} -S ${CPU} ${RLS_FLAGS} ${CXXFLAGS} ${INCS} $< -o $@
259 |
260 | ${ddir}/%.s : %.c ${PRECOMPILED_HEADER_GCH}
261 | @echo file: $< "->" $@
262 | ${CC} -S ${CPU} ${DBG_FLAGS} ${CXXFLAGS} ${INCS} $< -o $@
263 |
264 | ${rdir}/%.s : %.c ${PRECOMPILED_HEADER_GCH}
265 | @echo file: $< "->" $@
266 | ${CC} -S ${CPU} ${RLS_FLAGS} ${CXXFLAGS} ${INCS} $< -o $@
267 |
268 | ${rdir}/%.dep : %.c
269 | @echo file: $< "->" $@
270 | @echo INCS = ${INCS}
271 | mkdir -p $(dir $@)
272 | ${CC} -M -MT $(basename $@).o ${INCS} $< > $@
273 |
274 | ${ddir}/%.dep : %.c
275 | @echo file: $< "->" $@
276 | @echo INCS = ${INCS}
277 | mkdir -p $(dir $@)
278 | ${CC} -M -MT $(basename $@).o ${INCS} $< > $@
279 |
280 | ${rdir}/%.dep : %.cpp
281 | @echo file: $< "->" $@
282 | @echo INCS = ${INCS}
283 | mkdir -p $(dir $@)
284 | ${CXX} -M -MT $(basename $@).o ${INCS} $< > $@
285 |
286 | ${ddir}/%.dep : %.cpp
287 | @echo file: $< "->" $@
288 | @echo INCS = ${INCS}
289 | mkdir -p $(dir $@)
290 | ${CXX} -M -MT $(basename $@).o ${INCS} $< > $@
291 |
292 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | nark-rpc
2 | ========
3 | ## Now the newest version is in [topling-ark](https://github.com/topling/topling-ark)
4 |
5 | RPC(Remote Procedure Call) on top of nark-serialization
6 |
7 | ## Prerequisite
8 |
9 |
10 |
11 | boost-1.41 or newer |
12 |
13 |
14 | -
15 | Require `boost_thread`, `boost_date_time`, `boost_system` to be built
16 |
17 | -
18 | Other boost libraries are used as header-only
19 |
20 | |
22 |
23 | nark-serialization |
24 | Require binary library |
25 |
26 |
27 | nark-hashmap |
28 | header only |
29 |
30 |
31 | nark-bone |
32 | Require binary library |
33 |
34 |
35 |
36 |
37 | **Note:** All `nark` repositories should be in the same directory.
38 |
39 | ## Compile
40 | 1. Compile `boost_thread`, `boost_date_time`, `boost_system`
41 | 2. Compile
42 | ```bash
43 | $ cd /path/to/nark-bone
44 | $ make
45 | $ cd ../nark-serialization
46 | $ make
47 | $ cd ../nark-rpc
48 | $ make
49 | $ cd samples # /path/to/nark-rpc/samples
50 | $ make
51 | $ build/*/dbg/echo_server/echo_server.exe # run echo server
52 | $ #
53 | $ # open a new terminal
54 | $ build/*/dbg/echo_client/echo_client.exe # run echo client
55 | $ # Have Fun!
56 | ```
57 |
58 | ## Quick Start
59 | ### IDL
60 | nark-rpc are all in C++, even its IDL is C++, `samples/ifile.h` is a good example:
61 |
62 | ```c++
63 | BEGIN_RPC_INTERFACE(FileObj, SessionScope)
64 | RPC_ADD_MF(open)
65 | RPC_ADD_MF(read)
66 | RPC_ADD_MF(write)
67 | RPC_ADD_MF(close)
68 | END_RPC_ADD_MF()
69 | RPC_DECLARE_MF(open, (const string& fname, const string& mode))
70 | RPC_DECLARE_MF(read, (vector* buffer, uint32_t length))
71 | RPC_DECLARE_MF(write, (const vector& buffer, uint32_t* length))
72 | RPC_DECLARE_MF(close, ())
73 | END_RPC_INTERFACE()
74 | ```
75 |
76 | This can be thought of as nark-rpc's IDL, as it declared, `FileObj` is in `SessionScope`.
77 |
78 | There is another scope: `GlobaleScope`, `samples/echo.h` is such an example:
79 | ```c++
80 | BEGIN_RPC_INTERFACE(Echo, GlobaleScope)
81 | RPC_ADD_MF(echo)
82 | END_RPC_ADD_MF()
83 | RPC_DECLARE_MF_D(echo, (const string& msg, string* echoFromServer))
84 | END_RPC_INTERFACE()
85 | ```
86 |
87 | ### Notes
88 | 1. Function overload is not allowed in IDL.
89 | 2. `RPC_DECLARE_MF` or `RPC_DECLARE_MF_D` should be used consitently in the same RPC interface.
90 |
91 | ### Client
92 |
93 | RPC client just call the (member) functions defined in IDL, the functions seem defined as normal functions.
94 | `RPC_DECLARE_MF` and `RPC_DECLARE_MF_D` are the same at client side.
95 |
96 | See [samples/file\_client/file\_client.cpp](samples/file_client/file_client.cpp#L22)
97 | See [samples/echo\_client/echo\_client.cpp](samples/echo_client/echo_client.cpp#L23)
98 |
99 | ### Server
100 |
101 | RPC server implement the (member) functions, these functions are called by the client through network.
102 |
103 | Writing a RPC server is as simple as writing a normal class:
104 |
105 | Functions declared by `RPC_DECLARE_MF` are **pure virtual**, so you must define an implementation class:
106 | See [samples/file\_server/file\_server.cpp](samples/file_server/file_server.cpp#L24)
107 |
108 | Functions declared by `RPC_DECLARE_MF_D` are not **pure virtual**, `_D` means `Direct`:
109 | See [samples/echo\_server/echo\_server.cpp](samples/echo_server/echo_server.cpp#L24)
110 |
111 | ## More
112 |
113 | To be written...
114 |
--------------------------------------------------------------------------------
/doc/rpc.doc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rockeet/nark-rpc/af7ce41ed03ddb8bef3e49572ccf8dd97f27c83e/doc/rpc.doc
--------------------------------------------------------------------------------
/doc/rpc_inspecting.doc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rockeet/nark-rpc/af7ce41ed03ddb8bef3e49572ccf8dd97f27c83e/doc/rpc_inspecting.doc
--------------------------------------------------------------------------------
/doc/rpc_message.txt:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rockeet/nark-rpc/af7ce41ed03ddb8bef3e49572ccf8dd97f27c83e/doc/rpc_message.txt
--------------------------------------------------------------------------------
/gen_env_conf.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | #set -x
3 |
4 | CXX=$1
5 | COMPILER=$2
6 | EnvConf=$3
7 | echo COMPILER=$COMPILER 1>&2
8 |
9 | #EnvConf=Make.env.conf-${COMPILER}
10 |
11 | rm -f $EnvConf
12 | mkdir -p `dirname $EnvConf`
13 |
14 | hasboost=0
15 | if [ -z "$BOOST_INC" ]; then
16 | boost_prefix=""
17 | else
18 | boost_prefix=`dirname $BOOST_INC`
19 | fi
20 | for dir in "$boost_prefix" /usr /usr/local /opt $HOME $HOME/opt
21 | do
22 | vf=${dir}/include/boost/version.hpp
23 | # echo dir=$dir >&2
24 | if test -s $vf; then
25 | if test -d ${dir}/lib32; then
26 | DIR_LIB32=${dir}/lib32
27 | else
28 | DIR_LIB32=${dir}/lib
29 | fi
30 | if test -d ${dir}/lib64; then
31 | DIR_LIB64=${dir}/lib64
32 | else
33 | DIR_LIB64=${dir}/lib
34 | fi
35 | WORD_BITS=`uname -m | sed 's/.*_\(64\|32\)/\1/'`
36 | DIR_LIB="DIR_LIB${WORD_BITS}"
37 | BOOST_VERSION=`sed -n '/define\s\+BOOST_VERSION/s/^.*BOOST_VERSION\s\+\([0-9]*\).*/\1/p' $vf`
38 | BOOST_LIB_VERSION=`sed -n '/define\s\+BOOST_LIB_VERSION/s/.*BOOST_LIB_VERSION[^"]*"\([0-9_.]*\)".*/\1/p' $vf`
39 | if test -z "$BOOST_SUFFIX"; then
40 | for lib in $DIR_LIB32 $DIR_LIB64; do
41 | for suf in \
42 | -${COMPILER}-mt-d-${BOOST_LIB_VERSION}.a \
43 | -${COMPILER}-mt-d-${BOOST_LIB_VERSION}.so \
44 | -${COMPILER}-mt-d-${BOOST_LIB_VERSION}.so \
45 | -mt.so \
46 | -mt.so.5 \
47 | -mt.so.6 \
48 | ".a"
49 | do
50 | if test -e $lib/libboost_thread$suf -a -z "$BOOST_SUFFIX"; then
51 | BOOST_SUFFIX=$suf
52 | fi
53 | done
54 | done
55 | if expr match "$BOOST_SUFFIX" '.*\.a$' > /dev/null; then
56 | BOOST_SUFFIX=${BOOST_SUFFIX%.a*}
57 | fi
58 | if expr match "$BOOST_SUFFIX" '.*\.so' > /dev/null; then
59 | BOOST_SUFFIX=${BOOST_SUFFIX%.so*}
60 | fi
61 | fi
62 | cat >> $EnvConf <<- EOF
63 | WORD_BITS := ${WORD_BITS}
64 | DIR_LIB32 := ${DIR_LIB32}
65 | DIR_LIB64 := ${DIR_LIB64}
66 | DIR_LIB := `eval 'echo ${'$DIR_LIB'}'`
67 | BOOST_LIB_VERSION := ${BOOST_LIB_VERSION}
68 | BOOST_INC := ${dir}/include
69 | BOOST_LIB := ${dir}/lib64
70 | BOOST_SUFFIX := ${BOOST_SUFFIX}
71 | EOF
72 | if test "${COMPILER%-*}" = gcc && expr "${COMPILER#*-} < 4.7"; then
73 | if test ${BOOST_VERSION} -lt 104900; then
74 | echo BOOST_VERSION=${BOOST_VERSION} will wrongly define BOOST_DISABLE_THREADS for ${COMPILER} >&2
75 | fi
76 | fi
77 | hasboost=1
78 | break
79 | fi
80 | done
81 |
82 | if [ $hasboost -eq 0 ]; then
83 | echo $'\33[31m\33[1mFATAL: can not find boost\33[0m' 1>&2
84 | exit 1
85 | fi
86 |
87 | cat > is_cygwin.cpp << "EOF"
88 | #include
89 | int main() {
90 | #ifdef __CYGWIN__
91 | printf("1");
92 | #else
93 | printf("0");
94 | #endif
95 | return 0;
96 | }
97 | EOF
98 | if $CXX is_cygwin.cpp -o is_cygwin.exe; then
99 | IS_CYGWIN=`./is_cygwin.exe`
100 | echo IS_CYGWIN=$IS_CYGWIN >> $EnvConf
101 | fi
102 | rm -f is_cygwin.*
103 |
104 | if [ "$IS_CYGWIN" = 0 ]; then
105 | cat > get_glibc_version.cpp << "EOF"
106 | #include
107 | int main() {
108 | printf("%d.%d\n", __GLIBC__, __GLIBC_MINOR__);
109 | return 0;
110 | }
111 | EOF
112 | if $CXX get_glibc_version.cpp -o get_glibc_version.exe; then
113 | GLIBC_VERSION=`./get_glibc_version.exe`
114 | echo GLIBC_VERSION_FULL=${GLIBC_VERSION} >> $EnvConf
115 | echo GLIBC_VERSION_MAJOR=`echo ${GLIBC_VERSION} | cut -d. -f1` >> $EnvConf
116 | echo GLIBC_VERSION_MINOR=`echo ${GLIBC_VERSION} | cut -d. -f2` >> $EnvConf
117 | else
118 | echo "can not detect glibc version" 1>&2
119 | fi
120 | rm -f get_glibc_version.*
121 | fi
122 |
123 | cat > has_inheriting_cons.cpp << "EOF"
124 | struct A {
125 | A(int) {}
126 | A(int,int){}
127 | };
128 | struct B : public A {
129 | using A::A;
130 | };
131 | int main() {
132 | B b1(111);
133 | B b2(2,2);
134 | return 0;
135 | }
136 | EOF
137 | rm -f src/nark/my_auto_config.hpp
138 | touch src/nark/my_auto_config.hpp
139 | if $CXX -std=c++11 has_inheriting_cons.cpp > /dev/null 2>&1; then
140 | echo '#define NARK_HAS_INHERITING_CONSTRUCTORS' >> src/nark/my_auto_config.hpp
141 | fi
142 | rm -f has_inheriting_cons.cpp
143 |
144 | if [ "$IS_CYGWIN" -eq 1 ]; then
145 | rm -f a.exe
146 | else
147 | rm -f a.out
148 | fi
149 |
150 |
--------------------------------------------------------------------------------
/samples/Makefile:
--------------------------------------------------------------------------------
1 | DBG_FLAGS ?= -g3 -D_DEBUG
2 | RLS_FLAGS ?= -O3 -DNDEBUG
3 |
4 | ifeq "$(origin LD)" "default"
5 | LD := ${CXX}
6 | endif
7 |
8 | COMPILER := $(shell ${CXX} --version | head -1 | awk '{split($$3, Ver, "."); printf("%s-%d.%d", $$1, Ver[1], Ver[2]);}')
9 | #$(error COMPILER=${COMPILER})
10 | UNAME_MachineSystem := $(shell uname -m -s | sed 's:[ /]:-:g')
11 | BUILD_ROOT := build/${COMPILER}-${UNAME_MachineSystem}
12 | ddir:=${BUILD_ROOT}/dbg
13 | rdir:=${BUILD_ROOT}/rls
14 |
15 | NARK_INC := -I../src \
16 | -I../../nark-hashmap/src \
17 | -I../../nark-serialization/src \
18 | -I../../nark-bone/src
19 | NARK_LIB := -L../lib \
20 | -L../../nark-serialization/lib \
21 | -L../../nark-bone/lib
22 |
23 | NARK_LIB_D := ${NARK_LIB} -lnark-rpc-d -lnark-serialization-d -lnark-bone-d
24 | NARK_LIB_R := ${NARK_LIB} -lnark-rpc-r -lnark-serialization-r -lnark-bone-r
25 |
26 | UNAME_System := $(shell uname | sed 's/^\([0-9a-zA-Z]*\).*/\1/')
27 | ifeq (CYGWIN, ${UNAME_System})
28 | FPIC =
29 | # lazy expansion
30 | CYGWIN_LDFLAGS = -Wl,--out-implib=$@ \
31 | -Wl,--export-all-symbols \
32 | -Wl,--enable-auto-import
33 | DLL_SUFFIX = .dll.a
34 | CYG_DLL_FILE = $(shell echo $@ | sed 's:\(.*\)/lib\([^/]*\)\.a$$:\1/cyg\2:')
35 | else
36 | FPIC = -fPIC
37 | DLL_SUFFIX = .so
38 | CYG_DLL_FILE = $@
39 | endif
40 | override CFLAGS += ${FPIC}
41 | override CXXFLAGS += ${FPIC}
42 | override LDFLAGS += ${FPIC}
43 |
44 | ifeq "$(shell expr substr ${COMPILER} 1 3)" "g++"
45 | override LDFLAGS += -rdynamic
46 | override CXXFLAGS += -time
47 | ifeq "$(shell echo ${COMPILER} | awk -F- '{if ($$2 >= 4.8) print 1;}')" "1"
48 | CXX_STD := -std=gnu++1y
49 | endif
50 | endif
51 |
52 | ifeq "${CXX_STD}" ""
53 | CXX_STD := -std=gnu++11
54 | endif
55 |
56 | # icc or icpc
57 | ifeq "$(shell expr substr ${COMPILER} 1 2)" "ic"
58 | override CXXFLAGS += -xHost -fasm-blocks
59 | CPU = -xHost
60 | else
61 | CPU = -march=native
62 | COMMON_C_FLAGS += -Wno-deprecated-declarations
63 | COMMON_C_FLAGS += -fstrict-aliasing
64 | COMMON_C_FLAGS += -Wstrict-aliasing=3
65 | endif
66 |
67 | COMMON_C_FLAGS = -Wformat=2 -Wcomment
68 | COMMON_C_FLAGS += -Wall -Wextra
69 | COMMON_C_FLAGS += -Wno-unused-parameter
70 | COMMON_C_FLAGS += -D_GNU_SOURCE
71 |
72 | #-v #-Wall -Wparentheses
73 | #COMMON_C_FLAGS += ${COMMON_C_FLAGS} -Wpacked -Wpadded -v
74 | #COMMON_C_FLAGS += ${COMMON_C_FLAGS} -Winvalid-pch
75 | #COMMON_C_FLAGS += ${COMMON_C_FLAGS} -fmem-report
76 |
77 | ifeq "$(shell expr substr ${COMPILER} 1 5)" "clang"
78 | COMMON_C_FLAGS += -fcolor-diagnostics
79 | endif
80 |
81 | #CXXFLAGS +=
82 | #CXXFLAGS += -fpermissive
83 | #CXXFLAGS += -fexceptions
84 | #CXXFLAGS += -fdump-translation-unit -fdump-class-hierarchy
85 |
86 | override CFLAGS += ${COMMON_C_FLAGS}
87 | override CXXFLAGS += ${COMMON_C_FLAGS}
88 |
89 | DEFS := -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
90 | override CFLAGS += ${DEFS}
91 | override CXXFLAGS += ${DEFS}
92 |
93 | INCS = ${NARK_INC}
94 |
95 | LIBBOOST ?= \
96 | -lboost_thread${BOOST_SUFFIX} \
97 | -lboost_date_time${BOOST_SUFFIX} \
98 | -lboost_system${BOOST_SUFFIX}
99 |
100 | LIBS += -L/usr/local/lib64 -L/usr/local/lib
101 | LIBS += -L/usr/lib64 -L/usr/lib
102 | LIBS += -lpthread
103 | LIBS += ${LIBBOOST}
104 |
105 | extf = -pie -fno-stack-protector
106 | #extf+=-fno-stack-protector-all
107 | override CFLAGS += ${extf}
108 | #override CFLAGS += -g3
109 | override CXXFLAGS += ${extf}
110 | #override CXXFLAGS += -g3
111 | #CXXFLAGS += -fnothrow-opt
112 |
113 | allsrc := $(shell find * -name '*.cpp' -not -name 'stdafx.cpp')
114 | alldep := $(addprefix ${rdir}/, $(addsuffix .dep, $(basename ${allsrc}))) \
115 | $(addprefix ${ddir}/, $(addsuffix .dep, $(basename ${allsrc})))
116 |
117 | allsrc_d_o := $(addprefix ${ddir}/, $(addsuffix .o, $(basename ${allsrc})))
118 | allsrc_r_o := $(addprefix ${rdir}/, $(addsuffix .o, $(basename ${allsrc})))
119 |
120 | #$(warning ${allsrc_d_o} ${allsrc_r_o})
121 |
122 | .PHONY : all
123 | all : dbg rls
124 |
125 | .PHONY : dbg rls
126 | dbg: $(addsuffix .exe, $(basename ${allsrc_d_o}))
127 | rls: $(addsuffix .exe, $(basename ${allsrc_r_o}))
128 |
129 | .PHONY : clean
130 | clean:
131 | -rm -rf ${BUILD_ROOT} ${PRECOMPILED_HEADER_GCH}
132 |
133 | .PHONY : depends
134 | depends : ${alldep}
135 |
136 | -include ${alldep}
137 |
138 | ${rdir}/%.exe: ${rdir}/%.o
139 | @echo file: $< "->" $@
140 | mkdir -p $(dir $@)
141 | ${LD} ${DBG_FLAGS} ${CXXFLAGS} $< ${NARK_LIB_R} ${LIBS} -o $@
142 |
143 | ${ddir}/%.exe: ${ddir}/%.o
144 | @echo file: $< "->" $@
145 | mkdir -p $(dir $@)
146 | ${LD} ${DBG_FLAGS} ${CXXFLAGS} $< ${NARK_LIB_D} ${LIBS} -o $@
147 |
148 | ${rdir}/%.o: %.cpp
149 | @echo file: $< "->" $@
150 | @echo NARK_INC=${NARK_INC}
151 | @echo BOOST_INC=${BOOST_INC} BOOST_SUFFIX=${BOOST_SUFFIX}
152 | mkdir -p $(dir $@)
153 | ${CXX} ${CXX_STD} ${CPU} -c ${RLS_FLAGS} ${CXXFLAGS} ${INCS} $< -o $@
154 |
155 | ${ddir}/%.o: %.cpp
156 | @echo file: $< "->" $@
157 | @echo NARK_INC=${NARK_INC}
158 | @echo BOOST_INC=${BOOST_INC} BOOST_SUFFIX=${BOOST_SUFFIX}
159 | mkdir -p $(dir $@)
160 | ${CXX} ${CXX_STD} ${CPU} -c ${DBG_FLAGS} ${CXXFLAGS} ${INCS} $< -o $@
161 |
162 | ${rdir}/%.o: %.cpp
163 | @echo file: $< "->" $@
164 | @echo NARK_INC=${NARK_INC}
165 | @echo BOOST_INC=${BOOST_INC} BOOST_SUFFIX=${BOOST_SUFFIX}
166 | mkdir -p $(dir $@)
167 | ${CXX} ${CXX_STD} ${CPU} -c ${RLS_FLAGS} ${CXXFLAGS} ${INCS} $< -o $@
168 |
169 | ${ddir}/%.o: %.cc
170 | @echo file: $< "->" $@
171 | @echo NARK_INC=${NARK_INC}
172 | @echo BOOST_INC=${BOOST_INC} BOOST_SUFFIX=${BOOST_SUFFIX}
173 | mkdir -p $(dir $@)
174 | ${CXX} ${CXX_STD} ${CPU} -c ${DBG_FLAGS} ${CXXFLAGS} ${INCS} $< -o $@
175 |
176 | ${rdir}/%.o: %.cc
177 | @echo file: $< "->" $@
178 | @echo NARK_INC=${NARK_INC}
179 | @echo BOOST_INC=${BOOST_INC} BOOST_SUFFIX=${BOOST_SUFFIX}
180 | mkdir -p $(dir $@)
181 | ${CXX} ${CXX_STD} ${CPU} -c ${RLS_FLAGS} ${CXXFLAGS} ${INCS} $< -o $@
182 |
183 | ${ddir}/%.o : %.c
184 | @echo file: $< "->" $@
185 | mkdir -p $(dir $@)
186 | ${CC} -c ${CPU} ${DBG_FLAGS} ${CFLAGS} ${INCS} $< -o $@
187 |
188 | ${rdir}/%.o : %.c
189 | @echo file: $< "->" $@
190 | mkdir -p $(dir $@)
191 | ${CC} -c ${CPU} ${RLS_FLAGS} ${CFLAGS} ${INCS} $< -o $@
192 |
193 | ${ddir}/%.s : %.cpp ${PRECOMPILED_HEADER_GCH}
194 | @echo file: $< "->" $@
195 | ${CXX} -S ${CPU} ${DBG_FLAGS} ${CXXFLAGS} ${INCS} $< -o $@
196 |
197 | ${rdir}/%.s : %.cpp ${PRECOMPILED_HEADER_GCH}
198 | @echo file: $< "->" $@
199 | ${CXX} -S ${CPU} ${RLS_FLAGS} ${CXXFLAGS} ${INCS} $< -o $@
200 |
201 | ${ddir}/%.s : %.c ${PRECOMPILED_HEADER_GCH}
202 | @echo file: $< "->" $@
203 | ${CC} -S ${CPU} ${DBG_FLAGS} ${CXXFLAGS} ${INCS} $< -o $@
204 |
205 | ${rdir}/%.s : %.c ${PRECOMPILED_HEADER_GCH}
206 | @echo file: $< "->" $@
207 | ${CC} -S ${CPU} ${RLS_FLAGS} ${CXXFLAGS} ${INCS} $< -o $@
208 |
209 | ${rdir}/%.dep : %.c
210 | @echo file: $< "->" $@
211 | @echo INCS = ${INCS}
212 | mkdir -p $(dir $@)
213 | ${CC} -M -MT $(basename $@).o ${INCS} $< > $@
214 |
215 | ${ddir}/%.dep : %.c
216 | @echo file: $< "->" $@
217 | @echo INCS = ${INCS}
218 | mkdir -p $(dir $@)
219 | ${CC} -M -MT $(basename $@).o ${INCS} $< > $@
220 |
221 | ${rdir}/%.dep : %.cpp
222 | @echo file: $< "->" $@
223 | @echo INCS = ${INCS}
224 | mkdir -p $(dir $@)
225 | ${CXX} -M -MT $(basename $@).o ${INCS} $< > $@
226 |
227 | ${ddir}/%.dep : %.cpp
228 | @echo file: $< "->" $@
229 | @echo INCS = ${INCS}
230 | mkdir -p $(dir $@)
231 | ${CXX} -M -MT $(basename $@).o ${INCS} $< > $@
232 |
233 |
--------------------------------------------------------------------------------
/samples/async_client/ReadMe.txt:
--------------------------------------------------------------------------------
1 | ========================================================================
2 | CONSOLE APPLICATION : test_rpc_client Project Overview
3 | ========================================================================
4 |
5 | AppWizard has created this test_rpc_client application for you.
6 |
7 | This file contains a summary of what you will find in each of the files that
8 | make up your test_rpc_client application.
9 |
10 |
11 | test_rpc_client.vcproj
12 | This is the main project file for VC++ projects generated using an Application Wizard.
13 | It contains information about the version of Visual C++ that generated the file, and
14 | information about the platforms, configurations, and project features selected with the
15 | Application Wizard.
16 |
17 | test_rpc_client.cpp
18 | This is the main application source file.
19 |
20 | /////////////////////////////////////////////////////////////////////////////
21 | Other standard files:
22 |
23 | StdAfx.h, StdAfx.cpp
24 | These files are used to build a precompiled header (PCH) file
25 | named test_rpc_client.pch and a precompiled types file named StdAfx.obj.
26 |
27 | /////////////////////////////////////////////////////////////////////////////
28 | Other notes:
29 |
30 | AppWizard uses "TODO:" comments to indicate parts of the source code you
31 | should add to or customize.
32 |
33 | /////////////////////////////////////////////////////////////////////////////
34 |
--------------------------------------------------------------------------------
/samples/async_client/async_client.cpp:
--------------------------------------------------------------------------------
1 | #include "stdafx.h"
2 | #include
3 | #include
4 | #include
5 |
6 | using namespace std;
7 | using namespace nark;
8 | using namespace nark::rpc;
9 |
10 | #include "../test.h"
11 |
12 | #ifdef _MSC_VER
13 | #pragma comment(lib, "Ws2_32.lib")
14 | #endif
15 |
16 | void printVec(const vint_vec& vec)
17 | {
18 | for (size_t i = 0; i != vec.size(); ++i)
19 | {
20 | cout << "vec[" << i << "]=" << vec[i] << "\n";
21 | }
22 | }
23 |
24 | class AsyncImpl : public AsyncInterface
25 | {
26 | public:
27 | AsyncImpl()
28 | {
29 | this->multiVec.set_async_callback(&AsyncImpl::on_multiVec);
30 | }
31 | private:
32 | void on_multiVec(const client_packet_base& packet, vint_vec& z, vint_vec& x, vint_vec& y)
33 | {
34 | printf("AsyncImpl::on_multiVec\n");
35 | printf("ret=%u, z=%zu, x=%zu, y=%zu\n", packet.retv, z.size(), x.size(), y.size());
36 | }
37 | };
38 | RPC_TYPEDEF_PTR(AsyncImpl);
39 |
40 | int main0(int argc, char* argv[])
41 | try {
42 | auto_ptr cs(ConnectSocket("127.0.0.1:8001"));
43 | rpc_client client(cs.get());
44 |
45 | AsyncImplPtr obj1 = client.create("obj1");
46 | SampleRPC_Interface2Ptr obj2 = client.create("obj2");
47 |
48 | int ret = client.retrieve(obj2, "obj2");
49 | (void)ret;
50 |
51 | rpc_ret_t val = obj1->get_val(100);
52 | cout << "obj1->get_val(100)=" << val << "\n";
53 |
54 | val = obj1->get_len("hello, world!");
55 | cout << "obj1->get_len(\"hello, world!\")=" << val << "\n";
56 |
57 | std::vector vec;
58 | vec.push_back((1));
59 | vec.push_back((2));
60 | vec.push_back((3));
61 | vec.push_back((4));
62 | vec.push_back((11));
63 | vec.push_back((22));
64 | vec.push_back((33));
65 | vec.push_back((44));
66 |
67 | val = obj1->squareVec(vec);
68 | printVec(vec);
69 | val = obj1->squareVec(vec);
70 | printVec(vec);
71 |
72 | std::vector vec2;
73 | for (size_t i = 0; i != vec.size(); ++i)
74 | {
75 | vec2.push_back(i + 1);
76 | }
77 | // obj1->multiVec.on_return = &AsyncInterface::on_multiVec;
78 | for (size_t i = 0; i < 5; ++i)
79 | {
80 | std::vector vec3;
81 | obj1->multiVec.async(vec3, vec, vec2);
82 | cout << "obj1->multiVec(vec3, vec, vec2) = " << val << "\n";
83 | printVec(vec3);
84 | }
85 | client.wait_pending_async();
86 | return 0;
87 | }
88 | catch (const std::exception& exp)
89 | {
90 | printf("exception: what=%s\n", exp.what());
91 | return 1;
92 | }
93 |
94 | int main(int argc, char* argv[])
95 | {
96 | #if defined(_WIN32) || defined(_WIN64)
97 | WSADATA information;
98 | WSAStartup(MAKEWORD(2, 2), &information);
99 | int ret = main0(argc, argv);
100 | WSACleanup();
101 | return ret;
102 | #else
103 | return main0(argc, argv);
104 | #endif
105 | }
106 |
107 |
--------------------------------------------------------------------------------
/samples/async_client/async_client.vcproj:
--------------------------------------------------------------------------------
1 |
2 |
11 |
12 |
15 |
16 |
17 |
18 |
19 |
26 |
29 |
32 |
35 |
38 |
41 |
55 |
58 |
61 |
64 |
74 |
77 |
80 |
83 |
86 |
89 |
92 |
95 |
96 |
104 |
107 |
110 |
113 |
116 |
119 |
129 |
132 |
135 |
138 |
150 |
153 |
156 |
159 |
162 |
165 |
168 |
171 |
172 |
173 |
174 |
175 |
176 |
181 |
184 |
187 |
191 |
192 |
195 |
199 |
200 |
201 |
204 |
205 |
206 |
211 |
214 |
215 |
218 |
219 |
220 |
225 |
226 |
229 |
230 |
231 |
232 |
233 |
234 |
--------------------------------------------------------------------------------
/samples/async_client/stdafx.cpp:
--------------------------------------------------------------------------------
1 | // stdafx.cpp : source file that includes just the standard includes
2 | // test_rpc_client.pch will be the pre-compiled header
3 | // stdafx.obj will contain the pre-compiled type information
4 |
5 | #include "stdafx.h"
6 |
7 | // TODO: reference any additional headers you need in STDAFX.H
8 | // and not in this file
9 |
--------------------------------------------------------------------------------
/samples/async_client/stdafx.h:
--------------------------------------------------------------------------------
1 | #ifndef __stdafx_h__
2 | #define __stdafx_h__
3 |
4 | #if defined(_MSC_VER) && (_MSC_VER >= 1020)
5 | # pragma once
6 | #endif
7 |
8 | // stdafx.h : include file for standard system include files,
9 | // or project specific include files that are used frequently, but
10 | // are changed infrequently
11 | //
12 |
13 |
14 | #ifndef _WIN32_WINNT // Allow use of features specific to Windows XP or later.
15 | #define _WIN32_WINNT 0x0501 // Change this to the appropriate value to target other versions of Windows.
16 | #endif
17 |
18 | #include
19 | //#include
20 |
21 |
22 |
23 | // TODO: reference additional headers your program requires here
24 |
25 | #endif // __stdafx_h__
26 |
27 |
--------------------------------------------------------------------------------
/samples/async_server/ReadMe.txt:
--------------------------------------------------------------------------------
1 | ========================================================================
2 | CONSOLE APPLICATION : test_rpc_server Project Overview
3 | ========================================================================
4 |
5 | AppWizard has created this test_rpc_server application for you.
6 |
7 | This file contains a summary of what you will find in each of the files that
8 | make up your test_rpc_server application.
9 |
10 |
11 | test_rpc_server.vcproj
12 | This is the main project file for VC++ projects generated using an Application Wizard.
13 | It contains information about the version of Visual C++ that generated the file, and
14 | information about the platforms, configurations, and project features selected with the
15 | Application Wizard.
16 |
17 | test_rpc_server.cpp
18 | This is the main application source file.
19 |
20 | /////////////////////////////////////////////////////////////////////////////
21 | Other standard files:
22 |
23 | StdAfx.h, StdAfx.cpp
24 | These files are used to build a precompiled header (PCH) file
25 | named test_rpc_server.pch and a precompiled types file named StdAfx.obj.
26 |
27 | /////////////////////////////////////////////////////////////////////////////
28 | Other notes:
29 |
30 | AppWizard uses "TODO:" comments to indicate parts of the source code you
31 | should add to or customize.
32 |
33 | /////////////////////////////////////////////////////////////////////////////
34 |
--------------------------------------------------------------------------------
/samples/async_server/async_server.cpp:
--------------------------------------------------------------------------------
1 | // test_rpc_server.cpp : Defines the entry point for the console application.
2 | //
3 |
4 | #include "stdafx.h"
5 |
6 | #ifdef _MSC_VER
7 | #pragma warning(disable: 4251) // needs to have dll-interface
8 | #pragma warning(disable: 4267) // conversion from 'size_t' to 'long', possible loss of data
9 | #pragma warning(disable: 4244) // conversion from '__w64 const unsigned int' to 'unsigned int', possible loss of data
10 | #pragma comment(lib, "Ws2_32.lib")
11 | #endif
12 |
13 | #include
14 | #include
15 | #include
16 |
17 | using namespace nark;
18 | using namespace nark::rpc;
19 |
20 | #include "../test.h"
21 |
22 | // use macro for convenient
23 | BEGIN_RPC_IMP_INTERFACE(SampleRPC_Imp1, AsyncInterface)
24 | rpc_ret_t get_val(rpc_in x)
25 | {
26 | std::cout << "AsyncInterface::get_val(rpc_in x=" << x.r << ")\n";
27 | return x.r;
28 | }
29 | rpc_ret_t get_len(const std::string& x)
30 | {
31 | std::cout << "AsyncInterface::get_len(const std::string& x=\"" << x << "\")\n";
32 | return x.size();
33 | }
34 | rpc_ret_t squareVec(vint_vec& x)
35 | {
36 | for (vint_vec::iterator i = x.begin(); i != x.end(); ++i)
37 | {
38 | *i *= *i;
39 | }
40 | return x.size();
41 | }
42 | rpc_ret_t multiVec(vint_vec& z, vint_vec& x, vint_vec& y)
43 | {
44 | z.clear();
45 | for (size_t i = 0; i != x.size(); ++i)
46 | {
47 | z.push_back(x[i] * y[i]);
48 | }
49 | return 0x12345678;
50 | }
51 | END_RPC_IMP_INTERFACE()
52 |
53 | // don't use macro for more control
54 | class SampleRPC_Imp2 : public SampleRPC_Interface2
55 | {
56 | rpc_ret_t get_val(rpc_in x)
57 | {
58 | std::cout << BOOST_CURRENT_FUNCTION << "x=" < server(&acceptor);
82 |
83 | // register rpc implementation class...
84 | RPC_SERVER_AUTO_CREATE(server, SampleRPC_Imp1);
85 | server.auto_create((SampleRPC_Imp2*)0, &SampleRPC_Imp2::create);
86 |
87 | server.start();
88 | }
89 | catch (const std::exception& exp)
90 | {
91 | printf("exception: what=%s\n", exp.what());
92 | }
93 | return 0;
94 | }
95 |
96 | int main(int argc, char* argv[])
97 | {
98 | #if defined(_WIN32) || defined(_WIN64)
99 | WSADATA information;
100 | WSAStartup(MAKEWORD(2, 2), &information);
101 | int ret = main0(argc, argv);
102 | WSACleanup();
103 | return ret;
104 | #else
105 | return main0(argc, argv);
106 | #endif
107 | }
108 |
109 |
--------------------------------------------------------------------------------
/samples/async_server/async_server.vcproj:
--------------------------------------------------------------------------------
1 |
2 |
11 |
12 |
15 |
16 |
17 |
18 |
19 |
26 |
29 |
32 |
35 |
38 |
41 |
54 |
57 |
60 |
63 |
73 |
76 |
79 |
82 |
85 |
88 |
91 |
94 |
95 |
103 |
106 |
109 |
112 |
115 |
118 |
128 |
131 |
134 |
137 |
149 |
152 |
155 |
158 |
161 |
164 |
167 |
170 |
171 |
172 |
173 |
174 |
175 |
180 |
183 |
186 |
190 |
191 |
194 |
198 |
199 |
200 |
203 |
204 |
205 |
210 |
213 |
214 |
217 |
218 |
219 |
224 |
225 |
228 |
229 |
230 |
231 |
232 |
233 |
--------------------------------------------------------------------------------
/samples/async_server/stdafx.cpp:
--------------------------------------------------------------------------------
1 | // stdafx.cpp : source file that includes just the standard includes
2 | // test_rpc_server.pch will be the pre-compiled header
3 | // stdafx.obj will contain the pre-compiled type information
4 |
5 | #include "stdafx.h"
6 |
7 | // TODO: reference any additional headers you need in STDAFX.H
8 | // and not in this file
9 |
--------------------------------------------------------------------------------
/samples/async_server/stdafx.h:
--------------------------------------------------------------------------------
1 | #ifndef __stdafx_h__
2 | #define __stdafx_h__
3 |
4 | #if defined(_MSC_VER) && (_MSC_VER >= 1020)
5 | # pragma once
6 | #endif
7 |
8 | // stdafx.h : include file for standard system include files,
9 | // or project specific include files that are used frequently, but
10 | // are changed infrequently
11 | //
12 |
13 |
14 | #ifndef _WIN32_WINNT // Allow use of features specific to Windows XP or later.
15 | #define _WIN32_WINNT 0x0501 // Change this to the appropriate value to target other versions of Windows.
16 | #endif
17 |
18 | #include
19 | //#include
20 |
21 |
22 |
23 | // TODO: reference additional headers your program requires here
24 |
25 | #endif // __stdafx_h__
26 |
27 |
--------------------------------------------------------------------------------
/samples/echo.h:
--------------------------------------------------------------------------------
1 |
2 | // sample usage...
3 | // echo.h
4 |
5 | BEGIN_RPC_INTERFACE(Echo, GlobaleScope)
6 | RPC_ADD_MF(echo)
7 | END_RPC_ADD_MF()
8 | RPC_DECLARE_MF_D(echo, (const std::string& msg, std::string* y))
9 | END_RPC_INTERFACE()
10 |
11 |
--------------------------------------------------------------------------------
/samples/echo_client/ReadMe.txt:
--------------------------------------------------------------------------------
1 | ========================================================================
2 | CONSOLE APPLICATION : test_rpc_client Project Overview
3 | ========================================================================
4 |
5 | AppWizard has created this test_rpc_client application for you.
6 |
7 | This file contains a summary of what you will find in each of the files that
8 | make up your test_rpc_client application.
9 |
10 |
11 | test_rpc_client.vcproj
12 | This is the main project file for VC++ projects generated using an Application Wizard.
13 | It contains information about the version of Visual C++ that generated the file, and
14 | information about the platforms, configurations, and project features selected with the
15 | Application Wizard.
16 |
17 | test_rpc_client.cpp
18 | This is the main application source file.
19 |
20 | /////////////////////////////////////////////////////////////////////////////
21 | Other standard files:
22 |
23 | StdAfx.h, StdAfx.cpp
24 | These files are used to build a precompiled header (PCH) file
25 | named test_rpc_client.pch and a precompiled types file named StdAfx.obj.
26 |
27 | /////////////////////////////////////////////////////////////////////////////
28 | Other notes:
29 |
30 | AppWizard uses "TODO:" comments to indicate parts of the source code you
31 | should add to or customize.
32 |
33 | /////////////////////////////////////////////////////////////////////////////
34 |
--------------------------------------------------------------------------------
/samples/echo_client/echo_client.cpp:
--------------------------------------------------------------------------------
1 | // echo_client.cpp : Defines the entry point for the console application.
2 | //
3 |
4 | #include "stdafx.h"
5 |
6 | #ifdef _MSC_VER
7 | #pragma comment(lib, "Ws2_32.lib")
8 | #endif
9 |
10 | #include
11 | #include
12 | #include
13 | #include
14 |
15 | using namespace std;
16 | using namespace nark;
17 | using namespace nark::rpc;
18 |
19 | #include "../echo.h"
20 |
21 | int main0(int argc, char* argv[])
22 | try {
23 | auto_ptr cs(ConnectSocket("127.0.0.1:8001"));
24 | rpc_client client(cs.get());
25 | EchoPtr ec = client.create("echo");
26 | while (!cin.eof())
27 | {
28 | string msg, y;
29 | cin >> msg;
30 | ec->echo(msg, &y);
31 | cout << "msg:" << msg << endl;
32 | cout << "y__:" << y << endl;
33 | }
34 | return 0;
35 | }
36 | catch (const std::exception& exp)
37 | {
38 | printf("exception: what=%s\n", exp.what());
39 | return 1;
40 | }
41 |
42 | int main(int argc, char* argv[])
43 | {
44 | #if defined(_WIN32) || defined(_WIN64)
45 | WSADATA information;
46 | WSAStartup(MAKEWORD(2, 2), &information);
47 | int ret = main0(argc, argv);
48 | WSACleanup();
49 | return ret;
50 | #else
51 | return main0(argc, argv);
52 | #endif
53 | }
54 |
55 |
--------------------------------------------------------------------------------
/samples/echo_client/echo_client.vcproj:
--------------------------------------------------------------------------------
1 |
2 |
11 |
12 |
15 |
16 |
17 |
18 |
19 |
26 |
29 |
32 |
35 |
38 |
41 |
55 |
58 |
61 |
64 |
74 |
77 |
80 |
83 |
86 |
89 |
92 |
95 |
96 |
104 |
107 |
110 |
113 |
116 |
119 |
129 |
132 |
135 |
138 |
150 |
153 |
156 |
159 |
162 |
165 |
168 |
171 |
172 |
173 |
174 |
175 |
176 |
181 |
184 |
185 |
188 |
191 |
195 |
196 |
199 |
203 |
204 |
205 |
206 |
211 |
214 |
215 |
218 |
219 |
220 |
225 |
226 |
229 |
230 |
231 |
232 |
233 |
234 |
--------------------------------------------------------------------------------
/samples/echo_client/echo_client.vcproj.leipeng-PC.leipeng.user:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
11 |
35 |
36 |
39 |
63 |
64 |
65 |
66 |
--------------------------------------------------------------------------------
/samples/echo_client/stdafx.cpp:
--------------------------------------------------------------------------------
1 | // stdafx.cpp : source file that includes just the standard includes
2 | // echo_client.pch will be the pre-compiled header
3 | // stdafx.obj will contain the pre-compiled type information
4 |
5 | #include "stdafx.h"
6 |
7 | // TODO: reference any additional headers you need in STDAFX.H
8 | // and not in this file
9 |
--------------------------------------------------------------------------------
/samples/echo_client/stdafx.h:
--------------------------------------------------------------------------------
1 | #ifndef __stdafx_h__
2 | #define __stdafx_h__
3 |
4 | #if defined(_MSC_VER) && (_MSC_VER >= 1020)
5 | # pragma once
6 | #endif
7 |
8 | // stdafx.h : include file for standard system include files,
9 | // or project specific include files that are used frequently, but
10 | // are changed infrequently
11 | //
12 |
13 |
14 | #ifndef _WIN32_WINNT // Allow use of features specific to Windows XP or later.
15 | #define _WIN32_WINNT 0x0501 // Change this to the appropriate value to target other versions of Windows.
16 | #endif
17 |
18 | #include
19 | //#include
20 |
21 |
22 |
23 | // TODO: reference additional headers your program requires here
24 |
25 | #endif // __stdafx_h__
26 |
27 |
--------------------------------------------------------------------------------
/samples/echo_server/echo_server.cpp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rockeet/nark-rpc/af7ce41ed03ddb8bef3e49572ccf8dd97f27c83e/samples/echo_server/echo_server.cpp
--------------------------------------------------------------------------------
/samples/echo_server/echo_server.vcproj:
--------------------------------------------------------------------------------
1 |
2 |
11 |
12 |
15 |
16 |
17 |
18 |
19 |
26 |
29 |
32 |
35 |
38 |
41 |
54 |
57 |
60 |
63 |
73 |
76 |
79 |
82 |
85 |
88 |
91 |
94 |
95 |
103 |
106 |
109 |
112 |
115 |
118 |
128 |
131 |
134 |
137 |
149 |
152 |
155 |
158 |
161 |
164 |
167 |
170 |
171 |
172 |
173 |
174 |
175 |
180 |
183 |
184 |
187 |
190 |
194 |
195 |
198 |
202 |
203 |
204 |
205 |
210 |
213 |
214 |
217 |
218 |
219 |
224 |
225 |
228 |
229 |
230 |
231 |
232 |
233 |
--------------------------------------------------------------------------------
/samples/echo_server/echo_server.vcproj.leipeng-PC.leipeng.user:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
11 |
35 |
36 |
39 |
63 |
64 |
65 |
66 |
--------------------------------------------------------------------------------
/samples/echo_server/stdafx.cpp:
--------------------------------------------------------------------------------
1 | // stdafx.cpp : source file that includes just the standard includes
2 | // test_rpc_server.pch will be the pre-compiled header
3 | // stdafx.obj will contain the pre-compiled type information
4 |
5 | #include "stdafx.h"
6 |
7 | // TODO: reference any additional headers you need in STDAFX.H
8 | // and not in this file
9 |
--------------------------------------------------------------------------------
/samples/echo_server/stdafx.h:
--------------------------------------------------------------------------------
1 | #ifndef __stdafx_h__
2 | #define __stdafx_h__
3 |
4 | #if defined(_MSC_VER) && (_MSC_VER >= 1020)
5 | # pragma once
6 | #endif
7 |
8 | // stdafx.h : include file for standard system include files,
9 | // or project specific include files that are used frequently, but
10 | // are changed infrequently
11 | //
12 |
13 |
14 | #ifndef _WIN32_WINNT // Allow use of features specific to Windows XP or later.
15 | #define _WIN32_WINNT 0x0501 // Change this to the appropriate value to target other versions of Windows.
16 | #endif
17 |
18 | #include
19 | //#include
20 |
21 |
22 |
23 | // TODO: reference additional headers your program requires here
24 |
25 | #endif // __stdafx_h__
26 |
27 |
--------------------------------------------------------------------------------
/samples/employee.h:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | using namespace std;
4 | using namespace nark;
5 | using namespace nark::rpc;
6 |
7 | struct Employee
8 | {
9 | typedef unsigned key_type; // for id, used by kmapdset
10 |
11 | std::string name, department;
12 | unsigned id;
13 |
14 | void print(ostream& os) const
15 | {
16 | os << "id=" << dec << id << "[" << hex << id << "]"
17 | << ", name=" << name << ", department=" << department << endl;
18 | }
19 | #ifdef _DEBUG
20 | // for set break point and step into
21 | friend
22 | void DataIO_loadObject(PortableDataInput& dio, Employee& x)
23 | {
24 | dio >> x.name;
25 | dio >> x.department;
26 | dio >> x.id;
27 | }
28 | friend
29 | void DataIO_loadObject(PortableDataInput& dio, Employee& x)
30 | {
31 | dio >> x.name;
32 | dio >> x.department;
33 | dio >> x.id;
34 | }
35 | template
36 | friend
37 | void DataIO_loadObject(DataIO& dio, Employee& x)
38 | {
39 | dio >> x.name;
40 | dio >> x.department;
41 | dio >> x.id;
42 | }
43 | template
44 | friend
45 | void DataIO_saveObject(DataIO& dio, const Employee& x)
46 | {
47 | dio << x.name;
48 | dio << x.department;
49 | dio << x.id;
50 | }
51 | friend
52 | void DataIO_loadObject(PortableDataInput& dio, Employee& x)
53 | {
54 | dio >> x.name;
55 | dio >> x.department;
56 | dio >> x.id;
57 | }
58 | friend
59 | void DataIO_saveObject(PortableDataOutput& dio, const Employee& x)
60 | {
61 | dio << x.name;
62 | dio << x.department;
63 | dio << x.id;
64 | }
65 | #else
66 | DATA_IO_LOAD_SAVE(Employee, &name&department&id)
67 | #endif
68 | };
69 |
70 | BEGIN_RPC_INTERFACE(DbEmployee, GlobaleScope)
71 | RPC_ADD_MF(fillData)
72 | RPC_ADD_MF(getByID)
73 | RPC_ADD_MF(getByName)
74 | RPC_ADD_MF(getByDepartment)
75 | END_RPC_ADD_MF()
76 | RPC_DECLARE_MF(fillData, ())
77 | RPC_DECLARE_MF(getByID, (unsigned id, Employee& em))
78 | RPC_DECLARE_MF(getByName, (const std::string& name, std::vector& ems)) // maybe multi employee has same
79 | RPC_DECLARE_MF(getByDepartment, (const std::string& name, std::vector& ems))
80 | END_RPC_INTERFACE()
81 |
--------------------------------------------------------------------------------
/samples/employee_client/ReadMe.txt:
--------------------------------------------------------------------------------
1 | ========================================================================
2 | CONSOLE APPLICATION : employee_client Project Overview
3 | ========================================================================
4 |
5 | AppWizard has created this employee_client application for you.
6 |
7 | This file contains a summary of what you will find in each of the files that
8 | make up your employee_client application.
9 |
10 |
11 | employee_client.vcproj
12 | This is the main project file for VC++ projects generated using an Application Wizard.
13 | It contains information about the version of Visual C++ that generated the file, and
14 | information about the platforms, configurations, and project features selected with the
15 | Application Wizard.
16 |
17 | employee_client.cpp
18 | This is the main application source file.
19 |
20 | /////////////////////////////////////////////////////////////////////////////
21 | Other standard files:
22 |
23 | StdAfx.h, StdAfx.cpp
24 | These files are used to build a precompiled header (PCH) file
25 | named employee_client.pch and a precompiled types file named StdAfx.obj.
26 |
27 | /////////////////////////////////////////////////////////////////////////////
28 | Other notes:
29 |
30 | AppWizard uses "TODO:" comments to indicate parts of the source code you
31 | should add to or customize.
32 |
33 | /////////////////////////////////////////////////////////////////////////////
34 |
--------------------------------------------------------------------------------
/samples/employee_client/employee_client.cpp:
--------------------------------------------------------------------------------
1 | // employee_client.cpp : Defines the entry point for the console application.
2 | //
3 |
4 | #include "stdafx.h"
5 | #ifdef _MSC_VER
6 | #pragma comment(lib, "Ws2_32.lib")
7 | #endif
8 |
9 | #include
10 | #include "../employee.h"
11 |
12 | int main0(int argc, char* argv[])
13 | try {
14 | auto_ptr cs(ConnectSocket("127.0.0.1:8001"));
15 | rpc_client client(cs.get());
16 |
17 | DbEmployeePtr dbem = client.create("DbEmployeeObject");
18 |
19 | Employee em;
20 | vector ems;
21 | rpc_ret_t ret;
22 | ret = dbem->fillData();
23 | if (0 == ret)
24 | {
25 | cout << "fillData success\n";
26 | }
27 | for (;;)
28 | {
29 | cout << "getByID:\n";
30 | ret = dbem->getByID(1, em);
31 | if (0 == ret)
32 | {
33 | em.print(cout);
34 | }
35 | cout << "getByName:\n";
36 | ret = dbem->getByName("leipeng", ems);
37 | if (0 == ret)
38 | {
39 | for (vector::const_iterator i = ems.begin(); i != ems.end(); ++i)
40 | i->print(cout);
41 | }
42 | cout << "getByDepartment:\n";
43 | ret = dbem->getByDepartment("tech", ems);
44 | if (0 == ret)
45 | {
46 | for (vector::const_iterator i = ems.begin(); i != ems.end(); ++i)
47 | i->print(cout);
48 | }
49 | else
50 | {
51 | cout << "failed, ret=" << ret << endl;
52 | }
53 | if (getchar() == EOF)
54 | break;
55 | }
56 | return 0;
57 | }
58 | catch (const std::exception& exp)
59 | {
60 | printf("exception: what=%s\n", exp.what());
61 | return 1;
62 | }
63 |
64 | int main(int argc, char* argv[])
65 | {
66 | #if defined(_WIN32) || defined(_WIN64)
67 | WSADATA information;
68 | WSAStartup(MAKEWORD(2, 2), &information);
69 | int ret = main0(argc, argv);
70 | WSACleanup();
71 | return ret;
72 | #else
73 | return main0(argc, argv);
74 | #endif
75 | }
76 |
77 |
--------------------------------------------------------------------------------
/samples/employee_client/employee_client.vcproj:
--------------------------------------------------------------------------------
1 |
2 |
11 |
12 |
15 |
16 |
17 |
18 |
19 |
26 |
29 |
32 |
35 |
38 |
41 |
53 |
56 |
59 |
62 |
70 |
73 |
76 |
79 |
82 |
85 |
88 |
91 |
92 |
100 |
103 |
106 |
109 |
112 |
115 |
126 |
129 |
132 |
135 |
145 |
148 |
151 |
154 |
157 |
160 |
163 |
166 |
167 |
168 |
169 |
170 |
171 |
176 |
179 |
180 |
183 |
186 |
190 |
191 |
194 |
198 |
199 |
200 |
201 |
206 |
209 |
210 |
213 |
214 |
215 |
220 |
221 |
224 |
225 |
226 |
227 |
228 |
229 |
--------------------------------------------------------------------------------
/samples/employee_client/employee_client.vcproj.leipeng-PC.leipeng.user:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
11 |
35 |
36 |
39 |
63 |
64 |
65 |
66 |
--------------------------------------------------------------------------------
/samples/employee_client/stdafx.cpp:
--------------------------------------------------------------------------------
1 | // stdafx.cpp : source file that includes just the standard includes
2 | // employee_client.pch will be the pre-compiled header
3 | // stdafx.obj will contain the pre-compiled type information
4 |
5 | #include "stdafx.h"
6 |
7 | // TODO: reference any additional headers you need in STDAFX.H
8 | // and not in this file
9 |
--------------------------------------------------------------------------------
/samples/employee_client/stdafx.h:
--------------------------------------------------------------------------------
1 | #ifndef __stdafx_h__
2 | #define __stdafx_h__
3 |
4 | #if defined(_MSC_VER) && (_MSC_VER >= 1020)
5 | # pragma once
6 | #endif
7 |
8 | #include
9 | #include
10 | #include
11 |
12 | using namespace std;
13 |
14 |
15 | #endif // __stdafx_h__
16 |
--------------------------------------------------------------------------------
/samples/employee_server/ReadMe.txt:
--------------------------------------------------------------------------------
1 | ========================================================================
2 | CONSOLE APPLICATION : employee_server Project Overview
3 | ========================================================================
4 |
5 | AppWizard has created this employee_server application for you.
6 |
7 | This file contains a summary of what you will find in each of the files that
8 | make up your employee_server application.
9 |
10 |
11 | employee_server.vcproj
12 | This is the main project file for VC++ projects generated using an Application Wizard.
13 | It contains information about the version of Visual C++ that generated the file, and
14 | information about the platforms, configurations, and project features selected with the
15 | Application Wizard.
16 |
17 | employee_server.cpp
18 | This is the main application source file.
19 |
20 | /////////////////////////////////////////////////////////////////////////////
21 | Other standard files:
22 |
23 | StdAfx.h, StdAfx.cpp
24 | These files are used to build a precompiled header (PCH) file
25 | named employee_server.pch and a precompiled types file named StdAfx.obj.
26 |
27 | /////////////////////////////////////////////////////////////////////////////
28 | Other notes:
29 |
30 | AppWizard uses "TODO:" comments to indicate parts of the source code you
31 | should add to or customize.
32 |
33 | /////////////////////////////////////////////////////////////////////////////
34 |
--------------------------------------------------------------------------------
/samples/employee_server/employee_server.cpp:
--------------------------------------------------------------------------------
1 | // employee_server.cpp : Defines the entry point for the console application.
2 | //
3 |
4 | #include "stdafx.h"
5 |
6 | #ifdef _MSC_VER
7 | #pragma comment(lib, "Ws2_32.lib")
8 | #endif
9 |
10 | #include
11 | #include
12 |
13 | #include "../employee.h"
14 |
15 | // not need auto create
16 | class DbEmployeeImp : public DbEmployee
17 | {
18 | private:
19 | #ifdef EMPLOYEE_USE_BDB
20 | DbEnv m_env;
21 | auto_ptr > m_byID;
22 | auto_ptr > m_byName, m_byDepartment;
23 | #else
24 | int m_env; // dummy
25 | std::map m_byID;
26 | std::map > m_byName, m_byDepartment;
27 | #endif
28 |
29 | public:
30 | DbEmployeeImp() : m_env(0)
31 | {
32 | #ifdef EMPLOYEE_USE_BDB
33 | u_int32_t envFlags = 0
34 | |DB_CREATE
35 | |DB_THREAD
36 | |DB_INIT_MPOOL
37 | ;
38 | // printf("open env\n");
39 | m_env.open("db", envFlags, 0);
40 | // printf("open env success\n");
41 | m_byID.reset(new dbmap(&m_env, "byid"));
42 | m_byName.reset(new kmapdset(&m_env, "byname"));
43 | m_byDepartment.reset(new kmapdset(&m_env, "bydepartment"));
44 | #endif
45 | }
46 |
47 | rpc_ret_t fillData()
48 | {
49 | Employee e;
50 | e.id = 1;
51 | e.name = "leipeng";
52 | e.department = "tech";
53 | #ifdef EMPLOYEE_USE_BDB
54 | m_byID->insert(e.id, e);
55 | m_byName->insert(e.name, e);
56 | m_byDepartment->insert(e.department, e);
57 | #else
58 | m_byID[e.id] = e;
59 | m_byName[e.name].push_back(e);
60 | m_byDepartment[e.department].push_back(e);
61 | #endif
62 | // BOOST_CURRENT_FUNCTION/BOOST_STATIC_CONSTANT
63 | // WM_ACTIVATETOPLEVEL
64 | // CreateFile
65 | // abcdefghijklmnopqrstuvwxyz
66 | return 0;
67 | }
68 |
69 | rpc_ret_t getByID(unsigned id, Employee& em)
70 | {
71 | #ifdef EMPLOYEE_USE_BDB
72 | dbmap::iterator iter = m_byID->find(id);
73 | if (iter.exist())
74 | {
75 | em = iter->second;
76 | return 0;
77 | }
78 | #else
79 | std::map::iterator iter = m_byID.find(id);
80 | if (m_byID.end() == iter) {
81 | em = iter->second;
82 | return 0;
83 | }
84 | #endif
85 | return 1; // not found
86 | }
87 |
88 | rpc_ret_t getByName(const string& name, std::vector& em)
89 | {
90 | #ifdef EMPLOYEE_USE_BDB
91 | kmapdset::iterator iter = m_byName->find(name);
92 | if (iter.exist())
93 | {
94 | em.swap(iter.get_mutable().second);
95 | return 0;
96 | }
97 | #else
98 | std::map >::iterator iter = m_byName.find(name);
99 | if (m_byName.end() != iter) {
100 | em = iter->second;
101 | return 0;
102 | }
103 | #endif
104 | return 1; // not found
105 | }
106 |
107 | rpc_ret_t getByDepartment(const string& department, std::vector& em)
108 | {
109 | #ifdef EMPLOYEE_USE_BDB
110 | kmapdset::iterator iter = m_byDepartment->find(department);
111 | if (iter.exist())
112 | {
113 | em.swap(iter.get_mutable().second);
114 | return 0;
115 | }
116 | #else
117 | std::map >::iterator iter = m_byDepartment.find(department);
118 | if (m_byDepartment.end() != iter) {
119 | em = iter->second;
120 | return 0;
121 | }
122 | #endif
123 | return 1; // not found
124 | }
125 | };
126 |
127 | int main0(int argc, char* argv[])
128 | {
129 | try {
130 | SocketAcceptor acceptor("0.0.0.0:8001");
131 | rpc_server server(&acceptor);
132 |
133 | server.add_servant(
134 | new DbEmployeeImp,
135 | "DbEmployeeObject",
136 | 0 // 0 will not auto create GlobaleScope Object
137 | );
138 | server.start();
139 | }
140 | catch (const std::exception& exp)
141 | {
142 | printf("exception: what=%s\n", exp.what());
143 | }
144 | return 0;
145 | }
146 |
147 | int main(int argc, char* argv[])
148 | {
149 | #if defined(_WIN32) || defined(_WIN64)
150 | WSADATA information;
151 | WSAStartup(MAKEWORD(2, 2), &information);
152 | int ret = main0(argc, argv);
153 | WSACleanup();
154 | return ret;
155 | #else
156 | return main0(argc, argv);
157 | #endif
158 | }
159 |
160 |
--------------------------------------------------------------------------------
/samples/employee_server/employee_server.vcproj:
--------------------------------------------------------------------------------
1 |
2 |
11 |
12 |
15 |
16 |
17 |
18 |
19 |
26 |
29 |
32 |
35 |
38 |
41 |
53 |
56 |
59 |
62 |
70 |
73 |
76 |
79 |
82 |
85 |
88 |
91 |
92 |
100 |
103 |
106 |
109 |
112 |
115 |
126 |
129 |
132 |
135 |
145 |
148 |
151 |
154 |
157 |
160 |
163 |
166 |
167 |
168 |
169 |
170 |
171 |
176 |
179 |
180 |
183 |
186 |
190 |
191 |
194 |
198 |
199 |
200 |
201 |
206 |
209 |
210 |
213 |
214 |
215 |
220 |
221 |
224 |
225 |
226 |
227 |
228 |
229 |
--------------------------------------------------------------------------------
/samples/employee_server/employee_server.vcproj.leipeng-PC.leipeng.user:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
11 |
35 |
36 |
39 |
63 |
64 |
65 |
66 |
--------------------------------------------------------------------------------
/samples/employee_server/stdafx.cpp:
--------------------------------------------------------------------------------
1 | // stdafx.cpp : source file that includes just the standard includes
2 | // employee_server.pch will be the pre-compiled header
3 | // stdafx.obj will contain the pre-compiled type information
4 |
5 | #include "stdafx.h"
6 |
7 | // TODO: reference any additional headers you need in STDAFX.H
8 | // and not in this file
9 |
--------------------------------------------------------------------------------
/samples/employee_server/stdafx.h:
--------------------------------------------------------------------------------
1 | #ifndef __stdafx_h__
2 | #define __stdafx_h__
3 |
4 | #if defined(_MSC_VER) && (_MSC_VER >= 1020)
5 | # pragma once
6 | #endif
7 |
8 | #include
9 | #include
10 |
11 | #ifdef EMPLOYEE_USE_BDB
12 | #include
13 | #include
14 | #include
15 | #else
16 | #include