├── .gitignore ├── LICENSE ├── call_push_test ├── CMakeLists.txt ├── build.sh └── call_push_test.cpp ├── clientdemo ├── .classpath ├── .project ├── AndroidManifest.xml ├── ic_launcher-web.png ├── jni │ ├── Android.mk │ ├── Application.mk │ ├── android │ │ ├── androidclient.cpp │ │ ├── androidclient.h │ │ ├── client_jni.cpp │ │ └── client_jni.h │ ├── client_sdk │ │ ├── cli_test.cpp │ │ ├── client.cpp │ │ ├── client.h │ │ ├── client_conn.cpp │ │ ├── client_conn.h │ │ ├── client_def.h │ │ ├── client_log.h │ │ ├── err_no.h │ │ ├── eventloop.cpp │ │ ├── eventloop.h │ │ ├── logic_common.cpp │ │ ├── logic_common.h │ │ ├── msg_head.h │ │ ├── opbase.cpp │ │ ├── opbase.h │ │ ├── ops.cpp │ │ ├── ops.h │ │ ├── service_type.h │ │ └── srv_test.cpp │ ├── common │ │ ├── ef_aes.cpp │ │ ├── ef_aes.h │ │ ├── ef_auto_ptr.h │ │ ├── ef_base64.cpp │ │ ├── ef_base64.h │ │ ├── ef_btype.h │ │ ├── ef_hex.cpp │ │ ├── ef_hex.h │ │ ├── ef_loop_buf.cpp │ │ ├── ef_loop_buf.h │ │ ├── ef_md5.cpp │ │ ├── ef_md5.h │ │ ├── ef_no_copy.h │ │ ├── ef_singleton.h │ │ ├── ef_sock.cpp │ │ ├── ef_sock.h │ │ ├── ef_thread.cpp │ │ ├── ef_thread.h │ │ ├── ef_utility.cpp │ │ ├── ef_utility.h │ │ ├── err_no.h │ │ ├── rijndael-alg-fst.cpp │ │ ├── rijndael-alg-fst.h │ │ ├── rijndael-api-fst.cpp │ │ └── rijndael-api-fst.h │ ├── config.h │ ├── google │ │ └── protobuf │ │ │ ├── extension_set.cc │ │ │ ├── extension_set.h │ │ │ ├── generated_enum_reflection.h │ │ │ ├── generated_message_util.cc │ │ │ ├── generated_message_util.h │ │ │ ├── io │ │ │ ├── coded_stream.cc │ │ │ ├── coded_stream.h │ │ │ ├── coded_stream_inl.h │ │ │ ├── zero_copy_stream.cc │ │ │ ├── zero_copy_stream.h │ │ │ ├── zero_copy_stream_impl.h │ │ │ ├── zero_copy_stream_impl_lite.cc │ │ │ └── zero_copy_stream_impl_lite.h │ │ │ ├── message_lite.cc │ │ │ ├── message_lite.h │ │ │ ├── package_info.h │ │ │ ├── repeated_field.cc │ │ │ ├── repeated_field.h │ │ │ ├── stubs │ │ │ ├── atomicops.h │ │ │ ├── atomicops_internals_arm_gcc.h │ │ │ ├── atomicops_internals_arm_qnx.h │ │ │ ├── atomicops_internals_atomicword_compat.h │ │ │ ├── atomicops_internals_macosx.h │ │ │ ├── atomicops_internals_mips_gcc.h │ │ │ ├── atomicops_internals_pnacl.h │ │ │ ├── atomicops_internals_x86_gcc.cc │ │ │ ├── atomicops_internals_x86_gcc.h │ │ │ ├── common.cc │ │ │ ├── common.h │ │ │ ├── hash.h │ │ │ ├── map-util.h │ │ │ ├── once.cc │ │ │ ├── once.h │ │ │ ├── platform_macros.h │ │ │ ├── stl_util.h │ │ │ ├── template_util.h │ │ │ └── type_traits.h │ │ │ ├── wire_format_lite.cc │ │ │ ├── wire_format_lite.h │ │ │ └── wire_format_lite_inl.h │ ├── json │ │ ├── autolink.h │ │ ├── config.h │ │ ├── features.h │ │ ├── forwards.h │ │ ├── json.h │ │ ├── json_batchallocator.h │ │ ├── json_internalarray.inl │ │ ├── json_internalmap.inl │ │ ├── json_reader.cpp │ │ ├── json_value.cpp │ │ ├── json_valueiterator.inl │ │ ├── json_writer.cpp │ │ ├── reader.h │ │ ├── sconscript │ │ ├── value.h │ │ └── writer.h │ └── proto │ │ ├── connect_server.pb.cc │ │ ├── connect_server.pb.h │ │ ├── connect_server.proto │ │ ├── message.pb.cc │ │ ├── message.pb.h │ │ ├── message.proto │ │ ├── pair.pb.cc │ │ ├── pair.pb.h │ │ ├── pair.proto │ │ ├── peer_server.pb.cc │ │ ├── peer_server.pb.h │ │ ├── peer_server.proto │ │ ├── session.pb.cc │ │ ├── session.pb.h │ │ └── session.proto ├── proguard-project.txt ├── project.properties ├── res │ ├── drawable-hdpi │ │ └── ic_launcher.png │ ├── drawable-mdpi │ │ └── ic_launcher.png │ ├── drawable-xhdpi │ │ └── ic_launcher.png │ ├── drawable-xxhdpi │ │ └── ic_launcher.png │ ├── layout │ │ └── activity_main.xml │ ├── menu │ │ └── main.xml │ ├── values-w820dp │ │ └── dimens.xml │ └── values │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml └── src │ └── com │ └── gim │ ├── MainActivity.java │ ├── client.java │ └── listener.java ├── common ├── CMakeLists.txt ├── build.sh ├── include │ ├── cache_group.h │ ├── dynamic_tokenchk.h │ ├── ef_crypt.h │ ├── log_init.h │ ├── msg_db.h │ ├── redis_msg_q.h │ ├── server_status.h │ ├── sess_cache.h │ ├── token_checker.h │ ├── tree.h │ ├── zk_client.h │ ├── zk_config.h │ ├── zk_server_node.h │ ├── zk_server_ob.h │ └── zk_watcher.h ├── src │ ├── cache_group.cpp │ ├── def_cache_group.cpp │ ├── def_cache_group.h │ ├── def_sess_cache.cpp │ ├── def_sess_cache.h │ ├── def_sess_cache_group.cpp │ ├── def_sess_cache_group.h │ ├── dynamic_tokenchk.cpp │ ├── ef_crypt.cpp │ ├── gtype.h │ ├── log_init.cpp │ ├── message.pb.cpp │ ├── msg_db.cpp │ ├── pair.pb.cpp │ ├── redis_cg.cpp │ ├── redis_cg.h │ ├── redis_client.cpp │ ├── redis_client.h │ ├── redis_msg_db.h │ ├── redis_msg_q.cpp │ ├── server_status.cpp │ ├── sess_cache.cpp │ ├── session.pb.cpp │ ├── token_checker.cpp │ ├── zk_client.cpp │ ├── zk_config.cpp │ ├── zk_server_node.cpp │ ├── zk_server_ob.cpp │ └── zk_watcher.cpp └── test │ ├── logtest │ ├── CMakeLists.txt │ ├── build.sh │ ├── elog_test.cpp │ └── log.conf │ ├── msgboxtest │ ├── CMakeLists.txt │ ├── build.sh │ ├── msgboxtest.cpp │ └── test.conf │ ├── redisclitest │ ├── CMakeLists.txt │ ├── build.sh │ └── redis_cli_test.cpp │ ├── sess_cache_test │ ├── CMakeLists.txt │ ├── build.sh │ ├── log.conf │ └── sess_cache_test.cpp │ ├── sesstest │ ├── CMakeLists.txt │ ├── build.sh │ ├── sess.conf │ └── sess_test.cpp │ ├── svlsttest │ ├── CMakeLists.txt │ ├── build.sh │ ├── svlist.conf │ └── svlst_test.cpp │ └── zk_client_test │ ├── CMakeLists.txt │ ├── build.sh │ ├── log.conf │ └── test.cpp ├── connect_server ├── CMakeLists.txt ├── build.sh ├── etc │ ├── elog.conf │ └── settings.conf ├── src │ ├── client_config.h │ ├── client_conn.cpp │ ├── client_conn.h │ ├── common.cpp │ ├── common.h │ ├── connect_server.cpp │ ├── connect_server.h │ ├── main.cpp │ ├── proto │ │ ├── common_logic_service.proto │ │ ├── connect_server.pb.cpp │ │ ├── connect_server.pb.h │ │ ├── connect_server.proto │ │ ├── err_no.h │ │ ├── message.proto │ │ ├── msg_head.h │ │ ├── pair.proto │ │ ├── peer_server.proto │ │ ├── position_server.proto │ │ └── session.proto │ ├── server_config.h │ ├── type_map.cpp │ └── type_map.h ├── test_client │ ├── CMakeLists.txt │ ├── build.sh │ └── src │ │ ├── proto │ │ ├── common_logic_service.proto │ │ ├── connect_server.pb.cpp │ │ ├── connect_server.pb.h │ │ ├── connect_server.proto │ │ ├── err_no.h │ │ ├── message.pb.cpp │ │ ├── message.pb.h │ │ ├── message.proto │ │ ├── msg_head.h │ │ ├── pair.pb.cpp │ │ ├── pair.pb.h │ │ ├── pair.proto │ │ ├── peer_server.pb.cpp │ │ ├── peer_server.pb.h │ │ ├── peer_server.proto │ │ ├── position_server.proto │ │ └── session.proto │ │ └── test.cpp └── test_server │ ├── CMakeLists.txt │ ├── build.sh │ └── src │ ├── proto │ ├── common_logic_service.proto │ ├── connect_server.pb.cpp │ ├── connect_server.pb.h │ ├── connect_server.proto │ ├── err_no.h │ ├── message.proto │ ├── msg_head.h │ ├── pair.pb.cpp │ ├── pair.pb.h │ ├── pair.proto │ ├── peer_server.proto │ └── session.proto │ └── test.cpp ├── dispatch_server ├── CMakeLists.txt ├── build.sh ├── etc │ ├── ds.conf │ ├── elog.conf │ └── settings.conf ├── src │ ├── client_conn.cpp │ ├── client_conn.h │ ├── common.cpp │ ├── common.h │ ├── common_logic_service.proto │ ├── config.h │ ├── connect_server.proto │ ├── consv_config.h │ ├── dispatch_server.cpp │ ├── dispatch_server.h │ ├── err_no.h │ ├── main.cpp │ ├── message.proto │ ├── msg_head.h │ ├── pair.proto │ ├── proto │ │ ├── common_logic_service.proto │ │ ├── connect_server.pb.cpp │ │ ├── connect_server.pb.h │ │ ├── connect_server.proto │ │ ├── err_no.h │ │ ├── message.proto │ │ ├── msg_head.h │ │ ├── pair.pb.cpp │ │ ├── pair.pb.h │ │ ├── pair.proto │ │ └── session.proto │ └── session.proto └── test │ ├── CMakeLists.txt │ ├── build.sh │ └── src │ ├── common_logic_service.proto │ ├── connect_server.proto │ ├── err_no.h │ ├── main.cpp │ ├── message.proto │ ├── msg_head.h │ ├── pair.proto │ ├── proto │ ├── common_logic_service.proto │ ├── connect_server.pb.cpp │ ├── connect_server.pb.h │ ├── connect_server.proto │ ├── err_no.h │ ├── message.proto │ ├── msg_head.h │ ├── pair.pb.cpp │ ├── pair.pb.h │ ├── pair.proto │ └── session.proto │ └── session.proto ├── doc ├── GIM编码规范.doc └── 架构设计.doc ├── efnfw ├── CMakeLists.txt ├── base │ ├── ef_aes.cpp │ ├── ef_aes.h │ ├── ef_atomic.cpp │ ├── ef_atomic.h │ ├── ef_auto_ptr.h │ ├── ef_base64.cpp │ ├── ef_base64.h │ ├── ef_btype.h │ ├── ef_deamonize.cpp │ ├── ef_deamonize.h │ ├── ef_hex.cpp │ ├── ef_hex.h │ ├── ef_loader.h │ ├── ef_log.cpp │ ├── ef_log.h │ ├── ef_loop_buf.cpp │ ├── ef_loop_buf.h │ ├── ef_md5.cpp │ ├── ef_md5.h │ ├── ef_no_copy.h │ ├── ef_singleton.h │ ├── ef_statistic.cpp │ ├── ef_statistic.h │ ├── ef_thread.cpp │ ├── ef_thread.h │ ├── ef_thread_pool.cpp │ ├── ef_thread_pool.h │ ├── ef_tsd_ptr.h │ ├── ef_utility.cpp │ ├── ef_utility.h │ ├── rijndael-alg-fst.cpp │ ├── rijndael-alg-fst.h │ ├── rijndael-api-fst.cpp │ └── rijndael-api-fst.h ├── build.sh └── net │ ├── ef_acceptor.cpp │ ├── ef_acceptor.h │ ├── ef_client.cpp │ ├── ef_client.h │ ├── ef_common.h │ ├── ef_connection.cpp │ ├── ef_connection.h │ ├── ef_connector.h │ ├── ef_device.cpp │ ├── ef_device.h │ ├── ef_event_loop.cpp │ ├── ef_event_loop.h │ ├── ef_net_log.cpp │ ├── ef_net_log.h │ ├── ef_net_settings.cpp │ ├── ef_net_settings.h │ ├── ef_operator.cpp │ ├── ef_operator.h │ ├── ef_server.cpp │ ├── ef_server.h │ ├── ef_sock.cpp │ ├── ef_sock.h │ ├── ef_timer.cpp │ └── ef_timer.h ├── img └── jiagou.png ├── logic_server ├── CMakeLists.txt ├── build.sh ├── dispatcher.cpp ├── dispatcher.h ├── logic_common.cpp ├── logic_common.h ├── logic_server.cpp ├── logic_server.h ├── rpc_client.cpp ├── rpc_client.h ├── server_conn.cpp └── server_conn.h ├── logic_test ├── CMakeLists.txt ├── build.sh ├── etc │ ├── elog.conf │ └── settings.conf └── src │ ├── main.cpp │ ├── test_conn.cpp │ └── test_conn.h ├── proto ├── connect_server.proto ├── err_no.h ├── message.proto ├── msg_head.h ├── pair.proto ├── peer_server.proto └── session.proto ├── push_server ├── CMakeLists.txt ├── build.sh ├── etc │ └── elog.conf ├── run.sh └── src │ ├── main.cpp │ ├── push_client_conn.cpp │ ├── push_client_conn.h │ ├── push_common.cpp │ ├── push_common.h │ ├── push_def.h │ ├── push_msg_db.cpp │ ├── push_msg_db.h │ ├── push_server_conn.cpp │ └── push_server_conn.h └── thirdparty ├── jsoncpp-src-0.5.0.tar.gz └── scons-2.1.0.tar.gz /.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 | -------------------------------------------------------------------------------- /call_push_test/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project(ConnectServer) 2 | set(CMAKE_VERBOSE_MAKEFILE ON) 3 | add_definitions("-Wall -pg -g") 4 | include_directories(${PROJECT_SOURCE_DIR}/../efnfw) 5 | link_directories(/usr/local/lib ../efnfw/lib) 6 | link_libraries(libefnfw.a jsoncpp pthread) 7 | file(GLOB_RECURSE SRC_LIST call_push_test.cpp) 8 | add_executable(bin/CallPushTest ${SRC_LIST}) 9 | -------------------------------------------------------------------------------- /call_push_test/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | cd ../efnfw; 4 | sh build.sh 5 | cd - 6 | 7 | cd ../common; 8 | sh build.sh 9 | cd - 10 | 11 | mkdir -p bin 12 | 13 | rm -fr CMakeCache.txt 14 | rm -fr CMakeFiles 15 | 16 | 17 | cmake . 18 | make clean;make 19 | 20 | rm -fr CMakeCache.txt 21 | rm -fr CMakeFiles 22 | -------------------------------------------------------------------------------- /clientdemo/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /clientdemo/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | clientdemo 4 | 5 | 6 | 7 | 8 | 9 | com.android.ide.eclipse.adt.ResourceManagerBuilder 10 | 11 | 12 | 13 | 14 | com.android.ide.eclipse.adt.PreCompilerBuilder 15 | 16 | 17 | 18 | 19 | org.eclipse.jdt.core.javabuilder 20 | 21 | 22 | 23 | 24 | com.android.ide.eclipse.adt.ApkBuilder 25 | 26 | 27 | 28 | 29 | org.eclipse.ui.externaltools.ExternalToolBuilder 30 | full,incremental, 31 | 32 | 33 | LaunchConfigHandle 34 | <project>/.externalToolBuilders/BBBBBBBBBBB.launch 35 | 36 | 37 | 38 | 39 | 40 | com.android.ide.eclipse.adt.AndroidNature 41 | org.eclipse.jdt.core.javanature 42 | 43 | 44 | -------------------------------------------------------------------------------- /clientdemo/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 10 | 11 | 16 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /clientdemo/ic_launcher-web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmxiaocong/gim/f9bac9be3d2afdc9bea69d201d1ce3c10a56639e/clientdemo/ic_launcher-web.png -------------------------------------------------------------------------------- /clientdemo/jni/Application.mk: -------------------------------------------------------------------------------- 1 | APP_STL := stlport_static 2 | #APP_STL := gnustl_static 3 | 4 | #APP_STL := stl 5 | 6 | APP_PLATFORM := android-7 7 | #LOCAL_C_INCLUDES := ${ANDROID_NDK_ROOT}/sources/cxx-stl/stlport/stlport 8 | 9 | #TARGET_CPU_API := armeabi armeabi-v7a x86 10 | #APP_ABI := armeabi armeabi-v7a x86 11 | 12 | TARGET_CPU_API := armeabi 13 | APP_ABI := 14 | 15 | APP_OPTIM := debug 16 | #APP_OPTIM := release 17 | 18 | #TARGET_CPU_API := armeabi arm64-v8a x86 x86_64 mips64 19 | #APP_ABI := armeabi-v7a 20 | -------------------------------------------------------------------------------- /clientdemo/jni/android/client_jni.cpp: -------------------------------------------------------------------------------- 1 | #include "client_jni.h" 2 | #include "androidclient.h" 3 | 4 | #define C_STR(x) (gim::JstringToString(env, x).c_str()) 5 | 6 | #ifdef __cplusplus 7 | extern "C" { 8 | #endif 9 | static gim::AndroidClient s_cli; 10 | JNIEXPORT jint JNICALL Java_com_gim_client_init(JNIEnv *env, jobject jcli, jobject jlstn) 11 | { 12 | if(s_cli.initJniEnv(env, jlstn) < 0) 13 | return -1; 14 | 15 | return s_cli.init(); 16 | } 17 | JNIEXPORT jint JNICALL Java_com_gim_client_stop(JNIEnv *, jobject) 18 | { 19 | return s_cli.stop(); 20 | } 21 | JNIEXPORT jint JNICALL Java_com_gim_client_login(JNIEnv* env, jobject job, jstring jsrvip, jint srvport, 22 | jstring jcliver, jint enc,jstring jcid, jstring jpwd) 23 | { 24 | return s_cli.login(C_STR(jsrvip), srvport, C_STR(jcid), C_STR(jpwd), enc, C_STR(jcliver)); 25 | } 26 | JNIEXPORT jint JNICALL Java_com_gim_client_disconnect(JNIEnv *env, jobject job, jstring jcid) 27 | { 28 | return s_cli.disconnect(C_STR(jcid)); 29 | } 30 | JNIEXPORT jint JNICALL Java_com_gim_client_sendPeerMessage(JNIEnv *env, jobject, jstring jcid, jstring sn, jstring peercid, jstring data) 31 | { 32 | return s_cli.sendPeerMessage(C_STR(jcid), C_STR(sn), C_STR(peercid), C_STR(data)); 33 | } 34 | 35 | #ifdef __cplusplus 36 | } 37 | #endif 38 | -------------------------------------------------------------------------------- /clientdemo/jni/android/client_jni.h: -------------------------------------------------------------------------------- 1 | #include 2 | #ifndef _CLIENT_JNI_H_ 3 | #define _CLIENT_JNI_H_ 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | JNIEXPORT jint JNICALL Java_com_gim_client_init 9 | (JNIEnv *, jobject, jobject); 10 | 11 | JNIEXPORT jint JNICALL Java_com_gim_client_stop 12 | (JNIEnv *, jobject); 13 | 14 | JNIEXPORT jint JNICALL Java_com_gim_client_login 15 | (JNIEnv* env, jobject job, jstring jsrvip, jint srvport, jstring jcliver, jint enc, jstring jcid, jstring jpwd); 16 | 17 | JNIEXPORT jint JNICALL Java_com_gim_client_disconnect 18 | (JNIEnv *, jobject, jstring); 19 | 20 | JNIEXPORT jint JNICALL Java_com_gim_client_sendPeerMessage 21 | (JNIEnv *env, jobject, jstring jcid, jstring sn, jstring peercid, jstring data); 22 | 23 | #ifdef __cplusplus 24 | } 25 | #endif 26 | #endif 27 | -------------------------------------------------------------------------------- /clientdemo/jni/client_sdk/client.cpp: -------------------------------------------------------------------------------- 1 | #include "client.h" 2 | #include "ops.h" 3 | #include "common/ef_utility.h" 4 | namespace gim 5 | { 6 | std::string Client::getSN() 7 | { 8 | return itostr(m_sn++); 9 | } 10 | int32 Client::init() 11 | { 12 | m_sn = gettime_ms(); 13 | m_evlp.setMsgCb(eventLoopMsgRoutine, (void*)this); 14 | m_evlp.startLoop(); 15 | return 0; 16 | } 17 | 18 | int32 Client::login(const std::string& srvip, int32 srvport, const std::string& cid, const std::string& pwd, int32 enc, const std::string& version) 19 | { 20 | LoginOp* op = new LoginOp(cid); 21 | op->init(srvip, srvport, version, enc, pwd); 22 | return m_evlp.asynAddOp((Op*)op); 23 | } 24 | int32 Client::stop() 25 | { 26 | m_evlp.asynStop(); 27 | return 0; 28 | } 29 | int32 Client::disconnect(const std::string& cid) 30 | { 31 | DisconnectOp* op = new DisconnectOp(cid); 32 | return m_evlp.asynAddOp((Op*)op); 33 | } 34 | int32 Client::sendPeerMessage(const std::string& cid, const std::string& sn, const std::string& peercid, const std::string& data) 35 | { 36 | SendPeerMessageOp* op = new SendPeerMessageOp(sn, cid); 37 | op->init(peercid, data); 38 | return m_evlp.asynAddOp((Op*)op); 39 | } 40 | int Client::eventLoopMsgRoutine(void* cli, const std::string& msg) 41 | { 42 | return cli ? ((Client*)cli)->handleMessage(msg) : -1; 43 | } 44 | int Client::handleMessage(const std::string& msg) 45 | { 46 | return 0; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /clientdemo/jni/client_sdk/client.h: -------------------------------------------------------------------------------- 1 | #ifndef _CLIENT_H_ 2 | #define _CLIENT_H_ 3 | 4 | #include "eventloop.h" 5 | 6 | namespace gim 7 | { 8 | class Client 9 | { 10 | public: 11 | Client(){}; 12 | ~Client(){}; 13 | 14 | std::string getSN(); 15 | 16 | int32 init(); 17 | int32 login(const std::string& srvip, int32 srvport, const std::string& cid, const std::string& pwd, int32 enc, const std::string& version); 18 | int32 stop(); 19 | int32 disconnect(const std::string& cid); 20 | int32 sendPeerMessage(const std::string& cid, const std::string& sn, const std::string& peercid, const std::string& data); 21 | virtual int handleMessage(const std::string& msg); 22 | private: 23 | static int eventLoopMsgRoutine(void* cli, const std::string& msg); 24 | EventLoop m_evlp; 25 | int64 m_sn; 26 | }; 27 | } 28 | 29 | #endif //_CLIENT_H_ 30 | -------------------------------------------------------------------------------- /clientdemo/jni/client_sdk/client_def.h: -------------------------------------------------------------------------------- 1 | #ifndef CLIENT_DEF_H 2 | #define CLIENT_DEF_H 3 | 4 | namespace gim 5 | { 6 | typedef enum _LoginStatus 7 | { 8 | STATUS_DO_LOGIN = 1, 9 | STATUS_LOGIN = 2, 10 | STATUS_LOGIN_FAIL = 3, 11 | STATUS_DISCONNECT = 4 12 | }LoginStatus; 13 | 14 | typedef enum _GResult 15 | { 16 | MY_OK = 0, 17 | MY_ERROR = -1, 18 | MY_NETWORK_ERROR = -9999, 19 | MY_PROBUF_FORMAT_ERROR = -9998, 20 | MY_TOO_LONG_PACKET = -9997, 21 | MY_JNI_ERROR = -9996, 22 | MY_JSON_ERROR = -9995, 23 | MY_UNDEFINED_CMD = -9994, 24 | MY_NOT_LOGGED = -9993, 25 | MY_TIMEOUT = -9992 26 | }GResult; 27 | 28 | typedef enum _NotifyType 29 | { 30 | NOTIFY_TYPE_LOGIN_STATUS_CHANGE = 0, 31 | NOTIFY_TYPE_PEER_MSG = 200, 32 | NOTIFY_TYPE_PEER_SEND_RESP = 201, 33 | NOTIFY_TYPE_PEER_OFFLINE_MSG = 202 34 | }NotifyType; 35 | 36 | }; 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /clientdemo/jni/client_sdk/client_log.h: -------------------------------------------------------------------------------- 1 | #ifndef _CLIENT_SDK_LOG_H_ 2 | #define _CLIENT_SDK_LOG_H_ 3 | 4 | #include 5 | #include "client_def.h" 6 | #include 7 | #include 8 | 9 | namespace gim 10 | { 11 | typedef enum _LogLevel 12 | { 13 | LOG_LEVEL_TRACE = 1, 14 | LOG_LEVEL_WARN, 15 | LOG_LEVEL_DEBUG, 16 | LOG_LEVEL_ERROR 17 | }LogLevel; 18 | 19 | void logprint(LogLevel level, const char* logbuf); 20 | 21 | #ifdef _DEBUG 22 | #define SDK_LOG(lvl, format, ...)\ 23 | {\ 24 | char buf[1024];\ 25 | snprintf(buf, sizeof(buf), "[lvl:%d] " format" [src=%s:%d]\n", (int32)lvl, ##__VA_ARGS__, __FILE__, __LINE__ );\ 26 | logprint(lvl, buf); \ 27 | } 28 | #else 29 | #define SDK_LOG(lvl, format, ...)\ 30 | {\ 31 | if(lvl >= LOG_LEVEL_ERROR)\ 32 | {\ 33 | char buf[1024];\ 34 | snprintf(buf, sizeof(buf), "[lvl:%d] " format" [src=%s:%d]\n", (int32)lvl, ##__VA_ARGS__, __FILE__, __LINE__ );\ 35 | logprint(lvl, buf); \ 36 | }\ 37 | } 38 | #endif 39 | 40 | } 41 | 42 | 43 | #endif 44 | 45 | -------------------------------------------------------------------------------- /clientdemo/jni/client_sdk/err_no.h: -------------------------------------------------------------------------------- 1 | #ifndef ERR_NO_H 2 | #define ERR_NO_H 3 | 4 | namespace gim 5 | { 6 | enum 7 | { 8 | STATUS_OK = 0, 9 | INPUT_FORMAT_ERROR = -1, 10 | CHECK_TIME_FAIL = -2, 11 | CREATE_SESSION_FAIL = -10, 12 | GET_USER_KEY_FAIL = -11, 13 | DECRYPT_FAIL = -12, 14 | MISS_TOKEN = -13, 15 | CHECK_TOKEN_FAIL = -14, 16 | INVLID_SESSION_ID = -20, 17 | SESSION_TIMEOUT = -21, 18 | NO_SERVICE = -30, 19 | THIS_SERVICE_EMPTY = -31, 20 | SERVICE_EVENTLOOP_NULL = -32, 21 | SERVICE_TOO_BUSY = -33, 22 | INVALID_SN = -40, 23 | SN_TIMEOUT = -41, 24 | INNER_ERROR = -100, 25 | 26 | CONNECT_SERVER_FAIL = -600000, 27 | SEND_FAIL = -600010, 28 | REQUEST_TIME_OUT = -600020 29 | }; 30 | 31 | inline const char* getErrStr(int32 e){ 32 | switch (e){ 33 | case STATUS_OK: 34 | return "STATUS_OK"; 35 | case INPUT_FORMAT_ERROR: 36 | return "INPUT_FORMAT_ERROR"; 37 | case GET_USER_KEY_FAIL: 38 | return "GET_USER_KEY_FAIL"; 39 | case DECRYPT_FAIL: 40 | return "DECRYPT_FAIL"; 41 | case MISS_TOKEN: 42 | return "MISS_TOKEN"; 43 | case CHECK_TOKEN_FAIL: 44 | return "CHECK_TOKEN_FAIL"; 45 | case CREATE_SESSION_FAIL: 46 | return "CREATE_SESSION_FAIL"; 47 | case INVLID_SESSION_ID: 48 | return "INVLID_SESSION_ID"; 49 | case NO_SERVICE: 50 | return "NO_SERVICE"; 51 | case THIS_SERVICE_EMPTY: 52 | return "THIS_SERVICE_EMPTY"; 53 | case SERVICE_EVENTLOOP_NULL: 54 | return "SERVICE_EVENTLOOP_NULL"; 55 | case SESSION_TIMEOUT: 56 | return "SESSION_TIMEOUT"; 57 | case SERVICE_TOO_BUSY: 58 | return "SERVICE_TOO_BUSY"; 59 | case INVALID_SN: 60 | return "INVALID_SN"; 61 | case SN_TIMEOUT: 62 | return "SN_TIMEOUT"; 63 | case INNER_ERROR: 64 | return "INNER_ERROR"; 65 | case REQUEST_TIME_OUT: 66 | return "REQUEST_TIME_OUT"; 67 | case SEND_FAIL: 68 | return "SEND_FAIL"; 69 | } 70 | return ""; 71 | } 72 | 73 | }; 74 | 75 | 76 | #endif 77 | -------------------------------------------------------------------------------- /clientdemo/jni/client_sdk/eventloop.h: -------------------------------------------------------------------------------- 1 | #ifndef _EVENT_LOOP_H_ 2 | #define _EVENT_LOOP_H_ 3 | #include "common/ef_sock.h" 4 | #include "common/ef_thread.h" 5 | #include "common/ef_loop_buf.h" 6 | #include "opbase.h" 7 | #include 8 | #include 9 | 10 | using namespace ef; 11 | namespace gim 12 | { 13 | typedef int32 (*MSG_HANDLE_CB)(void* context, const std::string& msg); 14 | 15 | class EventLoop 16 | { 17 | friend class Op; 18 | public: 19 | EventLoop(); 20 | ~EventLoop(); 21 | void setMsgCb(MSG_HANDLE_CB cb, void* context); 22 | int32 publish(const std::string& msg); 23 | int32 startLoop(); 24 | int32 asynAddOp(Op* op); 25 | int32 asynStop(); 26 | CliConn* findConn(const std::string& cid); 27 | CliConn* addConn(const std::string& cid); 28 | int32 delConn(const std::string& cid); 29 | private: 30 | int32 delConnAll_(); 31 | int32 startCtl(); 32 | int32 stopCtl(); 33 | int32 run(); 34 | int32 onStopAndWait(); 35 | static int32 workThreadProcess(EventLoop* el); 36 | int32 processOps(); 37 | int32 processTimers(struct timeval& tv); 38 | private: 39 | SOCKET m_ctlfdr; 40 | SOCKET m_ctlfdw; 41 | THREADHANDLE m_thread; 42 | bool m_run; 43 | MUTEX m_ops_mtx; 44 | loop_buf m_ops; 45 | 46 | typedef std::map CliConnMap; 47 | CliConnMap m_conns; 48 | MSG_HANDLE_CB m_msgHandler; 49 | void* m_MsgHandleCtx; 50 | }; 51 | } 52 | #endif -------------------------------------------------------------------------------- /clientdemo/jni/client_sdk/logic_common.h: -------------------------------------------------------------------------------- 1 | #ifndef LOGIC_COMMON_H_ 2 | #define LOGIC_COMMON_H_ 3 | 4 | #include 5 | #include "common/ef_btype.h" 6 | #include "common/ef_utility.h" 7 | 8 | namespace gim 9 | { 10 | class head; 11 | using namespace ef; 12 | enum 13 | { 14 | MAGIC_NUMBER = 0x20140417, 15 | }; 16 | 17 | int32 constructRespPacket(int32 srvcmd, const std::string& body, std::string& respbuf); 18 | 19 | int32 constructReqPacket(const head& h, const std::string& body, std::string& respbuf); 20 | 21 | int32 constructServiceRequest(const std::string& sessid, int32 service_type, 22 | const std::string& sn, const std::string& payload, std::string& req); 23 | 24 | int32 constructServiceResponse(const std::string& fromsession, const std::string& tosession, int32 status, int32 service_type, 25 | const std::string& sn, const std::string& payload, std::string& req); 26 | 27 | }; 28 | 29 | #endif/*LOGIC_COMMON_H*/ 30 | -------------------------------------------------------------------------------- /clientdemo/jni/client_sdk/msg_head.h: -------------------------------------------------------------------------------- 1 | #ifndef MSG_HEAD_H 2 | #define MSG_HEAD_H 3 | #include "common/ef_btype.h" 4 | 5 | using namespace ef; 6 | 7 | namespace gim 8 | { 9 | enum 10 | { 11 | KEEPALIVE_REQ = 0, 12 | KEEPALIVE_RESP = KEEPALIVE_REQ + 1, 13 | LOGIN_REQ = 100, 14 | LOGIN_RESP = LOGIN_REQ + 1, 15 | SERVICE_CMD_REQ = 200, 16 | SERVICE_CMD_RESP = SERVICE_CMD_REQ + 1, 17 | SERVICE_REG_REQ = 300, 18 | SERVICE_REG_RESP = SERVICE_REG_REQ + 1, 19 | KICK_CLIENT = 400, 20 | REDIRECT_RESP = 1001, 21 | SET_TIME_RESP = 1101, 22 | JSON_PUSH_REQ = 9999, 23 | JSON_PUSH_RESP = JSON_PUSH_REQ + 1, 24 | SERVER_CMD_SERVICE_REQ = 6666, 25 | SERVER_CMD_TOPIC_REQ = SERVER_CMD_SERVICE_REQ, 26 | SERVER_SERVICE_RESP = SERVER_CMD_SERVICE_REQ + 1 27 | }; 28 | 29 | struct head 30 | { 31 | int32 magic; 32 | int32 len;//include head 33 | //0: keepalive, 1: keepalive resp 34 | //100: login, 101: login resp 35 | //200: service, 201: service resp 36 | //300: regist service 301: regist service resp 37 | //400: kick client 38 | //1001: redericet 39 | //9999:json push request 10000:json push response 40 | int32 cmd; 41 | }; 42 | }; 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /clientdemo/jni/client_sdk/opbase.h: -------------------------------------------------------------------------------- 1 | #ifndef _OP_BASE_H_ 2 | #define _OP_BASE_H_ 3 | 4 | #include 5 | #include 6 | using namespace ef; 7 | namespace gim 8 | { 9 | class EventLoop; 10 | class CliConn; 11 | 12 | #define OP_DEFAULT_TIMEOUT 30 13 | 14 | // ops do process in eventloop, and wait for respone in connection 15 | class Op 16 | { 17 | friend class SmartOp; 18 | public: 19 | Op(const std::string& sn, const std::string& cid); 20 | virtual ~Op(){}; 21 | virtual int32 process(EventLoop* el); 22 | virtual int32 process(CliConn * conn); 23 | void setTimeout(int32 sec); 24 | int32 getTimeout() const; 25 | virtual int32 OnTimeout(CliConn* conn); 26 | virtual int32 OnCancel(CliConn* conn); 27 | virtual int32 onRespone(CliConn* conn, int32 status, const std::string& payload); 28 | std::string getSN() const; 29 | std::string getCid()const; 30 | protected: 31 | int32 increase_(); 32 | int32 decrease_(); 33 | private: 34 | volatile int32 m_ref; 35 | std::string m_sn; 36 | std::string m_cid; 37 | int32 m_timeout; 38 | }; 39 | 40 | class SmartOp 41 | { 42 | public: 43 | SmartOp(Op* op); 44 | SmartOp(const SmartOp& oth); 45 | SmartOp& operator = (const SmartOp& right); 46 | ~SmartOp(); 47 | void reset(Op* op); 48 | int32 addref(); 49 | int32 release(); 50 | Op* get() const; 51 | Op* operator ->() const; 52 | protected: 53 | Op* m_op; 54 | private: 55 | }; 56 | } 57 | #endif 58 | -------------------------------------------------------------------------------- /clientdemo/jni/client_sdk/service_type.h: -------------------------------------------------------------------------------- 1 | #ifndef SERVICE_TYPE_H 2 | #define SERVICE_TYPE_H 3 | 4 | namespace gim 5 | { 6 | typedef enum _ServiceType 7 | { 8 | SERVICE_PUSH = 100, 9 | SERVICE_PEER = 200, 10 | SERVICE_GROUP = 300, 11 | SERVICE_VERIFY = 500, 12 | SERVICE_TOPIC = 600 13 | }ServiceType; 14 | }; 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /clientdemo/jni/common/ef_aes.cpp: -------------------------------------------------------------------------------- 1 | #include "ef_aes.h" 2 | #include "ef_md5.h" 3 | #include "ef_hex.h" 4 | #include "rijndael-api-fst.h" 5 | 6 | namespace ef{ 7 | int32 aes_encrypt(const std::string& src, 8 | const std::string& key, std::string& dst){ 9 | int ret = 0; 10 | keyInstance keyist; 11 | u8 keymd5[16] = {0}; 12 | u8 keymat[33] = {0}; 13 | ef::MD5(keymd5, (const u8*)key.data(), key.size()); 14 | ef::bytes_to_hexs((const char*)keymd5, 16, (char*)keymat, 32); 15 | ret = makeKey(&keyist, DIR_ENCRYPT, 128, keymat); 16 | if(ret < 0){ 17 | return ret; 18 | } 19 | 20 | cipherInstance cnst; 21 | ret = cipherInit(&cnst, MODE_ECB, (char*)keymat); 22 | if(ret < 0){ 23 | return ret; 24 | } 25 | int len = src.size(); 26 | dst.resize(len + 16 - len % 16); 27 | BYTE* encout = (BYTE*)dst.data(); 28 | ret = padEncrypt(&cnst, &keyist, (BYTE*)src.data(), len, encout); 29 | if(ret < 0) 30 | return ret; 31 | dst.resize(ret); 32 | return ret; 33 | } 34 | 35 | int32 aes_decrypt(const std::string& src, 36 | const std::string& key, std::string& dst){ 37 | int ret = 0; 38 | keyInstance keyist; 39 | u8 keymd5[16]; 40 | u8 keymat[33] = {0}; 41 | ef::MD5(keymd5, (const u8*)key.data(), key.size()); 42 | ef::bytes_to_hexs((const char*)keymd5, 16, (char*)keymat, 32); 43 | ret = makeKey(&keyist, DIR_DECRYPT, 128, keymat); 44 | if(ret < 0){ 45 | return ret; 46 | } 47 | 48 | cipherInstance cnst; 49 | ret = cipherInit(&cnst, MODE_ECB, (char*)keymat); 50 | if(ret < 0){ 51 | return ret; 52 | } 53 | int len = src.size(); 54 | dst.resize(len + 16 - len % 16); 55 | BYTE* encout = (BYTE*)dst.data(); 56 | ret = padDecrypt(&cnst, &keyist, (BYTE*)src.data(), len, encout); 57 | if(ret < 0) 58 | return ret; 59 | dst.resize(ret); 60 | return ret; 61 | } 62 | }; -------------------------------------------------------------------------------- /clientdemo/jni/common/ef_aes.h: -------------------------------------------------------------------------------- 1 | #ifndef EF_AES_H 2 | #define EF_AES_H 3 | 4 | #include 5 | #include "ef_btype.h" 6 | 7 | 8 | namespace ef{ 9 | 10 | /* Error Codes */ 11 | #define AES_BAD_KEY_DIR -1 /* Key direction is invalid, e.g., unknown value */ 12 | #define AES_BAD_KEY_MAT -2 /* Key material not of correct length */ 13 | #define AES_BAD_KEY_INSTANCE -3 /* Key passed is not valid */ 14 | #define AES_BAD_CIPHER_MODE -4 /* Params struct passed to cipherInit invalid */ 15 | #define AES_BAD_CIPHER_STATE -5 /* Cipher in wrong state (e.g., not initialized) */ 16 | #define AES_BAD_BLOCK_LENGTH -6 17 | #define AES_BAD_CIPHER_INSTANCE -7 18 | #define AES_BAD_DATA -8 /* Data contents are invalid, e.g., invalid padding */ 19 | #define AES_BAD_OTHER -9 /* Unknown error */ 20 | 21 | int32 aes_encrypt(const std::string& src, 22 | const std::string& key, std::string& dst); 23 | int32 aes_decrypt(const std::string& src, 24 | const std::string& key, std::string& dst); 25 | }; 26 | 27 | #endif -------------------------------------------------------------------------------- /clientdemo/jni/common/ef_base64.h: -------------------------------------------------------------------------------- 1 | #ifndef EF_BASE64_H 2 | #define EF_BASE64_H 3 | 4 | #include 5 | 6 | namespace ef{ 7 | 8 | std::string base64_encode(const std::string& s); 9 | std::string base64_decode(const std::string& s); 10 | 11 | } 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /clientdemo/jni/common/ef_btype.h: -------------------------------------------------------------------------------- 1 | #ifndef EF_BTYPE_H 2 | #define EF_BTYPE_H 3 | 4 | #ifdef __linux 5 | #include 6 | #endif 7 | 8 | 9 | namespace ef{ 10 | 11 | #ifdef __linux 12 | typedef u_int64_t uint64; 13 | typedef int64_t sint64; 14 | typedef int64_t int64; 15 | typedef u_int32_t uint32; 16 | typedef int32_t sint32; 17 | typedef int32_t int32; 18 | typedef u_int64_t uint16; 19 | typedef int16_t sint16; 20 | typedef int16_t int16; 21 | typedef u_int8_t uint8; 22 | typedef int8_t sint8; 23 | typedef int8_t int8; 24 | #else 25 | typedef unsigned long long uint64; 26 | typedef long long sint64; 27 | typedef long long int64; 28 | typedef unsigned long ulong; 29 | typedef unsigned int uint32; 30 | typedef int sint32; 31 | typedef int int32; 32 | typedef unsigned short int uint16; 33 | typedef short int sint16; 34 | typedef short int int16; 35 | typedef unsigned char uint8; 36 | typedef char sint8; 37 | typedef char int8; 38 | #endif 39 | 40 | }//namespace ef 41 | 42 | 43 | #endif 44 | 45 | -------------------------------------------------------------------------------- /clientdemo/jni/common/ef_hex.h: -------------------------------------------------------------------------------- 1 | #ifndef EF_HEX_H 2 | #define EF_HEX_H 3 | 4 | #include "ef_btype.h" 5 | #include 6 | 7 | namespace ef{ 8 | 9 | 10 | char* byte_to_hex(uint8 c, char buf[2]); 11 | int hex_to_byte(char buf[2]); 12 | 13 | int32 bytes_to_hexs(const std::string& bytes, std::string& hex); 14 | int32 bytes_to_hexs(const char* bytes, int32 len, char* out, int32 outlen); 15 | int32 hexs_to_bytes(const std::string& hex, std::string& bytes); 16 | int32 hexs_to_bytes(const char* hex, int32 len, char* out, int32 outlen); 17 | 18 | int32 hexs_bytes_test(const std::string& input); 19 | 20 | }; 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /clientdemo/jni/common/ef_loop_buf.h: -------------------------------------------------------------------------------- 1 | #ifndef EF_LOOP_BUF_H 2 | #define EF_LOOP_BUF_H 3 | 4 | #include"ef_btype.h" 5 | 6 | namespace ef{ 7 | 8 | class loop_buf{ 9 | public: 10 | loop_buf(int32 cap = 1024); 11 | ~loop_buf(); 12 | 13 | int32 capacity() const; 14 | int32 resize(int32 cap); 15 | //if buf is null, just drop data 16 | int32 read(uint8 *buf, int32 len); 17 | //if buf is null, just inc size 18 | int32 write(const uint8 *buf, int32 len); 19 | int32 auto_resize_write(const uint8 *buf, int32 len); 20 | int32 peek(uint8 *buf, int32 len) const; 21 | int32 first_half_len() const; 22 | const uint8* first_half() const; 23 | int32 next_part_len() const; 24 | uint8* next_part(); 25 | int32 clear(); 26 | int32 size() const{ 27 | return m_size; 28 | } 29 | 30 | private: 31 | uint8 *m_buf; 32 | int32 m_cap; 33 | int32 m_size; 34 | int32 m_start; 35 | 36 | }; 37 | 38 | }; 39 | 40 | 41 | #endif/**/ 42 | -------------------------------------------------------------------------------- /clientdemo/jni/common/ef_md5.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2004-2005 Sergey Lyubka 3 | * All rights reserved 4 | * 5 | * "THE BEER-WARE LICENSE" (Revision 42): 6 | * Sergey Lyubka wrote this file. As long as you retain this notice you 7 | * can do whatever you want with this stuff. If we meet some day, and you think 8 | * this stuff is worth it, you can buy me a beer in return. 9 | */ 10 | /**************************************************************** 11 | * 12 | * Thanks for Sergey Lynbka, I hope I could meet U and buy U a beer 13 | * 14 | * ***************************************************************/ 15 | 16 | #ifndef EF_MD5_H 17 | #define EF_MD5_H 18 | 19 | #include "ef_btype.h" 20 | #include "ef_hex.h" 21 | 22 | namespace ef{ 23 | 24 | typedef struct MD5Context { 25 | uint32 buf[4]; 26 | uint32 bits[2]; 27 | uint8 in[64]; 28 | } MD5_CTX; 29 | 30 | void MD5Init(MD5_CTX *ctx); 31 | void MD5Update(MD5_CTX *ctx, uint8 const *buf, uint32 len); 32 | void MD5Final(uint8 digest[16], MD5_CTX *ctx); 33 | void MD5(uint8 digest[16], uint8 const *buf, uint32 len); 34 | void MD5Hex(std::string& hex, uint8 const *buf, uint32 len); 35 | 36 | int32 file_md5 (char const * fname, uint8 * md5val); 37 | 38 | } 39 | 40 | #endif /*MD5_HEADER_INCLUDED */ 41 | -------------------------------------------------------------------------------- /clientdemo/jni/common/ef_no_copy.h: -------------------------------------------------------------------------------- 1 | #ifndef EF_NO_COPY_H 2 | #define EF_NO_COPY_H 3 | 4 | namespace ef{ 5 | 6 | /// base class to disable copy and asign construct 7 | class NoCopy 8 | { 9 | protected: 10 | NoCopy() {} 11 | virtual ~NoCopy() {} 12 | private: 13 | NoCopy(const NoCopy &); 14 | NoCopy &operator=(const NoCopy &); 15 | }; 16 | 17 | }; 18 | 19 | #endif 20 | 21 | -------------------------------------------------------------------------------- /clientdemo/jni/common/ef_singleton.h: -------------------------------------------------------------------------------- 1 | #ifndef EF_SINGLETON_H 2 | #define EF_SINGLETON_H 3 | 4 | #include 5 | #include 6 | 7 | #include "ef_no_copy.h" 8 | 9 | namespace ef{ 10 | 11 | template 12 | class Singleton : 13 | NoCopy 14 | { 15 | public: 16 | static T *instance() { 17 | pthread_once(&m_ponce, init); 18 | return m_ptr; 19 | } 20 | 21 | private: 22 | Singleton(); 23 | ~Singleton(); 24 | 25 | static void init() { 26 | m_ptr = new T(); 27 | ::atexit(destroy); 28 | } 29 | 30 | static void destroy() { 31 | delete m_ptr; 32 | } 33 | 34 | private: 35 | static pthread_once_t m_ponce; 36 | static T *m_ptr; 37 | }; 38 | 39 | template 40 | pthread_once_t Singleton::m_ponce = PTHREAD_ONCE_INIT; 41 | 42 | template 43 | T *Singleton::m_ptr = NULL; 44 | 45 | }; 46 | 47 | #endif 48 | 49 | -------------------------------------------------------------------------------- /clientdemo/jni/common/ef_sock.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmxiaocong/gim/f9bac9be3d2afdc9bea69d201d1ce3c10a56639e/clientdemo/jni/common/ef_sock.cpp -------------------------------------------------------------------------------- /clientdemo/jni/common/ef_utility.h: -------------------------------------------------------------------------------- 1 | #ifndef EF_UTILITY_H 2 | #define EF_UTILITY_H 3 | 4 | #include "ef_btype.h" 5 | 6 | #ifndef _WIN32 7 | #include 8 | #include 9 | #endif 10 | #include 11 | #include 12 | #include 13 | 14 | namespace ef{ 15 | 16 | #ifdef _WIN32 17 | int gettimeofday(struct timeval *tp, struct timezone *tz); 18 | #endif 19 | 20 | int64 gettime_ms(); 21 | 22 | int tv_cmp(struct timeval t1, struct timeval t2); 23 | 24 | struct timeval tv_diff(struct timeval t1, struct timeval t2); 25 | 26 | int split(const std::string& str, std::vector& ret_, 27 | std::string sep = ","); 28 | 29 | template 30 | std::string itostr(const T& t) 31 | { 32 | std::stringstream oss; 33 | oss << t; 34 | return oss.str(); 35 | } 36 | }; 37 | 38 | #endif/*EF_UTILITY_H*/ 39 | 40 | -------------------------------------------------------------------------------- /clientdemo/jni/common/err_no.h: -------------------------------------------------------------------------------- 1 | #ifndef ERR_NO_H 2 | #define ERR_NO_H 3 | 4 | 5 | namespace meet_you{ 6 | 7 | 8 | enum{ 9 | STATUS_OK = 0, 10 | INPUT_FORMAT_ERROR = -1, 11 | CHECK_TIME_FAIL = -2, 12 | CREATE_SESSION_FAIL = -10, 13 | GET_USER_KEY_FAIL = -11, 14 | DECRYPT_FAIL = -12, 15 | MISS_TOKEN = -13, 16 | CHECK_TOKEN_FAIL = -14, 17 | INVLID_SESSION_ID = -20, 18 | SESSION_TIMEOUT = -21, 19 | NO_SERVICE = -30, 20 | THIS_SERVICE_EMPTY = -31, 21 | SERVICE_EVENTLOOP_NULL = -32, 22 | SERVICE_TOO_BUSY = -33, 23 | INVALID_SN = -40, 24 | SN_TIMEOUT = -41, 25 | INNER_ERROR = -100, 26 | 27 | CONNECT_SERVER_FAIL = -600000, 28 | SEND_FAIL = -600010, 29 | REQUEST_TIME_OUT = -600020 30 | }; 31 | 32 | inline const char* getErrStr(int e){ 33 | switch(e){ 34 | case STATUS_OK: 35 | return "STATUS_OK"; 36 | case INPUT_FORMAT_ERROR: 37 | return "INPUT_FORMAT_ERROR"; 38 | case GET_USER_KEY_FAIL: 39 | return "GET_USER_KEY_FAIL"; 40 | case DECRYPT_FAIL: 41 | return "DECRYPT_FAIL"; 42 | case MISS_TOKEN: 43 | return "MISS_TOKEN"; 44 | case CHECK_TOKEN_FAIL: 45 | return "CHECK_TOKEN_FAIL"; 46 | case CREATE_SESSION_FAIL: 47 | return "CREATE_SESSION_FAIL"; 48 | case INVLID_SESSION_ID: 49 | return "INVLID_SESSION_ID"; 50 | case NO_SERVICE: 51 | return "NO_SERVICE"; 52 | case THIS_SERVICE_EMPTY: 53 | return "THIS_SERVICE_EMPTY"; 54 | case SERVICE_EVENTLOOP_NULL: 55 | return "SERVICE_EVENTLOOP_NULL"; 56 | case SESSION_TIMEOUT: 57 | return "SESSION_TIMEOUT"; 58 | case SERVICE_TOO_BUSY: 59 | return "SERVICE_TOO_BUSY"; 60 | case INVALID_SN: 61 | return "INVALID_SN"; 62 | case SN_TIMEOUT: 63 | return "SN_TIMEOUT"; 64 | case INNER_ERROR: 65 | return "INNER_ERROR"; 66 | case REQUEST_TIME_OUT: 67 | return "REQUEST_TIME_OUT"; 68 | case SEND_FAIL: 69 | return "SEND_FAIL"; 70 | case MY_JNI_ERROR: 71 | return "JNI_ERROR"; 72 | } 73 | return ""; 74 | } 75 | 76 | }; 77 | 78 | 79 | #endif 80 | -------------------------------------------------------------------------------- /clientdemo/jni/config.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmxiaocong/gim/f9bac9be3d2afdc9bea69d201d1ce3c10a56639e/clientdemo/jni/config.h -------------------------------------------------------------------------------- /clientdemo/jni/json/autolink.h: -------------------------------------------------------------------------------- 1 | #ifndef JSON_AUTOLINK_H_INCLUDED 2 | # define JSON_AUTOLINK_H_INCLUDED 3 | 4 | # include "config.h" 5 | 6 | # ifdef JSON_IN_CPPTL 7 | # include 8 | # endif 9 | 10 | # if !defined(JSON_NO_AUTOLINK) && !defined(JSON_DLL_BUILD) && !defined(JSON_IN_CPPTL) 11 | # define CPPTL_AUTOLINK_NAME "json" 12 | # undef CPPTL_AUTOLINK_DLL 13 | # ifdef JSON_DLL 14 | # define CPPTL_AUTOLINK_DLL 15 | # endif 16 | # include "autolink.h" 17 | # endif 18 | 19 | #endif // JSON_AUTOLINK_H_INCLUDED 20 | -------------------------------------------------------------------------------- /clientdemo/jni/json/config.h: -------------------------------------------------------------------------------- 1 | #ifndef JSON_CONFIG_H_INCLUDED 2 | # define JSON_CONFIG_H_INCLUDED 3 | 4 | /// If defined, indicates that json library is embedded in CppTL library. 5 | //# define JSON_IN_CPPTL 1 6 | 7 | /// If defined, indicates that json may leverage CppTL library 8 | //# define JSON_USE_CPPTL 1 9 | /// If defined, indicates that cpptl vector based map should be used instead of std::map 10 | /// as Value container. 11 | //# define JSON_USE_CPPTL_SMALLMAP 1 12 | /// If defined, indicates that Json specific container should be used 13 | /// (hash table & simple deque container with customizable allocator). 14 | /// THIS FEATURE IS STILL EXPERIMENTAL! 15 | //# define JSON_VALUE_USE_INTERNAL_MAP 1 16 | /// Force usage of standard new/malloc based allocator instead of memory pool based allocator. 17 | /// The memory pools allocator used optimization (initializing Value and ValueInternalLink 18 | /// as if it was a POD) that may cause some validation tool to report errors. 19 | /// Only has effects if JSON_VALUE_USE_INTERNAL_MAP is defined. 20 | //# define JSON_USE_SIMPLE_INTERNAL_ALLOCATOR 1 21 | 22 | /// If defined, indicates that Json use exception to report invalid type manipulation 23 | /// instead of C assert macro. 24 | # define JSON_USE_EXCEPTION 1 25 | 26 | # ifdef JSON_IN_CPPTL 27 | # include 28 | # ifndef JSON_USE_CPPTL 29 | # define JSON_USE_CPPTL 1 30 | # endif 31 | # endif 32 | 33 | # ifdef JSON_IN_CPPTL 34 | # define JSON_API CPPTL_API 35 | # elif defined(JSON_DLL_BUILD) 36 | # define JSON_API __declspec(dllexport) 37 | # elif defined(JSON_DLL) 38 | # define JSON_API __declspec(dllimport) 39 | # else 40 | # define JSON_API 41 | # endif 42 | 43 | #endif // JSON_CONFIG_H_INCLUDED 44 | -------------------------------------------------------------------------------- /clientdemo/jni/json/features.h: -------------------------------------------------------------------------------- 1 | #ifndef CPPTL_JSON_FEATURES_H_INCLUDED 2 | # define CPPTL_JSON_FEATURES_H_INCLUDED 3 | 4 | # include "forwards.h" 5 | 6 | namespace Json { 7 | 8 | /** \brief Configuration passed to reader and writer. 9 | * This configuration object can be used to force the Reader or Writer 10 | * to behave in a standard conforming way. 11 | */ 12 | class JSON_API Features 13 | { 14 | public: 15 | /** \brief A configuration that allows all features and assumes all strings are UTF-8. 16 | * - C & C++ comments are allowed 17 | * - Root object can be any JSON value 18 | * - Assumes Value strings are encoded in UTF-8 19 | */ 20 | static Features all(); 21 | 22 | /** \brief A configuration that is strictly compatible with the JSON specification. 23 | * - Comments are forbidden. 24 | * - Root object must be either an array or an object value. 25 | * - Assumes Value strings are encoded in UTF-8 26 | */ 27 | static Features strictMode(); 28 | 29 | /** \brief Initialize the configuration like JsonConfig::allFeatures; 30 | */ 31 | Features(); 32 | 33 | /// \c true if comments are allowed. Default: \c true. 34 | bool allowComments_; 35 | 36 | /// \c true if root must be either an array or an object value. Default: \c false. 37 | bool strictRoot_; 38 | }; 39 | 40 | } // namespace Json 41 | 42 | #endif // CPPTL_JSON_FEATURES_H_INCLUDED 43 | -------------------------------------------------------------------------------- /clientdemo/jni/json/forwards.h: -------------------------------------------------------------------------------- 1 | #ifndef JSON_FORWARDS_H_INCLUDED 2 | # define JSON_FORWARDS_H_INCLUDED 3 | 4 | # include "config.h" 5 | 6 | namespace Json { 7 | 8 | // writer.h 9 | class FastWriter; 10 | class StyledWriter; 11 | 12 | // reader.h 13 | class Reader; 14 | 15 | // features.h 16 | class Features; 17 | 18 | // value.h 19 | typedef int Int; 20 | typedef unsigned int UInt; 21 | class StaticString; 22 | class Path; 23 | class PathArgument; 24 | class Value; 25 | class ValueIteratorBase; 26 | class ValueIterator; 27 | class ValueConstIterator; 28 | #ifdef JSON_VALUE_USE_INTERNAL_MAP 29 | class ValueAllocator; 30 | class ValueMapAllocator; 31 | class ValueInternalLink; 32 | class ValueInternalArray; 33 | class ValueInternalMap; 34 | #endif // #ifdef JSON_VALUE_USE_INTERNAL_MAP 35 | 36 | } // namespace Json 37 | 38 | 39 | #endif // JSON_FORWARDS_H_INCLUDED 40 | -------------------------------------------------------------------------------- /clientdemo/jni/json/json.h: -------------------------------------------------------------------------------- 1 | #ifndef JSON_JSON_H_INCLUDED 2 | # define JSON_JSON_H_INCLUDED 3 | 4 | # include "autolink.h" 5 | # include "value.h" 6 | # include "reader.h" 7 | # include "writer.h" 8 | # include "features.h" 9 | 10 | #endif // JSON_JSON_H_INCLUDED 11 | -------------------------------------------------------------------------------- /clientdemo/jni/json/sconscript: -------------------------------------------------------------------------------- 1 | Import( 'env buildLibrary' ) 2 | 3 | buildLibrary( env, Split( """ 4 | json_reader.cpp 5 | json_value.cpp 6 | json_writer.cpp 7 | """ ), 8 | 'json' ) 9 | -------------------------------------------------------------------------------- /clientdemo/jni/proto/message.proto: -------------------------------------------------------------------------------- 1 | option optimize_for = LITE_RUNTIME; 2 | 3 | package gim; 4 | 5 | message Message{ 6 | optional string to = 1; 7 | optional int64 id = 2;//mssage id, did not set this field when send message 8 | optional int64 time = 3;//message send time 9 | optional string from = 4;//who send 10 | optional int32 type = 5; 11 | optional string sn = 6; 12 | optional bytes data = 7;//message data 13 | } 14 | 15 | 16 | -------------------------------------------------------------------------------- /clientdemo/jni/proto/pair.proto: -------------------------------------------------------------------------------- 1 | option optimize_for = LITE_RUNTIME; 2 | 3 | package gim; 4 | 5 | message Pair{ 6 | required string key = 1; 7 | optional bytes value = 2; 8 | } 9 | -------------------------------------------------------------------------------- /clientdemo/jni/proto/peer_server.proto: -------------------------------------------------------------------------------- 1 | option optimize_for = LITE_RUNTIME; 2 | 3 | package gim; 4 | 5 | import "message.proto"; 6 | 7 | message GetPeerMessageRequest{ 8 | optional string cid = 1;//to who 9 | optional int64 start_msgid = 2;//mssage id 10 | optional int64 count = 3;//message count 11 | } 12 | 13 | message GetPeerMessageResponse{ 14 | optional int64 last_msgid = 1; 15 | repeated Message msgs = 2; 16 | } 17 | 18 | message SendPeerMessageRequest{ 19 | required Message msg = 1; 20 | } 21 | 22 | //use for muti-point online 23 | message SendPeerMessageResponse{ 24 | required Message msg = 1; 25 | } 26 | 27 | message RecvPeerMessageResponse{ 28 | required Message msg = 1; 29 | } 30 | 31 | message PushMessageRequest{ 32 | required string sn = 1; 33 | required Message msg = 2; 34 | } 35 | 36 | message PushMessageResponse{ 37 | required string sn = 1; 38 | required int32 status = 2; 39 | } 40 | 41 | 42 | message PeerPacket{ 43 | required int32 cmd = 1;//110: send_req, 44 | //111: send_resp, 112: get_req, 45 | //113: get_resp, 115:recv_resp 46 | optional GetPeerMessageRequest get_peer_msg_req = 2; 47 | optional GetPeerMessageResponse get_peer_msg_resp = 3; 48 | optional SendPeerMessageRequest send_peer_msg_req = 4; 49 | optional SendPeerMessageResponse send_peer_msg_resp = 5; 50 | optional RecvPeerMessageResponse recv_peer_msg_resp = 6; 51 | } 52 | -------------------------------------------------------------------------------- /clientdemo/jni/proto/session.proto: -------------------------------------------------------------------------------- 1 | option optimize_for = LITE_RUNTIME; 2 | 3 | package gim; 4 | 5 | import "pair.proto"; 6 | 7 | message Sess{ 8 | optional string cid = 1; 9 | optional int64 lasttime = 2; 10 | optional string sessid = 3; 11 | optional int32 svid = 4; 12 | repeated Pair kvs = 5; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /clientdemo/proguard-project.txt: -------------------------------------------------------------------------------- 1 | # To enable ProGuard in your project, edit project.properties 2 | # to define the proguard.config property as described in that file. 3 | # 4 | # Add project specific ProGuard rules here. 5 | # By default, the flags in this file are appended to flags specified 6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt 7 | # You can edit the include path and order by changing the ProGuard 8 | # include property in project.properties. 9 | # 10 | # For more details, see 11 | # http://developer.android.com/guide/developing/tools/proguard.html 12 | 13 | # Add any project specific keep options here: 14 | 15 | # If your project uses WebView with JS, uncomment the following 16 | # and specify the fully qualified class name to the JavaScript interface 17 | # class: 18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 19 | # public *; 20 | #} 21 | -------------------------------------------------------------------------------- /clientdemo/project.properties: -------------------------------------------------------------------------------- 1 | # This file is automatically generated by Android Tools. 2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! 3 | # 4 | # This file must be checked in Version Control Systems. 5 | # 6 | # To customize properties used by the Ant build system edit 7 | # "ant.properties", and override values to adapt the script to your 8 | # project structure. 9 | # 10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home): 11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt 12 | 13 | # Project target. 14 | target=android-21 15 | android.library.reference.1=../appcompat_v7 16 | android.library.reference.2=../appcompat_v7 17 | -------------------------------------------------------------------------------- /clientdemo/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmxiaocong/gim/f9bac9be3d2afdc9bea69d201d1ce3c10a56639e/clientdemo/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /clientdemo/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmxiaocong/gim/f9bac9be3d2afdc9bea69d201d1ce3c10a56639e/clientdemo/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /clientdemo/res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmxiaocong/gim/f9bac9be3d2afdc9bea69d201d1ce3c10a56639e/clientdemo/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /clientdemo/res/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmxiaocong/gim/f9bac9be3d2afdc9bea69d201d1ce3c10a56639e/clientdemo/res/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /clientdemo/res/menu/main.xml: -------------------------------------------------------------------------------- 1 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /clientdemo/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 64dp 9 | 10 | 11 | -------------------------------------------------------------------------------- /clientdemo/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 16dp 5 | 16dp 6 | 7 | 8 | -------------------------------------------------------------------------------- /clientdemo/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | clientdemo 5 | Hello world! 6 | Settings 7 | 8 | 9 | -------------------------------------------------------------------------------- /clientdemo/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | 9 | 16 | 17 | 18 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /clientdemo/src/com/gim/MainActivity.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmxiaocong/gim/f9bac9be3d2afdc9bea69d201d1ce3c10a56639e/clientdemo/src/com/gim/MainActivity.java -------------------------------------------------------------------------------- /clientdemo/src/com/gim/client.java: -------------------------------------------------------------------------------- 1 | package com.gim; 2 | import com.gim.listener; 3 | 4 | public class client 5 | { 6 | public native int init(listener lstr); 7 | public native int stop(); 8 | public native int login(String srvip, int srvport, String cliver, int enc, String cid, String pwd); 9 | public native int disconnect(String cid); 10 | public native int sendPeerMessage(String cid, String sn, String peercid, String data); 11 | static { 12 | System.loadLibrary("clientsdk"); 13 | } 14 | } -------------------------------------------------------------------------------- /clientdemo/src/com/gim/listener.java: -------------------------------------------------------------------------------- 1 | package com.gim; 2 | 3 | /** 4 | * Created with IntelliJ IDEA. R 5 | * Date: 14-9-29 6 | */ 7 | public interface listener { 8 | public void handleMessage(String l); 9 | } -------------------------------------------------------------------------------- /common/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.6) 2 | project(common) 3 | set(CMAKE_VERBOSE_MAKEFILE ON) 4 | add_definitions("-Wall -g") 5 | set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib) 6 | 7 | include_directories(/usr/local/include) 8 | include_directories(${PROJECT_SOURCE_DIR}/../efnfw include) 9 | AUX_SOURCE_DIRECTORY(src SRC_LIST) 10 | add_library(common STATIC ${SRC_LIST}) 11 | #file(GLOB_RECURSE EF_LIST *.cpp) 12 | #add_library(cachegroup STATIC ${EF_LIST}) 13 | -------------------------------------------------------------------------------- /common/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | cp -fr ../proto . 3 | 4 | protoc proto/session.proto --cpp_out=proto -I=proto 5 | mv proto/session.pb.cc src/session.pb.cpp 6 | 7 | cp -f proto/*.pb.* include/ 8 | 9 | rm -fr proto 10 | 11 | mkdir -p lib 12 | rm -fr CMakeCache.txt 13 | rm -fr CMakeFiles 14 | cmake . 15 | make clean;make 16 | rm -fr CMakeCache.txt 17 | rm -fr CMakeFiles 18 | -------------------------------------------------------------------------------- /common/include/dynamic_tokenchk.h: -------------------------------------------------------------------------------- 1 | #ifndef __DYNAMIC_TOKEN_CHECKER_H__ 2 | #define __DYNAMIC_TOKEN_CHECKER_H__ 3 | 4 | #include "token_checker.h" 5 | #include "zk_client.h" 6 | #include "zk_server_ob.h" 7 | #include "zk_config.h" 8 | #include 9 | 10 | namespace gim { 11 | 12 | class DynamicTokenChk; 13 | class KeyWatcher;; 14 | 15 | struct kctx { 16 | DynamicTokenChk *tkchk; 17 | KeyWatcher *kwch; 18 | kctx():tkchk(NULL),kwch(NULL){}; 19 | }; 20 | 21 | class KeyWatcher { 22 | public: 23 | KeyWatcher(ZKClient* c, const std::string &mainpath, const std::string &subpath, 24 | DynamicTokenChk *ctx); 25 | ~KeyWatcher(){ 26 | if (m_zkcfg) delete m_zkcfg; 27 | if (m_kctx) delete m_kctx; 28 | } 29 | std::string &subpath(){return m_subpath;}; 30 | 31 | private: 32 | ZKConfig *m_zkcfg; 33 | kctx *m_kctx; 34 | std::string m_mainpath; 35 | std::string m_subpath; 36 | static void keyChangeCallBack(void* ctx, int version, const Json::Value& notify); 37 | }; 38 | 39 | class DynamicTokenChk : public TokenChecker { 40 | public: 41 | DynamicTokenChk():m_zkcli(NULL),m_zksrvob(NULL){}; 42 | ~DynamicTokenChk(); 43 | int init(const std::string &zkUrl, const std::string &keyPath); 44 | int init(ZKClient *zkcli, const std::string &keyPath); 45 | std::string &keypath(){return m_keypath;}; 46 | ZKClient *zkcli(){return m_zkcli;}; 47 | int setWatcher(const std::string &path, KeyWatcher *kw); 48 | private: 49 | static int childChangeCallBack(void *ctx, const children_map& children); 50 | 51 | // int updateKey(); 52 | ZKClient *m_zkcli; 53 | ZKServerListOb *m_zksrvob; 54 | std::map m_wchs; 55 | std::string m_keypath; 56 | }; 57 | 58 | 59 | }; 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /common/include/ef_crypt.h: -------------------------------------------------------------------------------- 1 | #ifndef __MYENCRYPT_H__ 2 | #define __MYENCRYPT_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | namespace ef { 9 | 10 | #define AUTHERR_OK 0 11 | #define AUTHERR_TIMEOUT -1 12 | #define AUTHERR_DECFAIL -2 13 | #define AUTHERR_ENCFAIL -3 14 | 15 | int setTokenTimeout(int timeout); 16 | 17 | int setTextTimeout(int timeout); 18 | 19 | int generateToken(const std::map &minfo, std::string &token); 20 | 21 | int checkToken(const std::string &token, std::map &minfo); 22 | 23 | int encrypt(const std::string &text, std::string &enctext); 24 | 25 | int decrypt(const std::string &enctext, std::string &text); 26 | 27 | }; 28 | #endif 29 | -------------------------------------------------------------------------------- /common/include/log_init.h: -------------------------------------------------------------------------------- 1 | #ifndef __LOG_INIT_H__ 2 | #define __LOG_INIT_H__ 3 | 4 | #include 5 | #include "base/ef_log.h" 6 | 7 | namespace gim{ 8 | #define TYPE_FILE_APPENDER "FileAppender" 9 | #define TYPE_CONSOLE_APPENDER "ConsoleAppender" 10 | #define TYPE_NULL_APPENDER "NullAppender" 11 | 12 | int logInit(const std::string& config); 13 | } 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /common/include/redis_msg_q.h: -------------------------------------------------------------------------------- 1 | #ifndef _JSON_REDIS_DB_H_ 2 | #define _JSON_REDIS_DB_H_ 3 | #include 4 | #include 5 | #include 6 | #include "base/ef_btype.h" 7 | #include "message.pb.h" 8 | 9 | using namespace std; 10 | using namespace ef; 11 | 12 | namespace gim{ 13 | class RedisCG; 14 | class RedisMQ{ 15 | public: 16 | enum{ 17 | DEFAULT_EXPIRE = 60 * 60 * 24 * 7, 18 | DEFAULT_CAPACITY = 100 19 | }; 20 | 21 | RedisMQ(const Json::Value& config); 22 | ~RedisMQ(); 23 | int incrId(const string& hash, const string& key, int64& id); 24 | int getId(const string& hash, const string& key, int64& id); 25 | int64 size(const string& hash, const string& key); 26 | int del(const string& hash, const string& key, int64 start, int64 end); 27 | 28 | int add(const string& hash, const string& key, const Message& m); 29 | int get(const string& hash, const string& key, int64 start, int len, vector& msgs); 30 | private: 31 | RedisCG *m_cg; 32 | int m_expiry; 33 | int m_capacity; 34 | }; 35 | } 36 | #endif //_JSON_REDIS_DB_H_ 37 | -------------------------------------------------------------------------------- /common/include/server_status.h: -------------------------------------------------------------------------------- 1 | #ifndef __SERVER_STATUS_H__ 2 | #define __SERVER_STATUS_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | namespace Json{ 10 | class Value; 11 | }; 12 | 13 | 14 | namespace gim{ 15 | struct PortConfig{ 16 | int Port; 17 | int MaxType; 18 | int MinType; 19 | std::map others; 20 | }; 21 | 22 | struct ServerStatus{ 23 | int ID; 24 | std::vector IPs; 25 | std::vector Ports; 26 | std::map Properties; 27 | 28 | int parseFromJson(const Json::Value& v); 29 | int serializeToJson(Json::Value& v); 30 | }; 31 | 32 | } 33 | 34 | #endif/*__SERVER_STATUS_H__*/ 35 | -------------------------------------------------------------------------------- /common/include/sess_cache.h: -------------------------------------------------------------------------------- 1 | #ifndef __SESS_CACHE_H__ 2 | #define __SESS_CACHE_H__ 3 | 4 | #include 5 | #include 6 | #include "session.pb.h" 7 | #include "json/json.h" 8 | #include "base/ef_btype.h" 9 | #include "base/ef_thread.h" 10 | #include "base/ef_loader.h" 11 | 12 | namespace gim{ 13 | 14 | class ZKClient; 15 | 16 | class SessCache{ 17 | public: 18 | virtual ~SessCache(){} 19 | virtual int getSession(const std::string &key, std::vector &m) = 0; 20 | virtual int setSession(const Sess &s) = 0; 21 | virtual int delSession(const Sess &s) = 0; 22 | }; 23 | 24 | class SsChFactory{ 25 | public: 26 | virtual ~SsChFactory(){} 27 | 28 | virtual SessCache* getSessCache() = 0; 29 | 30 | static int initSsChFactory(ZKClient* zkc, const Json::Value& conf); 31 | static int freeSsChFactory(); 32 | static SsChFactory* getSsChFactory(); 33 | private: 34 | static SsChFactory* g_fact; 35 | }; 36 | 37 | 38 | 39 | } 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /common/include/token_checker.h: -------------------------------------------------------------------------------- 1 | #ifndef __TOKENCHECKER_H__ 2 | #define __TOKENCHECKER_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include "base/ef_btype.h" 10 | 11 | namespace gim { 12 | 13 | #define AUTH_OK 0 14 | 15 | //encode error 16 | #define AUTH_ENCFAIL -1 17 | 18 | //check error 19 | #define AUTH_SIZE_ERROR -100 20 | #define AUTH_CHECKSUM_ERROR -101 21 | #define AUTH_TIMEOUT -102 22 | #define AUTH_INVALID_INDEX -103 23 | #define AUTH_DECFAIL -104 24 | 25 | struct keyinfo { 26 | std::string str; 27 | time_t startTime; 28 | keyinfo():startTime(0){}; 29 | }; 30 | 31 | struct keylist{ 32 | std::map mkey; 33 | volatile time_t upTime; 34 | ef::uint16 idx; 35 | keylist():upTime(0),idx(0){}; 36 | }; 37 | 38 | class TokenChecker { 39 | public: 40 | TokenChecker():m_timeout(7200),m_keyTimeout(600){ 41 | pthread_mutex_init(&m_lock, NULL); 42 | }; 43 | int setKey(ef::uint16 idx, const std::string &key); 44 | std::string getCurKey(void); 45 | int setTimeout(int timeout); 46 | int generateToken(const std::map &minfo, std::string &token); 47 | int checkToken(const std::string &token, std::map &minfo); 48 | keylist &getkeylist(){return m_keylist;}; 49 | 50 | protected: 51 | keylist m_keylist; 52 | int m_timeout; 53 | int m_keyTimeout; 54 | pthread_mutex_t m_lock; 55 | keylist *getMyKey(); 56 | }; 57 | 58 | const char *errPhase(int errcode); 59 | }; 60 | #endif 61 | -------------------------------------------------------------------------------- /common/include/tree.h: -------------------------------------------------------------------------------- 1 | #ifndef _ZK_TREE_H_ 2 | #define _ZK_TREE_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | namespace ef 12 | { 13 | class Tree; 14 | class TreeNode 15 | { 16 | friend class Tree; 17 | private: 18 | TreeNode(const std::string& s); 19 | ~TreeNode(); 20 | public: 21 | const std::string& getData() const; 22 | void setData(const std::string& d); 23 | const std::string& getId(); 24 | private: 25 | TreeNode* addChild(const std::string& s); 26 | void deleteChild(const std::string& s); 27 | void removeChild(const std::string& s); 28 | TreeNode* getNode(const std::string& id); 29 | 30 | void getChildren(std::vector& v); 31 | private: 32 | std::string data_; 33 | std::string id_; 34 | std::vector nodes_; 35 | }; 36 | 37 | class Tree 38 | { 39 | public: 40 | Tree(); 41 | ~Tree(); 42 | void getChildren(const std::string& path, std::vector& v); 43 | 44 | void delNode(const std::string& path); 45 | 46 | TreeNode* getNode(const std::string& path); 47 | TreeNode* addNode(const std::string& path); 48 | public: 49 | static std::string genPathName(const std::string& path, const std::string& id); 50 | static int split(const std::string& str, std::vector& ret_); 51 | void getTreeJson(const std::string& path, Json::Value& v); 52 | 53 | static bool isSubpath(const std::string& path, const std::string& subpath, unsigned int depth); 54 | static std::string getParentPath(const std::string& path); 55 | private: 56 | TreeNode root; 57 | }; 58 | } 59 | 60 | #endif //_ZK_TREE_H_ 61 | -------------------------------------------------------------------------------- /common/include/zk_config.h: -------------------------------------------------------------------------------- 1 | #ifndef _ZK_CFG_H_ 2 | #define _ZK_CFG_H_ 3 | #include 4 | #include 5 | 6 | namespace gim 7 | { 8 | class ZKClient; 9 | class ZKDataWatcher; 10 | 11 | typedef void (*CfgChangedCallback)(void* ctx, int version, const Json::Value& notify); 12 | class ZKConfig 13 | { 14 | public: 15 | ZKConfig(ZKClient* c, const std::string& typepath, const std::string& srvid = ""); 16 | ~ZKConfig(); 17 | int init(CfgChangedCallback cb, void* context); 18 | 19 | int getCfg(Json::Value& v); 20 | 21 | private: 22 | static int watchCallBack(void* ctx, int version, const std::string& data); 23 | int readNodeCfg(const std::string& path, Json::Value& v); 24 | private: 25 | ZKClient* m_cli; 26 | 27 | std::string m_typepath; 28 | 29 | std::string m_srvid; 30 | std::string m_srvpath; 31 | 32 | ZKDataWatcher* m_wt; 33 | ZKDataWatcher* m_ws; 34 | 35 | CfgChangedCallback m_cb; 36 | void* m_ctx; 37 | }; 38 | } 39 | 40 | #endif // !_ZK_CFG_H_ 41 | -------------------------------------------------------------------------------- /common/include/zk_server_node.h: -------------------------------------------------------------------------------- 1 | #ifndef _ZK_SERVER_NODE_H_ 2 | #define _ZK_SERVER_NODE_H_ 3 | #include 4 | #include 5 | 6 | namespace gim 7 | { 8 | class ZKClient; 9 | 10 | class ZKServerNode 11 | { 12 | public: 13 | ZKServerNode(ZKClient* c, const std::string& typepath, const std::string& id); 14 | ~ZKServerNode(); 15 | int init(const std::string& data); 16 | int setData(const std::string& data); 17 | int setJson(const Json::Value& v); 18 | private: 19 | ZKClient* m_cli; 20 | std::string m_path; 21 | }; 22 | } 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /common/include/zk_server_ob.h: -------------------------------------------------------------------------------- 1 | #ifndef _ZK_SERVER_OBSERVER_H_ 2 | #define _ZK_SERVER_OBSERVER_H_ 3 | #include 4 | #include 5 | #include 6 | #include "zk_watcher.h" 7 | 8 | namespace gim 9 | { 10 | class ZKClient; 11 | typedef std::map children_map; 12 | 13 | typedef int (*ServerListObCallBack)(void* ctx, const children_map& children); 14 | 15 | class ZKServerListOb 16 | :public ZKWatcher 17 | { 18 | public: 19 | ZKServerListOb(ZKClient* c, const std::string& typepath); 20 | ~ZKServerListOb(); 21 | int init(bool loaddata, ServerListObCallBack cb, void* context); 22 | 23 | void getServerList(children_map& m); 24 | virtual int onChildrenChange(const std::vector& children); 25 | private: 26 | void getServerList(const std::vector& v, children_map& m); 27 | private: 28 | std::string m_path; 29 | 30 | ServerListObCallBack m_cb; 31 | void* m_ctx; 32 | bool m_loaddata; 33 | }; 34 | } 35 | #endif // !_ZK_SERVER_OBSERVER_H_ 36 | -------------------------------------------------------------------------------- /common/src/cache_group.cpp: -------------------------------------------------------------------------------- 1 | #include "cache_group.h" 2 | #include "def_cache_group.h" 3 | 4 | namespace gim{ 5 | 6 | 7 | CacheGroupFactory* CacheGroupFactory::g_fac = NULL; 8 | 9 | int CacheGroupFactory::init(const Json::Value& config){ 10 | g_fac = create(config); 11 | return 0; 12 | } 13 | 14 | void CacheGroupFactory::free(){ 15 | delete g_fac; 16 | g_fac = NULL; 17 | } 18 | 19 | CacheGroupFactory* CacheGroupFactory::get(){ 20 | return g_fac; 21 | } 22 | 23 | CacheGroupFactory* CacheGroupFactory::create(const Json::Value& config){ 24 | const Json::Value& type = config["Type"]; 25 | 26 | if(!type.isString()){ 27 | return NULL; 28 | } 29 | 30 | const Json::Value& conf = config["Config"]; 31 | 32 | if(type.asString() == "DefCacheGroup"){ 33 | CacheGroupFactory* c = new DefCacheGroupFactory(conf); 34 | 35 | return c; 36 | } 37 | 38 | return NULL; 39 | } 40 | 41 | 42 | } 43 | -------------------------------------------------------------------------------- /common/src/def_sess_cache.h: -------------------------------------------------------------------------------- 1 | #ifndef __DEF_SESS_CACHE_H__ 2 | #define __DEF_SESS_CACHE_H__ 3 | 4 | #include "sess_cache.h" 5 | #include "redis_cg.h" 6 | #include "base/ef_loader.h" 7 | #include "base/ef_tsd_ptr.h" 8 | 9 | namespace gim{ 10 | 11 | 12 | class DefSessCache: public SessCache{ 13 | public: 14 | enum{ 15 | SESSION_DEFAULT_EXPIRE_SECOND = 500, 16 | }; 17 | 18 | DefSessCache(ef::DataSrc* config):m_dbg(NULL), 19 | m_expire(SESSION_DEFAULT_EXPIRE_SECOND), 20 | m_loader(config){ 21 | m_loader.loadData(); 22 | init(m_loader.getData()); 23 | }; 24 | virtual ~DefSessCache(); 25 | virtual int getSession(const string &key, vector &m); 26 | virtual int setSession(const Sess &s); 27 | virtual int delSession(const Sess &s); 28 | private: 29 | int init(const Json::Value& config); 30 | 31 | DBHandle getHandle(const string &key); 32 | 33 | RedisCG* m_dbg; 34 | int m_expire; 35 | ef::Loader m_loader; 36 | }; 37 | 38 | class ZKClient; 39 | class ZKDataWatcher; 40 | 41 | class DefSsChFactory: public SsChFactory{ 42 | public: 43 | DefSsChFactory() 44 | :m_w(NULL){ 45 | } 46 | ~DefSsChFactory(); 47 | int init(ZKClient* c, const std::string& wpath); 48 | virtual SessCache* getSessCache(); 49 | static int watchCallBack(void* ctx, int version, const std::string& data); 50 | private: 51 | int setConfig(const std::string& data); 52 | private: 53 | ZKDataWatcher* m_w; 54 | ef::DataSrc m_conf; 55 | TSDPtr m_cache; 56 | }; 57 | 58 | }//end of namespace 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /common/src/def_sess_cache_group.h: -------------------------------------------------------------------------------- 1 | #ifndef __DEF_SESS_CACHE_CLUSTER_H__ 2 | #define __DEF_SESS_CACHE_CLUSTER_H__ 3 | 4 | #include "sess_cache.h" 5 | #include "def_sess_cache.h" 6 | #include "redis_cg.h" 7 | #include "base/ef_loader.h" 8 | #include 9 | #include 10 | #include "base/ef_tsd_ptr.h" 11 | 12 | namespace gim{ 13 | class DefSessCacheGroup:public SessCache{ 14 | public: 15 | DefSessCacheGroup(){}; 16 | virtual ~DefSessCacheGroup(); 17 | virtual int getSession(const std::string &key, std::vector &m); 18 | virtual int setSession(const Sess &s); 19 | virtual int delSession(const Sess &s); 20 | void addSessCache(SessCache* s); 21 | private: 22 | std::vector m_caches; 23 | }; 24 | 25 | class DefSsChGrpFactory:public SsChFactory{ 26 | public: 27 | DefSsChGrpFactory(){}; 28 | virtual ~DefSsChGrpFactory(); 29 | 30 | int init(ZKClient* c, std::vector& configpaths); 31 | virtual SessCache* getSessCache(); 32 | private: 33 | std::vector m_fact; 34 | TSDPtr m_cache; 35 | }; 36 | }//end of namespace 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /common/src/gtype.h: -------------------------------------------------------------------------------- 1 | #ifndef __GTYPE_H__ 2 | #define __GTYPE_H__ 3 | 4 | #include 5 | 6 | namespace gim { 7 | 8 | typedef void (*LogCb)(const std::string &cmd); 9 | 10 | }; 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /common/src/msg_db.cpp: -------------------------------------------------------------------------------- 1 | #include "msg_db.h" 2 | #include "redis_msg_db.h" 3 | 4 | namespace gim{ 5 | 6 | MsgDBFactory* MsgDBFactory::g_fct = NULL; 7 | 8 | int MsgDBFactory::init(const Json::Value &conf){ 9 | g_fct = create(conf); 10 | return 0; 11 | } 12 | 13 | void MsgDBFactory::free(){ 14 | delete g_fct; 15 | g_fct = NULL; 16 | } 17 | 18 | MsgDBFactory* MsgDBFactory::get(){ 19 | return g_fct; 20 | } 21 | 22 | MsgDBFactory* MsgDBFactory::create(const Json::Value& config){ 23 | const Json::Value& type = config["Type"]; 24 | 25 | if(!type.isString()){ 26 | return NULL; 27 | } 28 | 29 | const Json::Value& conf = config["Config"]; 30 | 31 | if(type.asString() == "RedisMI"){ 32 | RedisMIFactory* c = new RedisMIFactory(conf); 33 | return c; 34 | } 35 | 36 | return NULL; 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /common/src/redis_cg.h: -------------------------------------------------------------------------------- 1 | #ifndef __REDIS_CG_H__ 2 | #define __REDIS_CG_H__ 3 | 4 | #include "redis_client.h" 5 | #include "json/json.h" 6 | 7 | namespace gim { 8 | 9 | class RedisCG { 10 | public: 11 | RedisCG(const Json::Value &config); 12 | ~RedisCG(); 13 | int clear(); 14 | DBHandle getHndl(const string &key); 15 | void setCmdLog(LogCb cb); 16 | private: 17 | int init(const Json::Value &config); 18 | 19 | vector m_dbs; 20 | Json::Value m_cfg; 21 | LogCb m_cmdLog; 22 | }; 23 | 24 | }; 25 | #endif 26 | -------------------------------------------------------------------------------- /common/src/sess_cache.cpp: -------------------------------------------------------------------------------- 1 | #include "sess_cache.h" 2 | #include "zk_client.h" 3 | #include "def_sess_cache.h" 4 | #include "def_sess_cache_group.h" 5 | 6 | 7 | namespace gim{ 8 | SsChFactory* SsChFactory::g_fact = NULL; 9 | 10 | int SsChFactory::initSsChFactory(ZKClient* zkc, const Json::Value& conf){ 11 | const Json::Value& typev = conf["Type"]; 12 | if(!typev.isString()){ 13 | return -1; 14 | } 15 | 16 | const std::string& type = typev.asString(); 17 | if(type == "DefSessCache"){ 18 | int ret = 0; 19 | const Json::Value& pathv = conf["Path"]; 20 | if (!pathv.isString()){ 21 | return -3; 22 | } 23 | 24 | DefSsChFactory* f = new DefSsChFactory(); 25 | ret = f->init(zkc, pathv.asString()); 26 | if(0 != ret){ 27 | delete f; 28 | return ret; 29 | } 30 | g_fact = f; 31 | return 0; 32 | } 33 | else if(type == "DefSessCacheGroup"){ 34 | int ret = 0; 35 | const Json::Value& paths = conf["Paths"]; 36 | if(!paths.isArray()) 37 | return -3; 38 | 39 | std::vector vec; 40 | for (unsigned int n = 0; n < paths.size();++n){ 41 | vec.push_back(paths[n].asString()); 42 | } 43 | DefSsChGrpFactory* f = new DefSsChGrpFactory(); 44 | ret = f->init(zkc, vec); 45 | if(0 != ret){ 46 | delete f; 47 | return ret; 48 | } 49 | g_fact = f; 50 | return 0; 51 | } 52 | 53 | return -2; 54 | } 55 | 56 | int SsChFactory::freeSsChFactory(){ 57 | delete g_fact; 58 | return 0; 59 | } 60 | 61 | SsChFactory* SsChFactory::getSsChFactory(){ 62 | return g_fact; 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /common/src/zk_server_node.cpp: -------------------------------------------------------------------------------- 1 | #include "zk_server_node.h" 2 | #include "zk_client.h" 3 | 4 | namespace gim 5 | { 6 | ZKServerNode::ZKServerNode(ZKClient* c, const std::string& typepath, const std::string& id) 7 | :m_cli(c) 8 | { 9 | m_path = zkPath(typepath, id); 10 | } 11 | 12 | ZKServerNode::~ZKServerNode() 13 | { 14 | if (m_cli) 15 | { 16 | m_cli->deleteNode(m_path); 17 | } 18 | } 19 | int ZKServerNode::init(const std::string& data) 20 | { 21 | if (m_cli) 22 | return m_cli->createEphemeralNode(m_path, data); 23 | 24 | return -1; 25 | } 26 | 27 | int ZKServerNode::setData(const std::string& data) 28 | { 29 | int ret = -1; 30 | if (m_cli){ 31 | ret = m_cli->setNodeData(m_path, data); 32 | if(ZNONODE == ret){ 33 | ret = m_cli->createEphemeralNode(m_path, data); 34 | } 35 | } 36 | return ret; 37 | } 38 | int ZKServerNode::setJson(const Json::Value& v) 39 | { 40 | try 41 | { 42 | Json::FastWriter w; 43 | return setData(w.write(v)); 44 | } 45 | catch (const std::exception &ex) 46 | { 47 | if(m_cli) 48 | m_cli->writeLog(std::string("ZKServerNode::setJson() exception:") + ex.what()); 49 | return -1; 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /common/src/zk_server_ob.cpp: -------------------------------------------------------------------------------- 1 | #include "zk_server_ob.h" 2 | #include "zk_client.h" 3 | 4 | namespace gim 5 | { 6 | ZKServerListOb::ZKServerListOb(ZKClient* c, const std::string& typepath) 7 | :ZKWatcher(typepath), m_path(typepath), 8 | m_cb(NULL), m_ctx(NULL), m_loaddata(false) 9 | { 10 | addToClient(c); 11 | } 12 | ZKServerListOb::~ZKServerListOb() 13 | { 14 | } 15 | int ZKServerListOb::init(bool loaddata, ServerListObCallBack cb, void* context) 16 | { 17 | m_loaddata = loaddata; 18 | m_cb = cb; 19 | m_ctx = context; 20 | addWatch(ZKWatcher::CHILD_EVENT); 21 | return 0; 22 | } 23 | void ZKServerListOb::getServerList(children_map& m){ 24 | s_vector v; 25 | getClient()->getChildren(m_path, v); 26 | getServerList(v, m); 27 | } 28 | void ZKServerListOb::getServerList(const std::vector& v, children_map& m){ 29 | for (unsigned int n = 0; ngetNodeData(p, data); 36 | try{ 37 | Json::Value jv; 38 | Json::Reader r; 39 | if (r.parse(data, jv)){ 40 | m[v[n]] = jv; 41 | continue; 42 | } 43 | } 44 | catch (const std::exception &ex){ 45 | if(m_cli) 46 | m_cli->writeLog(std::string("ZKServerListOb::getServerList exception:") + ex.what()); 47 | } 48 | } 49 | m[v[n]] = Json::Value(); 50 | } 51 | } 52 | int ZKServerListOb::onChildrenChange(const std::vector& children){ 53 | children_map m; 54 | getServerList(children, m); 55 | if(m_cb) 56 | return m_cb(m_ctx, m); 57 | return 0; 58 | } 59 | 60 | }//end of ef -------------------------------------------------------------------------------- /common/test/logtest/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.6) 2 | project(elog_test) 3 | 4 | set(CMAKE_VERBOSE_MAKEFILE ON) 5 | set(SRC_LIST elog_test.cpp) 6 | include_directories(../../include ../../../efnfw) 7 | add_definitions("-Wall -g") 8 | link_directories(../../lib) 9 | link_directories(../../../efnfw/lib) 10 | link_libraries(libcommon.a json libefnfw.a pthread config) 11 | add_executable(elog_test ${SRC_LIST}) 12 | -------------------------------------------------------------------------------- /common/test/logtest/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | mkdir -p log 3 | rm -fr CMakeCache.txt 4 | cmake . 5 | make clean;make 6 | rm -fr CMakeCache.txt 7 | rm -fr CMakeFiles 8 | rm -fr Makefile 9 | rm -fr cmake_install.cmake 10 | -------------------------------------------------------------------------------- /common/test/logtest/elog_test.cpp: -------------------------------------------------------------------------------- 1 | #include "log_init.h" 2 | #include 3 | #include 4 | #include 5 | #include "base/ef_base64.h" 6 | #include "base/ef_utility.h" 7 | 8 | bool g_run = true; 9 | 10 | using namespace ef; 11 | using namespace gim; 12 | 13 | int loop = 0; 14 | 15 | void* test_thread(void* p){ 16 | 17 | std::string s; 18 | 19 | int cnt = 0; 20 | while(cnt < loop){ 21 | char c = 'c'; 22 | int i = 10; 23 | long l = 1234567890; 24 | long long ll = 1234567890123456; 25 | double d = 12345.6789; 26 | void* p = &d; 27 | 28 | logDebug("file_logger") << "file test line:" 29 | << "c:" << c << ",i:" << i << ",l:" << l << ",ll:" << ll 30 | << ",d" << d << ",p:" << p << ",s:" << s; 31 | ++cnt; 32 | 33 | } 34 | return 0; 35 | } 36 | 37 | int main(int argc, const char** argv){ 38 | if(argc < 3){ 39 | std::cout << "elog_test \n"; 40 | exit(0); 41 | } 42 | int pcnt = atoi(argv[1]); 43 | loop = atoi(argv[2]); 44 | logInit("log.conf"); 45 | 46 | std::string s1 = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 47 | 48 | 49 | logDebug("console_logger") << "sleep_ms(" << 3600 << ")"; 50 | sleep_ms(3600); 51 | logDebug("console_logger") << "sleep_ms(" << 3600 << ") finish"; 52 | 53 | pthread_t* pids = new pthread_t[pcnt]; 54 | for(int i = 0; i < pcnt; ++i){ 55 | pthread_create(&pids[i], NULL, test_thread, NULL); 56 | } 57 | 58 | g_run = false; 59 | for(int j = 0; j < pcnt; ++j){ 60 | void* rt; 61 | pthread_join(pids[j], &rt); 62 | } 63 | 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /common/test/logtest/log.conf: -------------------------------------------------------------------------------- 1 | { 2 | "Appenders":[ { "Name":"console_appender", "Type":"ConsoleAppender"}, 3 | { "Name":"file_appender", "Type":"FileAppender", "Path":"log/all", "ScheduSpan":"MINUTE", "ImmediatelyFlush":"false"} 4 | ], 5 | "Logs": [{ "Name":"console_logger", "Level":"ALL", "Appender":"console_appender" }, 6 | { "Name":"file_logger", "Level":"ALL", "Appender":"file_appender"}, 7 | { "Name":"file_logger1", "Level":"ALL", "Appender":"file_appender"} 8 | ], 9 | "DefaultLog":{"Level":"ALL", "Appender":"console_appender"} 10 | } 11 | -------------------------------------------------------------------------------- /common/test/msgboxtest/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.6) 2 | set(CMAKE_VERBOSE_MAKEFILE ON) 3 | project(mbtest) 4 | add_definitions("-Wall -g") 5 | set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin) 6 | # add dependency 7 | include_directories(/usr/local/include ../../../efnfw ../../include ../../src) 8 | link_directories(/usr/local/lib ../../../efnfw/lib ../../lib) 9 | link_libraries(libcommon.a libefnfw.a protobuf hiredis json) 10 | set(SRC_LISTS msgboxtest.cpp) 11 | add_executable(mbtest ${SRC_LISTS}) 12 | -------------------------------------------------------------------------------- /common/test/msgboxtest/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | mkdir -p bin 3 | cmake . 4 | make clean;make 5 | -------------------------------------------------------------------------------- /common/test/msgboxtest/msgboxtest.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "redis_msg_db.h" 4 | #include "redis_cg.h" 5 | 6 | using namespace std; 7 | using namespace gim; 8 | 9 | void print(const string &data) 10 | { 11 | cout << data << endl; 12 | } 13 | 14 | int main(int argc, char *argv[]) 15 | { 16 | if (argc < 2) { 17 | std::cout << "usage:" << argv[0] << " " << std::endl; 18 | return -1; 19 | } 20 | 21 | Json::Value cfg; 22 | Json::Reader reader; 23 | 24 | ifstream is(argv[1]); 25 | string strcfg; 26 | while (is) { 27 | string tmp; 28 | getline(is, tmp); 29 | strcfg += tmp; 30 | } 31 | 32 | if (!reader.parse(strcfg, cfg)) { 33 | std::cout << "parse config file fail" << std::endl; 34 | return -1; 35 | } 36 | 37 | // cg.setCmdLog(print); 38 | 39 | RedisMI mb(cfg["RedisMICfg"]); 40 | Message msg; 41 | msg.set_to("234242145"); 42 | msg.set_from("8987945734"); 43 | msg.set_type(0); 44 | msg.set_sn("dalijlid9da3w40j09u"); 45 | msg.set_data("hello"); 46 | for (int i = 0; i < 10; i++) { 47 | msg.set_id(i + 100); 48 | mb.addMsg("msgbox_344", msg); 49 | } 50 | vector vmsg; 51 | int ret = mb.getMsgs("msgbox_344", 0, 100, vmsg); 52 | vector::iterator it; 53 | for (it = vmsg.begin(); it != vmsg.end(); it++) { 54 | cout << it->id() << endl; 55 | } 56 | cout << endl; 57 | vmsg.clear(); 58 | mb.delMsgs("msgbox_344", 107, 3); 59 | mb.getMsgs("msgbox_344", 0, 100, vmsg); 60 | for (it = vmsg.begin(); it != vmsg.end(); it++) { 61 | cout << it->id() << endl; 62 | } 63 | cout << endl; 64 | mb.delMsgs("msgbox_344", 108, 3); 65 | vmsg.clear(); 66 | mb.getMsgs("msgbox_344", 0, 100, vmsg); 67 | for (it = vmsg.begin(); it != vmsg.end(); it++) { 68 | cout << it->id() << endl; 69 | } 70 | cout << endl; 71 | mb.clear("msgbox_344"); 72 | return 0; 73 | } 74 | 75 | -------------------------------------------------------------------------------- /common/test/msgboxtest/test.conf: -------------------------------------------------------------------------------- 1 | { 2 | "RedisMICfg": 3 | { 4 | "ExpiryTime":0, 5 | "Capacity":0, 6 | "CacheCluster": 7 | { 8 | "NodeCfgs": 9 | [ 10 | {"ipaddr":"127.0.0.1", "port":7381, "passwd":""} 11 | ] 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /common/test/redisclitest/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.6) 2 | 3 | set(CMAKE_VERBOSE_MAKEFILE ON) 4 | project(redisclitest) 5 | 6 | add_definitions("-Wall -g") 7 | 8 | set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin) 9 | 10 | # add dependency 11 | include_directories(/usr/local/include ../../../efnfw ../../src) 12 | link_directories(/usr/local/lib ../../../efnfw/lib ../../lib) 13 | link_libraries(libcommon.a libefnfw.a hiredis) 14 | #file(GLOB_RECURSE SRC_LISTS ./*.cpp) 15 | set(SRC_LISTS redis_cli_test.cpp) 16 | add_executable(redisclitest ${SRC_LISTS}) 17 | -------------------------------------------------------------------------------- /common/test/redisclitest/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | mkdir -p bin 3 | cmake . 4 | make clean;make 5 | -------------------------------------------------------------------------------- /common/test/sess_cache_test/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.6) 2 | PROJECT(sess_cache_test) 3 | set(CMAKE_VERBOSE_MAKEFILE ON) 4 | add_definitions("-Wall -g") 5 | 6 | set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin) 7 | 8 | include_directories(/usr/local/include ../../include ../../../efnfw) 9 | 10 | 11 | 12 | link_directories(/usr/local/lib) 13 | link_directories(../../../efnfw/lib ../../lib) 14 | 15 | link_libraries(libcommon.a libefnfw.a zookeeper_mt.a jsoncpp protobuf hiredis pthread) 16 | 17 | file(GLOB SRC_LISTS *.cpp) 18 | MESSAGE( ${SRC_LISTS} ) 19 | 20 | add_executable(sess_cache_test ${SRC_LISTS}) -------------------------------------------------------------------------------- /common/test/sess_cache_test/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | mkdir -p log 3 | rm -fr CMakeCache.txt 4 | cmake . 5 | make clean;make 6 | rm -fr CMakeCache.txt 7 | rm -fr CMakeFiles 8 | rm -fr Makefile 9 | rm -fr cmake_install.cmake 10 | -------------------------------------------------------------------------------- /common/test/sess_cache_test/log.conf: -------------------------------------------------------------------------------- 1 | { 2 | "Appenders":[ { "Name":"console_appender", "Type":"ConsoleAppender"}, 3 | { "Name":"file_appender", "Type":"FileAppender", "Path":"log/all", "ScheduSpan":"MINUTE", "ImmediatelyFlush":"false"} 4 | ], 5 | "Logs": [{ "Name":"console_logger", "Level":"ALL", "Appender":"console_appender" }, 6 | { "Name":"file_logger", "Level":"ALL", "Appender":"file_appender"}, 7 | { "Name":"file_logger1", "Level":"ALL", "Appender":"file_appender"} 8 | ], 9 | "DefaultLog":{"Level":"ALL", "Appender":"console_appender"} 10 | } 11 | -------------------------------------------------------------------------------- /common/test/sesstest/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.6) 2 | project(elog_test) 3 | 4 | set(CMAKE_VERBOSE_MAKEFILE ON) 5 | set(SRC_LIST sess_test.cpp) 6 | include_directories(../../include ../../../efnfw) 7 | add_definitions("-Wall -g") 8 | link_directories(../../lib /usr/local/lib) 9 | link_directories(../../../efnfw/lib) 10 | link_libraries(libcommon.a json libefnfw.a hiredis protobuf pthread) 11 | add_executable(elog_test ${SRC_LIST}) 12 | -------------------------------------------------------------------------------- /common/test/sesstest/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | mkdir -p log 3 | rm -fr CMakeCache.txt 4 | cmake . 5 | make clean;make 6 | rm -fr CMakeCache.txt 7 | rm -fr CMakeFiles 8 | rm -fr Makefile 9 | rm -fr cmake_install.cmake 10 | -------------------------------------------------------------------------------- /common/test/sesstest/sess.conf: -------------------------------------------------------------------------------- 1 | { 2 | "SessionCache": 3 | { 4 | "Type":"DefaultType", 5 | "Config":{ 6 | "Expire":30, 7 | "DBGroup": 8 | { 9 | "NodeCfgs":[{"ipaddr":"127.0.0.1", "port":6379, "passwd":""} ] 10 | } 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /common/test/sesstest/sess_test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "sess_cache.h" 5 | 6 | using namespace std; 7 | using namespace gim; 8 | 9 | int printSessList(const vector& sslst){ 10 | for(size_t i = 0; i < sslst.size(); ++i){ 11 | std::cout << "{" << sslst[i].DebugString() << "}, "; 12 | } 13 | 14 | return 0; 15 | } 16 | 17 | int main(int argc, const char** argv){ 18 | if(argc < 4){ 19 | std::cout << "sess_test [sessid] []\n"; 20 | return -1; 21 | } 22 | 23 | 24 | const char* config = argv[1]; 25 | 26 | Json::Reader reader; 27 | Json::Value root; 28 | 29 | fstream f(config); 30 | 31 | if(!reader.parse(f, root, false)){ 32 | std::cout << "parse config file fail!\n"; 33 | return -1; 34 | } 35 | 36 | Json::Value& svconf = root["SessionCache"]; 37 | SsChFactory* sf = SsChFactory::create(svconf); 38 | 39 | SessCache* c = sf->newSessCache(); 40 | 41 | string cmd = argv[2]; 42 | string cid = argv[3]; 43 | 44 | int ret = 0; 45 | if(cmd == "get"){ 46 | vector vs; 47 | ret = c->getSession(cid, vs); 48 | std::cout << "get result:" << ret << std::endl; 49 | printSessList(vs); 50 | }else if(cmd == "set"){ 51 | Sess s; 52 | string sessid = argv[4]; 53 | s.set_cid(cid); 54 | s.set_sessid(sessid); 55 | ret = c->setSession(s); 56 | std::cout << "set result:" << ret << std::endl; 57 | }else if(cmd == "del"){ 58 | Sess s; 59 | string sessid = argv[4]; 60 | s.set_cid(cid); 61 | s.set_sessid(sessid); 62 | ret = c->delSession(s); 63 | std::cout << "del result:" << ret << std::endl; 64 | } 65 | 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /common/test/svlsttest/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.6) 2 | project(elog_test) 3 | 4 | set(CMAKE_VERBOSE_MAKEFILE ON) 5 | set(SRC_LIST svlst_test.cpp) 6 | include_directories(../../include ../../../efnfw) 7 | add_definitions("-Wall -g") 8 | link_directories(../../lib) 9 | link_directories(../../../efnfw/lib) 10 | link_libraries(libcommon.a json libefnfw.a pthread) 11 | add_executable(elog_test ${SRC_LIST}) 12 | -------------------------------------------------------------------------------- /common/test/svlsttest/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | mkdir -p log 3 | rm -fr CMakeCache.txt 4 | cmake . 5 | make clean;make 6 | rm -fr CMakeCache.txt 7 | rm -fr CMakeFiles 8 | rm -fr Makefile 9 | rm -fr cmake_install.cmake 10 | -------------------------------------------------------------------------------- /common/test/svlsttest/svlist.conf: -------------------------------------------------------------------------------- 1 | { 2 | "ServerList": 3 | { 4 | "Type":"DefaultType", 5 | "Config": 6 | [ 7 | { 8 | "Type":"0", 9 | "ServerList": 10 | [ 11 | {"ID":"1001", "IP": "192.168.1.6", "ClientListenPort": 3000, "ServerListPort": 13000}, 12 | {"ID":"1002", "IP": "192.168.1.7", "ClientListenPort": 3000, "ServerListPort": 13000} 13 | ] 14 | } 15 | ] 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /common/test/svlsttest/svlst_test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "svlist_cache.h" 4 | 5 | using namespace std; 6 | using namespace gim; 7 | 8 | int printServerList(const vector& svlst){ 9 | for(size_t i = 0; i < svlst.size(); ++i){ 10 | std::cout << "{id:" << svlst[i].id 11 | << ", type:" << svlst[i].type 12 | << ", config:" << svlst[i].v.toStyledString() 13 | << "}, \n"; 14 | } 15 | 16 | return 0; 17 | } 18 | 19 | class TestSvLstListener:public SvLstListener{ 20 | public: 21 | virtual int onListChange(int type, vector &servlist){ 22 | printServerList(servlist); 23 | return 0; 24 | } 25 | virtual int onDisableListChange(int type, vector &servlist){ 26 | return 0; 27 | } 28 | }; 29 | 30 | 31 | int main(int argc, const char** argv){ 32 | if(argc < 2){ 33 | std::cout << "svlist_cache_test \n"; 34 | return -1; 35 | } 36 | 37 | 38 | const char* config = argv[1]; 39 | 40 | Json::Reader reader; 41 | Json::Value root; 42 | 43 | fstream f(config); 44 | 45 | if(!reader.parse(f, root, false)){ 46 | std::cout << "parse config file fail!\n"; 47 | return -1; 48 | } 49 | 50 | Json::Value& svconf = root["ServerList"]; 51 | SvLstChFactory* sf = SvLstChFactory::create(svconf); 52 | 53 | SvLstCache* c = sf->newSvLstCache(); 54 | 55 | int ret = 0; 56 | 57 | vector servlist; 58 | TestSvLstListener l; 59 | 60 | c->setServerListListener(&l); 61 | 62 | ret = c->watchServerList(0); 63 | 64 | 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /common/test/zk_client_test/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.6) 2 | PROJECT(zk_client_test) 3 | #set(CMAKE_VERBOSE_MAKEFILE ON) 4 | add_definitions("-Wall -g") 5 | 6 | set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin) 7 | 8 | include_directories(/usr/local/include ../../include ../../../efnfw) 9 | 10 | 11 | 12 | link_directories(/usr/local/lib) 13 | link_directories(../../../efnfw/lib ../../lib) 14 | 15 | link_libraries(pthread libcommon.a zookeeper_mt jsoncpp libefnfw.a) 16 | 17 | file(GLOB SRC_LISTS test.cpp) 18 | MESSAGE( ${SRC_LISTS} ) 19 | 20 | add_executable(zk_client_test ${SRC_LISTS}) 21 | -------------------------------------------------------------------------------- /common/test/zk_client_test/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | #cd 'dirname $0` 3 | 4 | #cp -fr ../src/proto src/ 5 | 6 | #protoc src/proto/connect_server.proto -I=src/proto/ --cpp_out=src/proto 7 | #mv src/proto/connect_server.pb.cc src/proto/connect_server.pb.cpp 8 | 9 | 10 | echo "clean build files" 11 | rm -fr CMakeCache.txt 12 | rm -fr CMakeFiles 13 | 14 | cmake . 15 | make clean;make 16 | -------------------------------------------------------------------------------- /common/test/zk_client_test/log.conf: -------------------------------------------------------------------------------- 1 | { 2 | "Appenders":[ { "Name":"console_appender", "Type":"ConsoleAppender"}, 3 | { "Name":"file_appender", "Type":"FileAppender", "Path":"log/all", "ScheduSpan":"MINUTE", "ImmediatelyFlush":"false"} 4 | ], 5 | "Logs": [{ "Name":"console_logger", "Level":"ALL", "Appender":"console_appender" }, 6 | { "Name":"file_logger", "Level":"ALL", "Appender":"file_appender"}, 7 | { "Name":"file_logger1", "Level":"ALL", "Appender":"file_appender"} 8 | ], 9 | "DefaultLog":{"Level":"ALL", "Appender":"console_appender"} 10 | } 11 | -------------------------------------------------------------------------------- /common/test/zk_client_test/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "zk_client.h" 3 | #include 4 | #include 5 | #include 6 | #include "zk_server_ob.h" 7 | #include 8 | #include "zk_config.h" 9 | #include "zk_server_node.h" 10 | 11 | 12 | using namespace gim; 13 | 14 | int obCallBack(void* ctx, const children_map& children){ 15 | std::cout << "obcallback\n"; 16 | try{ 17 | Json::FastWriter w; 18 | for (children_map::const_iterator it = children.begin(); it != children.end(); ++it) 19 | { 20 | std::cout << it->first << ", json:" << w.write(it->second) << std::endl; 21 | } 22 | } 23 | catch (const std::exception &ex){ 24 | std::cout << ex.what() << "\n"; 25 | } 26 | 27 | return 0; 28 | } 29 | 30 | void cfgCallback(void* ctx, int version, const Json::Value& notify){ 31 | std::cout << "cfgCallback\n"; 32 | try{ 33 | Json::FastWriter w; 34 | std::cout << w.write(notify) << std::endl; 35 | } 36 | catch (const std::exception &ex){ 37 | std::cout << ex.what() << "\n"; 38 | } 39 | } 40 | 41 | void logfn(void* context, const std::string& l){ 42 | std::cout << l << std::endl; 43 | } 44 | int main(int argc, const char** argv) 45 | { 46 | std::string url = "127.0.0.1:12306,127.0.0.1:22306,127.0.0.1:32306,127.0.0.1:42306,127.0.0.1:52306,127.0.0.1:62306"; 47 | std::string path = "/dps"; 48 | ZKClient c; 49 | c.setLogFn(NULL, logfn); 50 | c.init(url); 51 | 52 | ZKServerListOb ob(&c, "/dps/ob"); 53 | ob.init(true, obCallBack, NULL); 54 | 55 | 56 | 57 | ZKConfig cfg(&c, "/dps/cfg", ""); 58 | cfg.init(cfgCallback, NULL); 59 | 60 | ZKServerNode node(&c, "/dps/node", "1"); 61 | node.init("ohe \nhe"); 62 | 63 | int count = 5; 64 | while (count > 0) 65 | { 66 | #ifndef _WIN32 67 | sleep(1); 68 | #endif // !_WIN32 69 | } 70 | 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /connect_server/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project(ConnectServer) 2 | set(CMAKE_VERBOSE_MAKEFILE ON) 3 | add_definitions("-Wall -pg -g") 4 | include_directories(${PROJECT_SOURCE_DIR}/../common/include ${PROJECT_SOURCE_DIR}/../efnfw) 5 | link_directories(/usr/local/lib ../common/lib/ ../efnfw/lib) 6 | link_libraries(libcommon.a libefnfw.a hiredis zookeeper_mt protobuf jsoncpp pthread) 7 | file(GLOB_RECURSE SRC_LIST src/*.cpp) 8 | add_executable(bin/ConnectServer ${SRC_LIST}) 9 | -------------------------------------------------------------------------------- /connect_server/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | cd ../efnfw; 4 | sh build.sh 5 | cd - 6 | 7 | cd ../common; 8 | sh build.sh 9 | cd - 10 | 11 | cp -fr ../proto src/ 12 | 13 | protoc src/proto/connect_server.proto --cpp_out=src/proto -I=src/proto 14 | 15 | mv src/proto/connect_server.pb.cc src/proto/connect_server.pb.cpp 16 | 17 | mkdir -p bin 18 | 19 | rm -fr CMakeCache.txt 20 | rm -fr CMakeFiles 21 | 22 | 23 | cmake . 24 | make clean;make 25 | 26 | rm -fr CMakeCache.txt 27 | rm -fr CMakeFiles 28 | -------------------------------------------------------------------------------- /connect_server/etc/elog.conf: -------------------------------------------------------------------------------- 1 | { 2 | "Appenders":[ { "Name":"console_appender", "Type":"ConsoleAppender"}, 3 | { "Name":"file_appender", "Type":"FileAppender", "Path":"/opt/log/connect_server/all", "ScheduSpan":"HOUR", "ImmediatelyFlush":"false"} 4 | ], 5 | "Logs": [{ "Name":"console_logger", "Level":"ALL", "Appender":"console_appender" }, 6 | { "Name":"file_logger", "Level":"ALL", "Appender":"file_appender"}, 7 | { "Name":"file_logger1", "Level":"ALL", "Appender":"file_appender"} 8 | ], 9 | "DefaultLog":{"Level":"ALL", "Appender":"console_appender"} 10 | } 11 | -------------------------------------------------------------------------------- /connect_server/etc/settings.conf: -------------------------------------------------------------------------------- 1 | { 2 | "Daemon":1, 3 | "Type":0, 4 | 5 | "ThreadCount":2, 6 | 7 | "NetLogName":"ConnectNetLog", 8 | 9 | "CliConfigs": 10 | [{"Enc":0, "AliveMs":300000, 11 | "MinType":-1000, "MaxType":1, 12 | "ListenPort": 3000, "MaxReqQueSize": 0, 13 | "MaxPackCntPerMin": 0, "StartThreadIdx": 0, 14 | "ThreadCnt": 1}, 15 | 16 | {"Enc":0, "AliveMs":300000, 17 | "MinType":1, "MaxType":1000, 18 | "ListenPort": 3000, "MaxReqQueSize": 10000, 19 | "MaxPackCntPerMin": 0, "StartThreadIdx": 1, 20 | "ThreadCnt": 1} 21 | ], 22 | 23 | 24 | "SessCacheConfig": 25 | { 26 | "Type":"DefSessCache", 27 | "Config":{ 28 | "Expire":30, 29 | "DBGroup": 30 | { 31 | "NodeCfgs":[{"ipaddr":"127.0.0.1", "port":6379, "passwd":""} ] 32 | } 33 | } 34 | } 35 | 36 | 37 | } 38 | -------------------------------------------------------------------------------- /connect_server/src/client_config.h: -------------------------------------------------------------------------------- 1 | #ifndef __CLI_CONFIG_H__ 2 | #define __CLI_CONFIG_H__ 3 | 4 | 5 | namespace gim{ 6 | 7 | class CliConfig{ 8 | public: 9 | int Enc; 10 | int AliveMs; 11 | int MinType; 12 | int MaxType; 13 | int ListenPort; 14 | int MaxReqQueSize; 15 | int MaxPackCntPerMin; 16 | int StartThreadIdx; 17 | int ThreadCnt; 18 | }; 19 | 20 | } 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /connect_server/src/common.cpp: -------------------------------------------------------------------------------- 1 | #include "common.h" 2 | #include "net/ef_connection.h" 3 | #include "proto/msg_head.h" 4 | #include "base/ef_utility.h" 5 | #include "base/ef_base64.h" 6 | #include 7 | #include 8 | 9 | namespace gim{ 10 | 11 | int32 checkLen(ef::Connection& c){ 12 | head h; 13 | if(c.bufLen() < (int32)sizeof(h)){ 14 | return 0; 15 | } 16 | c.peekBuf((char*)&h, sizeof(h)); 17 | h.magic = htonl(h.magic); 18 | h.len = htonl(h.len); 19 | if(h.len < (int32)sizeof(h) 20 | || h.len > 1024 * 1024){ 21 | return -1; 22 | } 23 | if(h.len <= c.bufLen()){ 24 | return h.len; 25 | } 26 | return 0; 27 | } 28 | 29 | 30 | int32 constructPacket(const head& h, 31 | const std::string& body, std::string& respbuf){ 32 | head rh; 33 | respbuf.reserve(sizeof(h) + body.size()); 34 | rh.cmd = htonl(h.cmd); 35 | rh.magic = h.magic; 36 | rh.len = htonl(sizeof(h) + body.size()); 37 | respbuf.append((char*)&rh, sizeof(rh)); 38 | respbuf.append(body); 39 | return 0; 40 | } 41 | 42 | int32 decorationName(int32 svid, int32 conid, 43 | const std::string& n, std::string& dn){ 44 | 45 | std::string buf; 46 | buf.resize(8); 47 | 48 | char* p = (char*)buf.data(); 49 | 50 | *(int32*)p = svid; 51 | *(int32*)(p + sizeof(int32)) = conid; 52 | 53 | dn = base64Encode(buf) + n; 54 | return 0; 55 | } 56 | 57 | int32 getDecorationInfo(const std::string& dn, int32& svid, 58 | int32& conid, std::string& n){ 59 | 60 | std::string b64buf = dn.substr(0, 12); 61 | 62 | std::string buf = base64Decode(b64buf); 63 | 64 | char* p = (char*)buf.data(); 65 | svid = *(int32*)p; 66 | conid = *(int32*)(p + sizeof(int32)); 67 | 68 | n = dn.substr(12); 69 | 70 | return 0; 71 | } 72 | 73 | 74 | }; 75 | -------------------------------------------------------------------------------- /connect_server/src/common.h: -------------------------------------------------------------------------------- 1 | #ifndef __COMMON_H__ 2 | #define __COMMON_H__ 3 | 4 | #include 5 | #include "base/ef_btype.h" 6 | #include "base/ef_log.h" 7 | #include "base/ef_utility.h" 8 | 9 | namespace ef{ 10 | class EventLoop; 11 | class Connection; 12 | }; 13 | 14 | namespace gim{ 15 | 16 | using namespace ef; 17 | 18 | class head; 19 | 20 | int32 checkLen(ef::Connection& c); 21 | 22 | int32 constructPacket(const head& h, 23 | const std::string& body, std::string& pack); 24 | 25 | enum{ 26 | EVENT_LOOP_PART_CNT = 4, 27 | EVENT_LOOP_ID_MASK = 1000000, 28 | FRE_CHECK_SPAN = 60, 29 | }; 30 | 31 | int32 decorationName(int32 svid, int32 conid, 32 | const std::string& n, std::string& dn); 33 | 34 | int32 getDecorationInfo(const std::string& dn, int32& svid, 35 | int32& conid, std::string& n); 36 | 37 | 38 | inline int32 serverEventLoopCount(int32 maxevlp){ 39 | int32 svevcnt = maxevlp >= EVENT_LOOP_PART_CNT ? 40 | maxevlp / EVENT_LOOP_PART_CNT : 1; 41 | return svevcnt; 42 | } 43 | 44 | inline int32 getConnectionId(int32 evlpid, int32 conid){ 45 | return (evlpid * EVENT_LOOP_ID_MASK) 46 | + (conid % EVENT_LOOP_ID_MASK); 47 | } 48 | 49 | inline int32 getEventLoopId(int32 conid){ 50 | return conid / EVENT_LOOP_ID_MASK; 51 | } 52 | 53 | 54 | 55 | 56 | }; 57 | 58 | #endif/*COMMON_H*/ 59 | -------------------------------------------------------------------------------- /connect_server/src/proto/common_logic_service.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | message CommonServiceRequest{ 4 | optional string cid = 1; 5 | optional int32 type = 2; 6 | optional string parms = 3; 7 | } 8 | 9 | message CommonServiceResponse{ 10 | optional string cid = 1; 11 | optional int32 type = 2; 12 | optional string status = 3; 13 | optional string code = 4; 14 | } -------------------------------------------------------------------------------- /connect_server/src/proto/connect_server.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | import "pair.proto"; 4 | 5 | message LoginRequest{ 6 | required string id = 1;//id of user or logic server 7 | optional int32 type = 2;//type 0:client, > 0:logic server 8 | optional string version = 3;//client version 9 | optional string token = 4; //use for check 10 | repeated Pair kvs = 5; 11 | } 12 | 13 | 14 | message LoginResponse{ 15 | required int32 status = 1;//if 0 success, else fail 16 | optional string sessid = 2;//the session id, 17 | optional int64 server_time = 3;//server timestamp 18 | } 19 | 20 | message Address{ 21 | optional string ip = 1; 22 | optional int32 port = 2; 23 | } 24 | 25 | message RedirectResponse{ 26 | required int32 status = 1;//if 0 success, else fail 27 | repeated Address addrs = 2; 28 | } 29 | 30 | 31 | message ServiceRequest{ 32 | optional string from_sessid = 1;//from sessid 33 | optional string to_sessid = 2;//to sessid 34 | required int32 from_type = 3;// 100:push 200:peer 35 | required int32 to_type = 4;// 100:push 200:peer 36 | required string sn = 5;//each req has an unique sn 37 | optional bytes payload = 6;// payload 38 | } 39 | 40 | message ServiceResponse{ 41 | optional string from_sessid = 1;//from sessid 42 | optional string to_sessid = 2; //to sessid 43 | required int32 from_type = 3; 44 | required int32 to_type = 4; 45 | required string sn = 5;//each req has an unique sn 46 | required int32 status = 6;// 0 success 47 | optional bytes payload = 7; 48 | } 49 | 50 | message KickCliRequest{ 51 | optional string sessid = 1; 52 | optional string id = 2; 53 | optional int32 type = 3; 54 | optional bytes reason = 4; 55 | } 56 | -------------------------------------------------------------------------------- /connect_server/src/proto/message.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | 4 | message Message{ 5 | optional string to = 1; 6 | optional int64 id = 2;//mssage id, did not set this field when send message 7 | optional int64 time = 3;//message send time 8 | optional string from = 4;//who send 9 | optional int32 type = 5; 10 | optional string sn = 6; 11 | optional bytes data = 7;//message data 12 | } 13 | 14 | 15 | -------------------------------------------------------------------------------- /connect_server/src/proto/msg_head.h: -------------------------------------------------------------------------------- 1 | #ifndef __MSG_HEAD_H__ 2 | #define __MSG_HEAD_H__ 3 | 4 | #include "base/ef_btype.h" 5 | 6 | namespace gim{ 7 | 8 | using namespace ef; 9 | 10 | enum{ 11 | KEEPALIVE_REQ = 0, 12 | KEEPALIVE_RESP = KEEPALIVE_REQ + 1, 13 | LOGIN_REQ = 100, 14 | LOGIN_RESP = LOGIN_REQ + 1, 15 | SERVICE_REQ = 200, 16 | SERVICE_RESP = SERVICE_REQ + 1, 17 | KICK_CLIENT = 400, 18 | REDIRECT_RESP = 1001, 19 | SET_TIME_RESP = 1101, 20 | MAGIC_NUMBER = 0x20141228, 21 | }; 22 | 23 | struct head{ 24 | int32 magic; 25 | int32 len;//include head 26 | //0: keepalive, 1: keepalive resp 27 | //100: login, 101: login resp 28 | //200: service, 201: service resp 29 | //300: regist service 301: regist service resp 30 | //400: kick client 31 | //1001: redericet 32 | int32 cmd; 33 | }; 34 | 35 | 36 | }; 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /connect_server/src/proto/pair.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | message Pair{ 4 | required string key = 1; 5 | optional bytes value = 2; 6 | } 7 | -------------------------------------------------------------------------------- /connect_server/src/proto/peer_server.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | import "message.proto"; 4 | 5 | message GetPeerMessageRequest{ 6 | optional string cid = 1;//to who 7 | optional int64 start_msgid = 2;//mssage id 8 | optional int64 count = 3;//message count 9 | } 10 | 11 | message GetPeerMessageResponse{ 12 | optional int64 last_msgid = 1; 13 | repeated Message msgs = 2; 14 | } 15 | 16 | message SendPeerMessageRequest{ 17 | required Message msg = 1; 18 | } 19 | 20 | //use for muti-point online 21 | message SendPeerMessageResponse{ 22 | required Message msg = 1; 23 | } 24 | 25 | message RecvPeerMessageResponse{ 26 | required Message msg = 1; 27 | } 28 | 29 | message PushMessageRequest{ 30 | required string sn = 1; 31 | required Message msg = 2; 32 | } 33 | 34 | message PushMessageResponse{ 35 | required string sn = 1; 36 | required int32 status = 2; 37 | } 38 | 39 | 40 | message PeerPacket{ 41 | required int32 cmd = 1;//110: send_req, 42 | //111: send_resp, 112: get_req, 43 | //113: get_resp, 115:recv_resp 44 | optional GetPeerMessageRequest get_peer_msg_req = 2; 45 | optional GetPeerMessageResponse get_peer_msg_resp = 3; 46 | optional SendPeerMessageRequest send_peer_msg_req = 4; 47 | optional SendPeerMessageResponse send_peer_msg_resp = 5; 48 | optional RecvPeerMessageResponse recv_peer_msg_resp = 6; 49 | } 50 | -------------------------------------------------------------------------------- /connect_server/src/proto/position_server.proto: -------------------------------------------------------------------------------- 1 | { 2 | "command":"set_position_req", 3 | "position":{ 4 | "line_way":"xiamen_to_zhangzhou", 5 | "uid":"user1", 6 | "location":{ 7 | "long":125.23, 8 | "lat":26.24 9 | } 10 | } 11 | } 12 | 13 | { 14 | "command":"set_position_resp", 15 | "line_way":"xiamen_to_zhangzhou", 16 | "uid":"user1", 17 | "status":0, 18 | "err_str":"SUCCESS" 19 | } 20 | 21 | 22 | { 23 | "command":"get_position_req", 24 | "line_way":"xiamen_to_zhangzhou", 25 | "uid":"user1", 26 | 27 | } 28 | 29 | { 30 | "command":"get_position_resp", 31 | "status":0, 32 | "err_str":"SUCCESS" 33 | "position":{ 34 | "line_way":"xiamen_to_zhangzhou", 35 | "uid":"user1", 36 | "location":{ 37 | "long":125.23, 38 | "lat":26.24 39 | { 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /connect_server/src/proto/session.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | import "pair.proto"; 4 | 5 | message Sess{ 6 | optional string id = 1; 7 | optional int32 type = 2; 8 | optional int64 lasttime = 3; 9 | optional string sessid = 4; 10 | optional int32 consvid = 5; 11 | optional string version = 6; 12 | repeated Pair kvs = 7; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /connect_server/src/server_config.h: -------------------------------------------------------------------------------- 1 | #ifndef __SERVER_CONFIG_H__ 2 | #define __SERVER_CONFIG_H__ 3 | 4 | 5 | namespace gim{ 6 | 7 | class ServerConfig{ 8 | public: 9 | int deamon; 10 | int consvid; 11 | int thread_cnt; 12 | }; 13 | 14 | } 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /connect_server/src/type_map.h: -------------------------------------------------------------------------------- 1 | #ifndef __TYPE_MAP_H__ 2 | #define __TYPE_MAP_H__ 3 | 4 | #include 5 | #include "base/ef_thread.h" 6 | 7 | namespace gim{ 8 | 9 | class ServiceRequest; 10 | class CliCon; 11 | 12 | 13 | class SvList{ 14 | public: 15 | SvList(); 16 | ~SvList(); 17 | 18 | int addServer(CliCon* c); 19 | int delServer(CliCon* c); 20 | 21 | 22 | int transRequest(const ServiceRequest& req); 23 | private: 24 | ef::MUTEX m_cs; 25 | std::vector m_svs; 26 | int m_cnt; 27 | }; 28 | 29 | class TypeMap{ 30 | public: 31 | 32 | static int init(int maxtype = 1024); 33 | static int addServer(int type, CliCon* c); 34 | static int delServer(int type, CliCon* c); 35 | 36 | static int transRequest(const ServiceRequest& req); 37 | private: 38 | static std::vector m_svlsts; 39 | }; 40 | 41 | }; 42 | 43 | #endif/*__TYPE_MAP_H__*/ 44 | -------------------------------------------------------------------------------- /connect_server/test_client/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.6) 2 | 3 | set(CMAKE_VERBOSE_MAKEFILE ON) 4 | project(ConnectTest) 5 | 6 | add_definitions(-g) 7 | 8 | set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin) 9 | 10 | # add dependency 11 | 12 | include_directories(/usr/local/include ../../efnfw ../../common/include) 13 | link_directories(/usr/local/lib ../../efnfw/lib ../../common/lib) 14 | 15 | link_libraries(libcommon.a libefnfw.a protobuf pthread) 16 | 17 | 18 | file(GLOB_RECURSE SRC_LISTS src/*.cpp) 19 | add_executable(TestClient ${SRC_LISTS}) 20 | 21 | -------------------------------------------------------------------------------- /connect_server/test_client/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | cd `dirname $0` 4 | 5 | cp -fr ../../proto src/ 6 | 7 | protoc src/proto/pair.proto -I=src/proto/ --cpp_out=src/proto 8 | mv src/proto/pair.pb.cc src/proto/pair.pb.cpp 9 | 10 | protoc src/proto/message.proto -I=src/proto/ --cpp_out=src/proto 11 | mv src/proto/message.pb.cc src/proto/message.pb.cpp 12 | 13 | protoc src/proto/connect_server.proto -I=src/proto/ --cpp_out=src/proto 14 | mv src/proto/connect_server.pb.cc src/proto/connect_server.pb.cpp 15 | 16 | protoc src/proto/peer_server.proto -I=src/proto/ --cpp_out=src/proto 17 | mv src/proto/peer_server.pb.cc src/proto/peer_server.pb.cpp 18 | 19 | echo "clean build files" 20 | rm -fr CMakeCache.txt 21 | rm -fr CMakeFiles 22 | 23 | mkdir -p bin 24 | 25 | cmake . 26 | make clean;make 27 | 28 | rm -fr CMakeCache.txt 29 | rm -fr CMakeFiles 30 | -------------------------------------------------------------------------------- /connect_server/test_client/src/proto/common_logic_service.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | message CommonServiceRequest{ 4 | optional string cid = 1; 5 | optional int32 type = 2; 6 | optional string parms = 3; 7 | } 8 | 9 | message CommonServiceResponse{ 10 | optional string cid = 1; 11 | optional int32 type = 2; 12 | optional string status = 3; 13 | optional string code = 4; 14 | } -------------------------------------------------------------------------------- /connect_server/test_client/src/proto/connect_server.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | import "pair.proto"; 4 | 5 | message LoginRequest{ 6 | required string id = 1;//id of user or logic server 7 | optional int32 type = 2;//type 0:client, > 0:logic server 8 | optional string version = 3;//client version 9 | optional string token = 4; //use for check 10 | repeated Pair kvs = 5; 11 | } 12 | 13 | 14 | message LoginResponse{ 15 | required int32 status = 1;//if 0 success, else fail 16 | optional string sessid = 2;//the session id, 17 | optional int64 server_time = 3;//server timestamp 18 | } 19 | 20 | message Address{ 21 | optional string ip = 1; 22 | optional int32 port = 2; 23 | } 24 | 25 | message RedirectResponse{ 26 | required int32 status = 1;//if 0 success, else fail 27 | repeated Address addrs = 2; 28 | } 29 | 30 | 31 | message ServiceRequest{ 32 | optional string from_sessid = 1;//from sessid 33 | optional string to_sessid = 2;//to sessid 34 | required int32 from_type = 3;// 100:push 200:peer 35 | required int32 to_type = 4;// 100:push 200:peer 36 | required string sn = 5;//each req has an unique sn 37 | optional bytes payload = 6;// payload 38 | } 39 | 40 | message ServiceResponse{ 41 | optional string from_sessid = 1;//from sessid 42 | optional string to_sessid = 2; //to sessid 43 | required int32 from_type = 3; 44 | required int32 to_type = 4; 45 | required string sn = 5;//each req has an unique sn 46 | required int32 status = 6;// 0 success 47 | optional bytes payload = 7; 48 | } 49 | 50 | message KickCliRequest{ 51 | optional string sessid = 1; 52 | optional string id = 2; 53 | optional int32 type = 3; 54 | optional bytes reason = 4; 55 | } 56 | -------------------------------------------------------------------------------- /connect_server/test_client/src/proto/err_no.h: -------------------------------------------------------------------------------- 1 | #ifndef __ERR_NO_H__ 2 | #define __ERR_NO_H__ 3 | 4 | 5 | namespace gim{ 6 | 7 | 8 | enum{ 9 | STATUS_OK = 0, 10 | INPUT_FORMAT_ERROR = -1, 11 | CREATE_SESSION_FAIL = -10, 12 | GET_KEY_FAIL = -11, 13 | CHECK_TOKEN_FAIL = -12, 14 | NO_ENC_UNSUPPORT = -13, 15 | INVALID_SESSION_ID = -20, 16 | SESSION_TIMEOUT = -21, 17 | NO_SERVICE = -30, 18 | SERVICE_TOO_BUSY = -33, 19 | CALL_TIMEOUT = -34, 20 | INVALID_SN = -40, 21 | SN_TIMEOUT = -41, 22 | DECRYPT_FAIL = -50, 23 | INVALID_TYPE = -60, 24 | SERVICE_TYPE_ERROR = -70, 25 | ASSESS_FORBIDDEN = -80, 26 | INNER_ERROR = -100, 27 | }; 28 | 29 | inline const char* getErrStr(int e){ 30 | switch(e){ 31 | case STATUS_OK: 32 | return "STATUS_OK"; 33 | case INPUT_FORMAT_ERROR: 34 | return "INPUT_FORMAT_ERROR"; 35 | case CREATE_SESSION_FAIL: 36 | return "CREATE_SESSION_FAIL"; 37 | case GET_KEY_FAIL: 38 | return "GET_USER_KEY_FAIL"; 39 | case CHECK_TOKEN_FAIL: 40 | return "CHECK_TOKEN_FAIL"; 41 | case NO_ENC_UNSUPPORT: 42 | return "NO_ENC_UNSUPPORT"; 43 | case INVALID_SESSION_ID: 44 | return "INVALID_SESSION_ID"; 45 | case NO_SERVICE: 46 | return "NO_SERVICE"; 47 | case SESSION_TIMEOUT: 48 | return "SESSION_TIMEOUT"; 49 | case SERVICE_TOO_BUSY: 50 | return "SERVICE_TOO_BUSY"; 51 | case INVALID_SN: 52 | return "INVALID_SN"; 53 | case SN_TIMEOUT: 54 | return "SN_TIMEOUT"; 55 | case INVALID_TYPE: 56 | return "INVALID_TYPE"; 57 | case DECRYPT_FAIL: 58 | return "DECRYPT_FAIL"; 59 | case SERVICE_TYPE_ERROR: 60 | return "SERVICE_TYPE_ERROR"; 61 | case ASSESS_FORBIDDEN: 62 | return "ASSESS_FORBIDDEN"; 63 | case CALL_TIMEOUT: 64 | return "CALL_TIMEOUT"; 65 | case INNER_ERROR: 66 | return "INNER_ERROR"; 67 | } 68 | return ""; 69 | } 70 | 71 | }; 72 | 73 | 74 | #endif 75 | -------------------------------------------------------------------------------- /connect_server/test_client/src/proto/message.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | 4 | message Message{ 5 | optional string to = 1; 6 | optional int64 id = 2;//mssage id, did not set this field when send message 7 | optional int64 time = 3;//message send time 8 | optional string from = 4;//who send 9 | optional int32 type = 5; 10 | optional string sn = 6; 11 | optional bytes data = 7;//message data 12 | } 13 | 14 | 15 | -------------------------------------------------------------------------------- /connect_server/test_client/src/proto/msg_head.h: -------------------------------------------------------------------------------- 1 | #ifndef __MSG_HEAD_H__ 2 | #define __MSG_HEAD_H__ 3 | 4 | #include "base/ef_btype.h" 5 | 6 | namespace gim{ 7 | 8 | using namespace ef; 9 | 10 | enum{ 11 | KEEPALIVE_REQ = 0, 12 | KEEPALIVE_RESP = KEEPALIVE_REQ + 1, 13 | LOGIN_REQ = 100, 14 | LOGIN_RESP = LOGIN_REQ + 1, 15 | SERVICE_REQ = 200, 16 | SERVICE_RESP = SERVICE_REQ + 1, 17 | KICK_CLIENT = 400, 18 | REDIRECT_RESP = 1001, 19 | SET_TIME_RESP = 1101, 20 | MAGIC_NUMBER = 0x20141228, 21 | }; 22 | 23 | struct head{ 24 | int32 magic; 25 | int32 len;//include head 26 | //0: keepalive, 1: keepalive resp 27 | //100: login, 101: login resp 28 | //200: service, 201: service resp 29 | //300: regist service 301: regist service resp 30 | //400: kick client 31 | //1001: redericet 32 | int32 cmd; 33 | }; 34 | 35 | 36 | }; 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /connect_server/test_client/src/proto/pair.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | message Pair{ 4 | required string key = 1; 5 | optional bytes value = 2; 6 | } 7 | -------------------------------------------------------------------------------- /connect_server/test_client/src/proto/peer_server.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | import "message.proto"; 4 | 5 | message GetPeerMessageRequest{ 6 | optional string cid = 1;//to who 7 | optional int64 start_msgid = 2;//mssage id 8 | optional int64 count = 3;//message count 9 | } 10 | 11 | message GetPeerMessageResponse{ 12 | optional int64 last_msgid = 1; 13 | repeated Message msgs = 2; 14 | } 15 | 16 | message SendPeerMessageRequest{ 17 | required Message msg = 1; 18 | } 19 | 20 | //use for muti-point online 21 | message SendPeerMessageResponse{ 22 | required Message msg = 1; 23 | } 24 | 25 | message RecvPeerMessageResponse{ 26 | required Message msg = 1; 27 | } 28 | 29 | message PushMessageRequest{ 30 | required string sn = 1; 31 | required Message msg = 2; 32 | } 33 | 34 | message PushMessageResponse{ 35 | required string sn = 1; 36 | required int32 status = 2; 37 | } 38 | 39 | 40 | message PeerPacket{ 41 | required int32 cmd = 1;//110: send_req, 42 | //111: send_resp, 112: get_req, 43 | //113: get_resp, 115:recv_resp 44 | optional GetPeerMessageRequest get_peer_msg_req = 2; 45 | optional GetPeerMessageResponse get_peer_msg_resp = 3; 46 | optional SendPeerMessageRequest send_peer_msg_req = 4; 47 | optional SendPeerMessageResponse send_peer_msg_resp = 5; 48 | optional RecvPeerMessageResponse recv_peer_msg_resp = 6; 49 | } 50 | -------------------------------------------------------------------------------- /connect_server/test_client/src/proto/position_server.proto: -------------------------------------------------------------------------------- 1 | { 2 | "command":"set_position_req", 3 | "position":{ 4 | "line_id":"xiamen_to_zhangzhou", 5 | "uid":"user1", 6 | "location":{ 7 | "long":125.23, 8 | "lat":26.24 9 | } 10 | } 11 | } 12 | 13 | { 14 | "command":"set_position_resp", 15 | "line_id":"xiamen_to_zhangzhou", 16 | "uid":"user1", 17 | "status":0, 18 | "err_str":"SUCCESS" 19 | } 20 | 21 | 22 | { 23 | "command":"get_position_req", 24 | "line_id":"xiamen_to_zhangzhou", 25 | "uid":"user1", 26 | 27 | } 28 | 29 | { 30 | "command":"get_position_resp", 31 | "status":0, 32 | "err_str":"SUCCESS" 33 | "position":{ 34 | "line_id":"xiamen_to_zhangzhou", 35 | "uid":"user1", 36 | "location":{ 37 | "long":125.23, 38 | "lat":26.24 39 | { 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /connect_server/test_client/src/proto/session.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | import "pair.proto"; 4 | 5 | message Sess{ 6 | optional string id = 1; 7 | optional int32 type = 2; 8 | optional int64 lasttime = 3; 9 | optional string sessid = 4; 10 | optional int32 consvid = 5; 11 | optional string version = 6; 12 | repeated Pair kvs = 7; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /connect_server/test_server/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.6) 2 | 3 | set(CMAKE_VERBOSE_MAKEFILE ON) 4 | project(ConnectTest) 5 | 6 | add_definitions(-g) 7 | 8 | set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin) 9 | 10 | # add dependency 11 | 12 | include_directories(/usr/local/include ../../efnfw) 13 | link_directories(/usr/local/lib ../../efnfw/lib) 14 | 15 | link_libraries(libefnfw.a protobuf pthread) 16 | 17 | 18 | file(GLOB_RECURSE SRC_LISTS src/*.cpp) 19 | add_executable(TestServer ${SRC_LISTS}) 20 | 21 | -------------------------------------------------------------------------------- /connect_server/test_server/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | cd `dirname $0` 4 | 5 | cp -fr ../src/proto src/ 6 | 7 | protoc src/proto/pair.proto -I=src/proto/ --cpp_out=src/proto 8 | mv src/proto/pair.pb.cc src/proto/pair.pb.cpp 9 | 10 | protoc src/proto/connect_server.proto -I=src/proto/ --cpp_out=src/proto 11 | mv src/proto/connect_server.pb.cc src/proto/connect_server.pb.cpp 12 | 13 | mkdir -p bin 14 | 15 | echo "clean build files" 16 | rm -fr CMakeCache.txt 17 | rm -fr CMakeFiles 18 | 19 | cmake . 20 | make clean;make 21 | 22 | rm -fr CMakeCache.txt 23 | rm -fr CMakeFiles 24 | -------------------------------------------------------------------------------- /connect_server/test_server/src/proto/common_logic_service.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | message CommonServiceRequest{ 4 | optional string cid = 1; 5 | optional int32 type = 2; 6 | optional string parms = 3; 7 | } 8 | 9 | message CommonServiceResponse{ 10 | optional string cid = 1; 11 | optional int32 type = 2; 12 | optional string status = 3; 13 | optional string code = 4; 14 | } -------------------------------------------------------------------------------- /connect_server/test_server/src/proto/connect_server.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | import "pair.proto"; 4 | 5 | message LoginRequest{ 6 | required string id = 1;//id of user or logic server 7 | optional int32 type = 2;//type 0:client, > 0:logic server 8 | optional string version = 3;//client version 9 | optional string token = 4; //use for check 10 | repeated Pair kvs = 5; 11 | } 12 | 13 | 14 | message LoginResponse{ 15 | required int32 status = 1;//if 0 success, else fail 16 | optional string sessid = 2;//the session id, 17 | optional int64 server_time = 3;//server timestamp 18 | } 19 | 20 | message Address{ 21 | optional string ip = 1; 22 | optional int32 port = 2; 23 | } 24 | 25 | message RedirectResponse{ 26 | required int32 status = 1;//if 0 success, else fail 27 | repeated Address addrs = 2; 28 | } 29 | 30 | 31 | message ServiceRequest{ 32 | optional string from_sessid = 1;//from sessid 33 | optional string to_sessid = 2;//to sessid 34 | required int32 from_type = 3;// 100:push 200:peer 35 | required int32 to_type = 4;// 100:push 200:peer 36 | required string sn = 5;//each req has an unique sn 37 | optional bytes payload = 6;// payload 38 | } 39 | 40 | message ServiceResponse{ 41 | optional string from_sessid = 1;//from sessid 42 | optional string to_sessid = 2; //to sessid 43 | required int32 from_type = 3; 44 | required int32 to_type = 4; 45 | required string sn = 5;//each req has an unique sn 46 | required int32 status = 6;// 0 success 47 | optional bytes payload = 7; 48 | } 49 | 50 | message KickCliRequest{ 51 | optional string sessid = 1; 52 | optional string id = 2; 53 | optional int32 type = 3; 54 | optional bytes reason = 4; 55 | } 56 | -------------------------------------------------------------------------------- /connect_server/test_server/src/proto/err_no.h: -------------------------------------------------------------------------------- 1 | #ifndef __ERR_NO_H__ 2 | #define __ERR_NO_H__ 3 | 4 | 5 | namespace gim{ 6 | 7 | 8 | enum{ 9 | STATUS_OK = 0, 10 | INPUT_FORMAT_ERROR = -1, 11 | CREATE_SESSION_FAIL = -10, 12 | GET_KEY_FAIL = -11, 13 | CHECK_TOKEN_FAIL = -12, 14 | NO_ENC_UNSUPPORT = -13, 15 | INVALID_SESSION_ID = -20, 16 | SESSION_TIMEOUT = -21, 17 | NO_SERVICE = -30, 18 | SERVICE_TOO_BUSY = -33, 19 | CALL_TIMEOUT = -34, 20 | INVALID_SN = -40, 21 | SN_TIMEOUT = -41, 22 | DECRYPT_FAIL = -50, 23 | INVALID_TYPE = -60, 24 | SERVICE_TYPE_ERROR = -70, 25 | ASSESS_FORBIDDEN = -80, 26 | INNER_ERROR = -100, 27 | }; 28 | 29 | inline const char* getErrStr(int e){ 30 | switch(e){ 31 | case STATUS_OK: 32 | return "STATUS_OK"; 33 | case INPUT_FORMAT_ERROR: 34 | return "INPUT_FORMAT_ERROR"; 35 | case CREATE_SESSION_FAIL: 36 | return "CREATE_SESSION_FAIL"; 37 | case GET_KEY_FAIL: 38 | return "GET_USER_KEY_FAIL"; 39 | case CHECK_TOKEN_FAIL: 40 | return "CHECK_TOKEN_FAIL"; 41 | case NO_ENC_UNSUPPORT: 42 | return "NO_ENC_UNSUPPORT"; 43 | case INVALID_SESSION_ID: 44 | return "INVALID_SESSION_ID"; 45 | case NO_SERVICE: 46 | return "NO_SERVICE"; 47 | case SESSION_TIMEOUT: 48 | return "SESSION_TIMEOUT"; 49 | case SERVICE_TOO_BUSY: 50 | return "SERVICE_TOO_BUSY"; 51 | case INVALID_SN: 52 | return "INVALID_SN"; 53 | case SN_TIMEOUT: 54 | return "SN_TIMEOUT"; 55 | case INVALID_TYPE: 56 | return "INVALID_TYPE"; 57 | case DECRYPT_FAIL: 58 | return "DECRYPT_FAIL"; 59 | case SERVICE_TYPE_ERROR: 60 | return "SERVICE_TYPE_ERROR"; 61 | case ASSESS_FORBIDDEN: 62 | return "ASSESS_FORBIDDEN"; 63 | case CALL_TIMEOUT: 64 | return "CALL_TIMEOUT"; 65 | case INNER_ERROR: 66 | return "INNER_ERROR"; 67 | } 68 | return ""; 69 | } 70 | 71 | }; 72 | 73 | 74 | #endif 75 | -------------------------------------------------------------------------------- /connect_server/test_server/src/proto/message.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | 4 | message Message{ 5 | optional string to = 1; 6 | optional int64 id = 2;//mssage id, did not set this field when send message 7 | optional int64 time = 3;//message send time 8 | optional string from = 4;//who send 9 | optional int32 type = 5; 10 | optional string sn = 6; 11 | optional bytes data = 7;//message data 12 | } 13 | 14 | 15 | -------------------------------------------------------------------------------- /connect_server/test_server/src/proto/msg_head.h: -------------------------------------------------------------------------------- 1 | #ifndef __MSG_HEAD_H__ 2 | #define __MSG_HEAD_H__ 3 | 4 | #include "base/ef_btype.h" 5 | 6 | namespace gim{ 7 | 8 | using namespace ef; 9 | 10 | enum{ 11 | KEEPALIVE_REQ = 0, 12 | KEEPALIVE_RESP = KEEPALIVE_REQ + 1, 13 | LOGIN_REQ = 100, 14 | LOGIN_RESP = LOGIN_REQ + 1, 15 | SERVICE_REQ = 200, 16 | SERVICE_RESP = SERVICE_REQ + 1, 17 | KICK_CLIENT = 400, 18 | REDIRECT_RESP = 1001, 19 | SET_TIME_RESP = 1101, 20 | MAGIC_NUMBER = 0x20141228, 21 | }; 22 | 23 | struct head{ 24 | int32 magic; 25 | int32 len;//include head 26 | //0: keepalive, 1: keepalive resp 27 | //100: login, 101: login resp 28 | //200: service, 201: service resp 29 | //300: regist service 301: regist service resp 30 | //400: kick client 31 | //1001: redericet 32 | int32 cmd; 33 | }; 34 | 35 | 36 | }; 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /connect_server/test_server/src/proto/pair.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | message Pair{ 4 | required string key = 1; 5 | optional bytes value = 2; 6 | } 7 | -------------------------------------------------------------------------------- /connect_server/test_server/src/proto/peer_server.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | import "message.proto"; 4 | 5 | message GetPeerMessageRequest{ 6 | optional string cid = 1;//to who 7 | optional int64 start_msgid = 2;//mssage id 8 | optional int64 count = 3;//message count 9 | } 10 | 11 | message GetPeerMessageResponse{ 12 | optional int64 last_msgid = 1; 13 | repeated Message msgs = 2; 14 | } 15 | 16 | message SendPeerMessageRequest{ 17 | required Message msg = 1; 18 | } 19 | 20 | //use for muti-point online 21 | message SendPeerMessageResponse{ 22 | required Message msg = 1; 23 | } 24 | 25 | message RecvPeerMessageResponse{ 26 | required Message msg = 1; 27 | } 28 | 29 | message PushMessageRequest{ 30 | required string sn = 1; 31 | required Message msg = 2; 32 | } 33 | 34 | message PushMessageResponse{ 35 | required string sn = 1; 36 | required int32 status = 2; 37 | } 38 | 39 | 40 | message PeerPacket{ 41 | required int32 cmd = 1;//110: send_req, 42 | //111: send_resp, 112: get_req, 43 | //113: get_resp, 115:recv_resp 44 | optional GetPeerMessageRequest get_peer_msg_req = 2; 45 | optional GetPeerMessageResponse get_peer_msg_resp = 3; 46 | optional SendPeerMessageRequest send_peer_msg_req = 4; 47 | optional SendPeerMessageResponse send_peer_msg_resp = 5; 48 | optional RecvPeerMessageResponse recv_peer_msg_resp = 6; 49 | } 50 | -------------------------------------------------------------------------------- /connect_server/test_server/src/proto/session.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | import "pair.proto"; 4 | 5 | message Sess{ 6 | optional string id = 1; 7 | optional int32 type = 2; 8 | optional int64 lasttime = 3; 9 | optional string sessid = 4; 10 | optional int32 consvid = 5; 11 | optional string version = 6; 12 | repeated Pair kvs = 7; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /dispatch_server/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project(DispatchServer) 2 | set(CMAKE_VERBOSE_MAKEFILE ON) 3 | add_definitions("-Wall -pg -g") 4 | include_directories(${PROJECT_SOURCE_DIR}/../common/include ${PROJECT_SOURCE_DIR}/../efnfw) 5 | link_directories(/usr/local/lib ../common/lib/ ../efnfw/lib) 6 | link_libraries(libcommon.a libefnfw.a hiredis zookeeper_mt protobuf jsoncpp pthread) 7 | file(GLOB_RECURSE SRC_LIST src/*.cpp) 8 | add_executable(bin/DispatchServer ${SRC_LIST}) 9 | -------------------------------------------------------------------------------- /dispatch_server/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | #cd ../efnfw 4 | #sh build.sh 5 | #cd - 6 | 7 | #cd ../common; 8 | #sh build.sh 9 | #cd - 10 | 11 | cp -fr ../proto src/ 12 | 13 | protoc src/proto/connect_server.proto --cpp_out=src/proto -I=src/proto 14 | 15 | mv src/proto/connect_server.pb.cc src/proto/connect_server.pb.cpp 16 | 17 | mkdir -p bin 18 | 19 | rm -fr CMakeCache.txt 20 | rm -fr CMakeFiles 21 | 22 | 23 | cmake . 24 | make clean;make 25 | 26 | rm -fr CMakeCache.txt 27 | rm -fr CMakeFiles 28 | -------------------------------------------------------------------------------- /dispatch_server/etc/ds.conf: -------------------------------------------------------------------------------- 1 | { 2 | "ID":23980923, 3 | "ThreadCnt":4, 4 | "Daemon":1, 5 | "ListenPort":13000, 6 | "StartThreadIdx":0, 7 | "ListenIP":"0.0.0.0", 8 | "zkurl":"115.159.89.35:12306,115.159.89.35:22306,115.159.89.35:32306,115.159.89.35:42306,115.159.89.35:52306", 9 | "ConsvPath":"/ConnectStatus", 10 | "LogConfig":"etc/elog.conf", 11 | "NetLogName":"DispatchNetLog" 12 | } 13 | 14 | -------------------------------------------------------------------------------- /dispatch_server/etc/elog.conf: -------------------------------------------------------------------------------- 1 | { 2 | "Appenders":[ { "Name":"console_appender", "Type":"ConsoleAppender"}, 3 | { "Name":"file_appender", "Type":"FileAppender", "Path":"/data/log/dispatch_server/all", "ScheduSpan":"HOUR", "ImmediatelyFlush":"false"} 4 | ], 5 | "Logs": [{ "Name":"console_logger", "Level":"ALL", "Appender":"console_appender" }, 6 | { "Name":"DispatchServer", "Level":"ALL", "Appender":"file_appender"}, 7 | { "Name":"file_logger1", "Level":"ALL", "Appender":"file_appender"} 8 | ], 9 | "DefaultLog":{"Level":"ALL", "Appender":"console_appender"} 10 | } 11 | -------------------------------------------------------------------------------- /dispatch_server/etc/settings.conf: -------------------------------------------------------------------------------- 1 | { 2 | "Daemon":1, 3 | "Type":0, 4 | 5 | "ThreadCount":2, 6 | 7 | "NetLogName":"ConnectNetLog", 8 | 9 | "CliConfigs": 10 | [{"Enc":0, "AliveMs":300000, 11 | "MinType":-1000, "MaxType":1, 12 | "ListenPort": 3000, "MaxReqQueSize": 0, 13 | "MaxPackCntPerMin": 0, "StartThreadIdx": 0, 14 | "ThreadCnt": 1}, 15 | 16 | {"Enc":0, "AliveMs":300000, 17 | "MinType":1, "MaxType":1000, 18 | "ListenPort": 3000, "MaxReqQueSize": 10000, 19 | "MaxPackCntPerMin": 0, "StartThreadIdx": 1, 20 | "ThreadCnt": 1} 21 | ], 22 | 23 | 24 | "SessCacheConfig": 25 | { 26 | "Type":"DefSessCache", 27 | "Config":{ 28 | "Expire":30, 29 | "DBGroup": 30 | { 31 | "NodeCfgs":[{"ipaddr":"127.0.0.1", "port":6379, "passwd":""} ] 32 | } 33 | } 34 | } 35 | 36 | 37 | } 38 | -------------------------------------------------------------------------------- /dispatch_server/src/common.cpp: -------------------------------------------------------------------------------- 1 | #include "common.h" 2 | #include "net/ef_connection.h" 3 | #include "proto/msg_head.h" 4 | #include "base/ef_utility.h" 5 | #include "base/ef_base64.h" 6 | #include 7 | #include 8 | 9 | namespace gim{ 10 | 11 | int32 checkLen(ef::Connection& c){ 12 | head h; 13 | if(c.bufLen() < (int32)sizeof(h)){ 14 | return 0; 15 | } 16 | c.peekBuf((char*)&h, sizeof(h)); 17 | h.magic = htonl(h.magic); 18 | h.len = htonl(h.len); 19 | if(h.len < (int32)sizeof(h) 20 | || h.len > 1024 * 1024){ 21 | return -1; 22 | } 23 | if(h.len <= c.bufLen()){ 24 | return h.len; 25 | } 26 | return 0; 27 | } 28 | 29 | 30 | int32 constructPacket(const head& h, 31 | const std::string& body, std::string& respbuf){ 32 | head rh; 33 | respbuf.reserve(sizeof(h) + body.size()); 34 | rh.cmd = htonl(h.cmd); 35 | rh.magic = h.magic; 36 | rh.len = htonl(sizeof(h) + body.size()); 37 | respbuf.append((char*)&rh, sizeof(rh)); 38 | respbuf.append(body); 39 | return 0; 40 | } 41 | 42 | int32 decorationName(int32 svid, int32 conid, 43 | const std::string& n, std::string& dn){ 44 | 45 | std::string buf; 46 | buf.resize(8); 47 | 48 | char* p = (char*)buf.data(); 49 | 50 | *(int32*)p = svid; 51 | *(int32*)(p + sizeof(int32)) = conid; 52 | 53 | dn = base64Encode(buf) + n; 54 | return 0; 55 | } 56 | 57 | int32 getDecorationInfo(const std::string& dn, int32& svid, 58 | int32& conid, std::string& n){ 59 | 60 | std::string b64buf = dn.substr(0, 12); 61 | 62 | std::string buf = base64Decode(b64buf); 63 | 64 | char* p = (char*)buf.data(); 65 | svid = *(int32*)p; 66 | conid = *(int32*)(p + sizeof(int32)); 67 | 68 | n = dn.substr(12); 69 | 70 | return 0; 71 | } 72 | 73 | 74 | }; 75 | -------------------------------------------------------------------------------- /dispatch_server/src/common.h: -------------------------------------------------------------------------------- 1 | #ifndef __COMMON_H__ 2 | #define __COMMON_H__ 3 | 4 | #include 5 | #include "base/ef_btype.h" 6 | #include "base/ef_log.h" 7 | #include "base/ef_utility.h" 8 | 9 | namespace ef{ 10 | class EventLoop; 11 | class Connection; 12 | }; 13 | 14 | namespace gim{ 15 | 16 | using namespace ef; 17 | 18 | class head; 19 | 20 | int32 checkLen(ef::Connection& c); 21 | 22 | int32 constructPacket(const head& h, 23 | const std::string& body, std::string& pack); 24 | 25 | enum{ 26 | EVENT_LOOP_PART_CNT = 4, 27 | EVENT_LOOP_ID_MASK = 1000000, 28 | FRE_CHECK_SPAN = 60, 29 | }; 30 | 31 | int32 decorationName(int32 svid, int32 conid, 32 | const std::string& n, std::string& dn); 33 | 34 | int32 getDecorationInfo(const std::string& dn, int32& svid, 35 | int32& conid, std::string& n); 36 | 37 | 38 | inline int32 serverEventLoopCount(int32 maxevlp){ 39 | int32 svevcnt = maxevlp >= EVENT_LOOP_PART_CNT ? 40 | maxevlp / EVENT_LOOP_PART_CNT : 1; 41 | return svevcnt; 42 | } 43 | 44 | inline int32 getConnectionId(int32 evlpid, int32 conid){ 45 | return (evlpid * EVENT_LOOP_ID_MASK) 46 | + (conid % EVENT_LOOP_ID_MASK); 47 | } 48 | 49 | inline int32 getEventLoopId(int32 conid){ 50 | return conid / EVENT_LOOP_ID_MASK; 51 | } 52 | 53 | 54 | #define SVID ef::Singleton::instance()->Id 55 | 56 | #define ALogTrace(a) logTrace(a) << " " 59 | #define ALogError(a) logError(a) << " " 62 | 63 | #define USER_KEY_FIELD_NAME "user_key" 64 | 65 | }; 66 | 67 | #endif/*COMMON_H*/ 68 | -------------------------------------------------------------------------------- /dispatch_server/src/common_logic_service.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | message CommonServiceRequest{ 4 | optional string cid = 1; 5 | optional int32 type = 2; 6 | optional string parms = 3; 7 | } 8 | 9 | message CommonServiceResponse{ 10 | optional string cid = 1; 11 | optional int32 type = 2; 12 | optional string status = 3; 13 | optional string code = 4; 14 | } -------------------------------------------------------------------------------- /dispatch_server/src/config.h: -------------------------------------------------------------------------------- 1 | #ifndef __DISPATCH_CFG_H__ 2 | #define __DISPATCH_CFG_H__ 3 | 4 | #include "json/json.h" 5 | #include 6 | 7 | namespace gim{ 8 | 9 | class DPConfig{ 10 | public: 11 | int ID; 12 | int ThreadCnt; 13 | int Daemon; 14 | int ListenPort; 15 | int StartThreadIdx; 16 | std::string ListenIP; 17 | std::string zkurl; 18 | std::string ConsvPath; 19 | std::string LogConfig; 20 | std::string NetLogName; 21 | }; 22 | 23 | }; 24 | #endif 25 | -------------------------------------------------------------------------------- /dispatch_server/src/connect_server.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | import "pair.proto"; 4 | 5 | message LoginRequest{ 6 | required string id = 1;//id of user or logic server 7 | optional int32 type = 2;//type 0:client, > 0:logic server 8 | optional string version = 3;//client version 9 | optional string token = 4; //use for check 10 | repeated Pair kvs = 5; 11 | } 12 | 13 | 14 | message LoginResponse{ 15 | required int32 status = 1;//if 0 success, else fail 16 | optional string sessid = 2;//the session id, 17 | optional int64 server_time = 3;//server timestamp 18 | } 19 | 20 | message Address{ 21 | optional string ip = 1; 22 | optional int32 port = 2; 23 | } 24 | 25 | message RedirectResponse{ 26 | required int32 status = 1;//if 0 success, else fail 27 | repeated Address addrs = 2; 28 | } 29 | 30 | 31 | message ServiceRequest{ 32 | optional string from_sessid = 1;//from sessid 33 | optional string to_sessid = 2;//to sessid 34 | required int32 from_type = 3;// 100:push 200:peer 35 | required int32 to_type = 4;// 100:push 200:peer 36 | required string sn = 5;//each req has an unique sn 37 | optional bytes payload = 6;// payload 38 | } 39 | 40 | message ServiceResponse{ 41 | optional string from_sessid = 1;//from sessid 42 | optional string to_sessid = 2; //to sessid 43 | required int32 from_type = 3; 44 | required int32 to_type = 4; 45 | required string sn = 5;//each req has an unique sn 46 | required int32 status = 6;// 0 success 47 | optional bytes payload = 7; 48 | } 49 | 50 | message KickCliRequest{ 51 | optional string sessid = 1; 52 | optional string id = 2; 53 | optional int32 type = 3; 54 | optional bytes reason = 4; 55 | } 56 | -------------------------------------------------------------------------------- /dispatch_server/src/consv_config.h: -------------------------------------------------------------------------------- 1 | #ifndef __CLI_CONFIG_H__ 2 | #define __CLI_CONFIG_H__ 3 | 4 | 5 | namespace gim{ 6 | 7 | class ConSvConfig{ 8 | public: 9 | int Enc; 10 | int AliveMs; 11 | int MinType; 12 | int MaxType; 13 | int ListenPort; 14 | int MaxReqQueSize; 15 | int MaxPackCntPerMin; 16 | int StartThreadIdx; 17 | int ThreadCnt; 18 | }; 19 | 20 | } 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /dispatch_server/src/dispatch_server.h: -------------------------------------------------------------------------------- 1 | #ifndef __DISPATCH_SERVER_H__ 2 | #define __DISPATCH_SERVER_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | #include "net/ef_server.h" 8 | #include "json/json.h" 9 | #include "config.h" 10 | #include "zk_client.h" 11 | #include "zk_server_ob.h" 12 | #include "zk_config.h" 13 | 14 | namespace ef { 15 | class EventLoop; 16 | }; 17 | 18 | namespace gim{ 19 | 20 | class CliConFactory; 21 | class CDispatcher; 22 | class ZKClient; 23 | class ZKConfig; 24 | class ZKServerNode; 25 | 26 | struct ConsvCfg { 27 | std::vector iplist; 28 | int port; 29 | }; 30 | 31 | typedef std::vector ConsvLst; 32 | 33 | struct typemap { 34 | int mintype; 35 | int maxtype; 36 | ConsvLst svlst; 37 | }; 38 | 39 | class DispatchServer{ 40 | public: 41 | DispatchServer():m_run(false),m_zkc(NULL),m_zkcnf(NULL),m_zksvob(NULL){}; 42 | int init(const std::string &confPath); 43 | int start(); 44 | int stop(); 45 | int getConsv(int type, const std::string &hashseed, ConsvCfg &csvcfg); 46 | ef::EventLoop& getEventLoop(int idx); 47 | 48 | const DPConfig& getConfig() const; 49 | 50 | private: 51 | int loadConsvCfg(); 52 | int initLog(); 53 | int startListen(); 54 | int stopListen(); 55 | int loadConfig(const Json::Value& v); 56 | int loadConsvCfg(const children_map &children); 57 | 58 | static void ConfUpdateCallback(void* ctx, int evtype, const Json::Value& notify); 59 | static int ConsvObCallBack(void* ctx, const children_map& children); 60 | 61 | DPConfig m_conf; 62 | ef::Server m_serv; 63 | std::vector m_tms; 64 | CliConFactory *m_cfac; 65 | CDispatcher *m_cdisp; 66 | 67 | bool m_run; 68 | 69 | ZKClient* m_zkc; 70 | ZKConfig* m_zkcnf; 71 | ZKServerListOb *m_zksvob; 72 | }; 73 | 74 | }; 75 | 76 | 77 | #endif/*__DISPATCH_SERVER_H__*/ 78 | -------------------------------------------------------------------------------- /dispatch_server/src/err_no.h: -------------------------------------------------------------------------------- 1 | #ifndef __ERR_NO_H__ 2 | #define __ERR_NO_H__ 3 | 4 | 5 | namespace gim{ 6 | 7 | 8 | enum{ 9 | STATUS_OK = 0, 10 | INPUT_FORMAT_ERROR = -1, 11 | CREATE_SESSION_FAIL = -10, 12 | GET_KEY_FAIL = -11, 13 | CHECK_TOKEN_FAIL = -12, 14 | NO_ENC_UNSUPPORT = -13, 15 | INVALID_SESSION_ID = -20, 16 | SESSION_TIMEOUT = -21, 17 | NO_SERVICE = -30, 18 | SERVICE_TOO_BUSY = -33, 19 | CALL_TIMEOUT = -34, 20 | INVALID_SN = -40, 21 | SN_TIMEOUT = -41, 22 | DECRYPT_FAIL = -50, 23 | INVALID_TYPE = -60, 24 | SERVICE_TYPE_ERROR = -70, 25 | ASSESS_FORBIDDEN = -80, 26 | INNER_ERROR = -100, 27 | }; 28 | 29 | inline const char* getErrStr(int e){ 30 | switch(e){ 31 | case STATUS_OK: 32 | return "STATUS_OK"; 33 | case INPUT_FORMAT_ERROR: 34 | return "INPUT_FORMAT_ERROR"; 35 | case CREATE_SESSION_FAIL: 36 | return "CREATE_SESSION_FAIL"; 37 | case GET_KEY_FAIL: 38 | return "GET_USER_KEY_FAIL"; 39 | case CHECK_TOKEN_FAIL: 40 | return "CHECK_TOKEN_FAIL"; 41 | case NO_ENC_UNSUPPORT: 42 | return "NO_ENC_UNSUPPORT"; 43 | case INVALID_SESSION_ID: 44 | return "INVALID_SESSION_ID"; 45 | case NO_SERVICE: 46 | return "NO_SERVICE"; 47 | case SESSION_TIMEOUT: 48 | return "SESSION_TIMEOUT"; 49 | case SERVICE_TOO_BUSY: 50 | return "SERVICE_TOO_BUSY"; 51 | case INVALID_SN: 52 | return "INVALID_SN"; 53 | case SN_TIMEOUT: 54 | return "SN_TIMEOUT"; 55 | case INVALID_TYPE: 56 | return "INVALID_TYPE"; 57 | case DECRYPT_FAIL: 58 | return "DECRYPT_FAIL"; 59 | case SERVICE_TYPE_ERROR: 60 | return "SERVICE_TYPE_ERROR"; 61 | case ASSESS_FORBIDDEN: 62 | return "ASSESS_FORBIDDEN"; 63 | case CALL_TIMEOUT: 64 | return "CALL_TIMEOUT"; 65 | case INNER_ERROR: 66 | return "INNER_ERROR"; 67 | } 68 | return ""; 69 | } 70 | 71 | }; 72 | 73 | 74 | #endif 75 | -------------------------------------------------------------------------------- /dispatch_server/src/message.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | 4 | message Message{ 5 | optional string to = 1; 6 | optional int64 id = 2;//mssage id, did not set this field when send message 7 | optional int64 time = 3;//message send time 8 | optional string from = 4;//who send 9 | optional int32 type = 5; 10 | optional string sn = 6; 11 | optional bytes data = 7;//message data 12 | } 13 | 14 | 15 | -------------------------------------------------------------------------------- /dispatch_server/src/msg_head.h: -------------------------------------------------------------------------------- 1 | #ifndef __MSG_HEAD_H__ 2 | #define __MSG_HEAD_H__ 3 | 4 | #include "base/ef_btype.h" 5 | 6 | namespace gim{ 7 | 8 | using namespace ef; 9 | 10 | enum{ 11 | KEEPALIVE_REQ = 0, 12 | KEEPALIVE_RESP = KEEPALIVE_REQ + 1, 13 | LOGIN_REQ = 100, 14 | LOGIN_RESP = LOGIN_REQ + 1, 15 | SERVICE_REQ = 200, 16 | SERVICE_RESP = SERVICE_REQ + 1, 17 | KICK_CLIENT = 400, 18 | REDIRECT_RESP = 1001, 19 | SET_TIME_RESP = 1101, 20 | MAGIC_NUMBER = 0x20141228, 21 | }; 22 | 23 | struct head{ 24 | int32 magic; 25 | int32 len;//include head 26 | //0: keepalive, 1: keepalive resp 27 | //100: login, 101: login resp 28 | //200: service, 201: service resp 29 | //300: regist service 301: regist service resp 30 | //400: kick client 31 | //1001: redericet 32 | int32 cmd; 33 | }; 34 | 35 | 36 | }; 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /dispatch_server/src/pair.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | message Pair{ 4 | required string key = 1; 5 | optional bytes value = 2; 6 | } 7 | -------------------------------------------------------------------------------- /dispatch_server/src/proto/common_logic_service.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | message CommonServiceRequest{ 4 | optional string cid = 1; 5 | optional int32 type = 2; 6 | optional string parms = 3; 7 | } 8 | 9 | message CommonServiceResponse{ 10 | optional string cid = 1; 11 | optional int32 type = 2; 12 | optional string status = 3; 13 | optional string code = 4; 14 | } -------------------------------------------------------------------------------- /dispatch_server/src/proto/connect_server.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | import "pair.proto"; 4 | 5 | message LoginRequest{ 6 | required string id = 1;//id of user or logic server 7 | optional int32 type = 2;//type 0:client, > 0:logic server 8 | optional string version = 3;//client version 9 | optional string token = 4; //use for check 10 | repeated Pair kvs = 5; 11 | } 12 | 13 | 14 | message LoginResponse{ 15 | required int32 status = 1;//if 0 success, else fail 16 | optional string sessid = 2;//the session id, 17 | optional int64 server_time = 3;//server timestamp 18 | } 19 | 20 | message Address{ 21 | optional string ip = 1; 22 | optional int32 port = 2; 23 | } 24 | 25 | message RedirectResponse{ 26 | required int32 status = 1;//if 0 success, else fail 27 | repeated Address addrs = 2; 28 | } 29 | 30 | 31 | message ServiceRequest{ 32 | optional string from_sessid = 1;//from sessid 33 | optional string to_sessid = 2;//to sessid 34 | required int32 from_type = 3;// 100:push 200:peer 35 | required int32 to_type = 4;// 100:push 200:peer 36 | required string sn = 5;//each req has an unique sn 37 | optional bytes payload = 6;// payload 38 | } 39 | 40 | message ServiceResponse{ 41 | optional string from_sessid = 1;//from sessid 42 | optional string to_sessid = 2; //to sessid 43 | required int32 from_type = 3; 44 | required int32 to_type = 4; 45 | required string sn = 5;//each req has an unique sn 46 | required int32 status = 6;// 0 success 47 | optional bytes payload = 7; 48 | } 49 | 50 | message KickCliRequest{ 51 | optional string sessid = 1; 52 | optional string id = 2; 53 | optional int32 type = 3; 54 | optional bytes reason = 4; 55 | } 56 | -------------------------------------------------------------------------------- /dispatch_server/src/proto/err_no.h: -------------------------------------------------------------------------------- 1 | #ifndef __ERR_NO_H__ 2 | #define __ERR_NO_H__ 3 | 4 | 5 | namespace gim{ 6 | 7 | 8 | enum{ 9 | STATUS_OK = 0, 10 | INPUT_FORMAT_ERROR = -1, 11 | CREATE_SESSION_FAIL = -10, 12 | GET_KEY_FAIL = -11, 13 | CHECK_TOKEN_FAIL = -12, 14 | NO_ENC_UNSUPPORT = -13, 15 | INVALID_SESSION_ID = -20, 16 | SESSION_TIMEOUT = -21, 17 | NO_SERVICE = -30, 18 | SERVICE_TOO_BUSY = -33, 19 | CALL_TIMEOUT = -34, 20 | INVALID_SN = -40, 21 | SN_TIMEOUT = -41, 22 | DECRYPT_FAIL = -50, 23 | INVALID_TYPE = -60, 24 | SERVICE_TYPE_ERROR = -70, 25 | ASSESS_FORBIDDEN = -80, 26 | INNER_ERROR = -100, 27 | }; 28 | 29 | inline const char* getErrStr(int e){ 30 | switch(e){ 31 | case STATUS_OK: 32 | return "STATUS_OK"; 33 | case INPUT_FORMAT_ERROR: 34 | return "INPUT_FORMAT_ERROR"; 35 | case CREATE_SESSION_FAIL: 36 | return "CREATE_SESSION_FAIL"; 37 | case GET_KEY_FAIL: 38 | return "GET_USER_KEY_FAIL"; 39 | case CHECK_TOKEN_FAIL: 40 | return "CHECK_TOKEN_FAIL"; 41 | case NO_ENC_UNSUPPORT: 42 | return "NO_ENC_UNSUPPORT"; 43 | case INVALID_SESSION_ID: 44 | return "INVALID_SESSION_ID"; 45 | case NO_SERVICE: 46 | return "NO_SERVICE"; 47 | case SESSION_TIMEOUT: 48 | return "SESSION_TIMEOUT"; 49 | case SERVICE_TOO_BUSY: 50 | return "SERVICE_TOO_BUSY"; 51 | case INVALID_SN: 52 | return "INVALID_SN"; 53 | case SN_TIMEOUT: 54 | return "SN_TIMEOUT"; 55 | case INVALID_TYPE: 56 | return "INVALID_TYPE"; 57 | case DECRYPT_FAIL: 58 | return "DECRYPT_FAIL"; 59 | case SERVICE_TYPE_ERROR: 60 | return "SERVICE_TYPE_ERROR"; 61 | case ASSESS_FORBIDDEN: 62 | return "ASSESS_FORBIDDEN"; 63 | case CALL_TIMEOUT: 64 | return "CALL_TIMEOUT"; 65 | case INNER_ERROR: 66 | return "INNER_ERROR"; 67 | } 68 | return ""; 69 | } 70 | 71 | }; 72 | 73 | 74 | #endif 75 | -------------------------------------------------------------------------------- /dispatch_server/src/proto/message.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | 4 | message Message{ 5 | optional string to = 1; 6 | optional int64 id = 2;//mssage id, did not set this field when send message 7 | optional int64 time = 3;//message send time 8 | optional string from = 4;//who send 9 | optional int32 type = 5; 10 | optional string sn = 6; 11 | optional bytes data = 7;//message data 12 | } 13 | 14 | 15 | -------------------------------------------------------------------------------- /dispatch_server/src/proto/msg_head.h: -------------------------------------------------------------------------------- 1 | #ifndef __MSG_HEAD_H__ 2 | #define __MSG_HEAD_H__ 3 | 4 | #include "base/ef_btype.h" 5 | 6 | namespace gim{ 7 | 8 | using namespace ef; 9 | 10 | enum{ 11 | KEEPALIVE_REQ = 0, 12 | KEEPALIVE_RESP = KEEPALIVE_REQ + 1, 13 | LOGIN_REQ = 100, 14 | LOGIN_RESP = LOGIN_REQ + 1, 15 | SERVICE_REQ = 200, 16 | SERVICE_RESP = SERVICE_REQ + 1, 17 | KICK_CLIENT = 400, 18 | REDIRECT_RESP = 1001, 19 | SET_TIME_RESP = 1101, 20 | MAGIC_NUMBER = 0x20141228, 21 | }; 22 | 23 | struct head{ 24 | int32 magic; 25 | int32 len;//include head 26 | //0: keepalive, 1: keepalive resp 27 | //100: login, 101: login resp 28 | //200: service, 201: service resp 29 | //300: regist service 301: regist service resp 30 | //400: kick client 31 | //1001: redericet 32 | int32 cmd; 33 | }; 34 | 35 | 36 | }; 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /dispatch_server/src/proto/pair.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | message Pair{ 4 | required string key = 1; 5 | optional bytes value = 2; 6 | } 7 | -------------------------------------------------------------------------------- /dispatch_server/src/proto/session.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | import "pair.proto"; 4 | 5 | message Sess{ 6 | optional string id = 1; 7 | optional int32 type = 2; 8 | optional int64 lasttime = 3; 9 | optional string sessid = 4; 10 | optional int32 consvid = 5; 11 | optional string version = 6; 12 | repeated Pair kvs = 7; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /dispatch_server/src/session.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | import "pair.proto"; 4 | 5 | message Sess{ 6 | optional string id = 1; 7 | optional int32 type = 2; 8 | optional int64 lasttime = 3; 9 | optional string sessid = 4; 10 | optional int32 consvid = 5; 11 | optional string version = 6; 12 | repeated Pair kvs = 7; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /dispatch_server/test/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project(DPTEST) 2 | set(CMAKE_VERBOSE_MAKEFILE ON) 3 | add_definitions("-Wall -pg -g") 4 | include_directories(${PROJECT_SOURCE_DIR}/../../common/include 5 | ${PROJECT_SOURCE_DIR}/../../efnfw ${PROJECT_SOURCE_DIR}/src) 6 | link_directories(/usr/local/lib ../../common/lib/ ../../efnfw/lib) 7 | link_libraries(libcommon.a libefnfw.a hiredis zookeeper_mt protobuf jsoncpp pthread) 8 | file(GLOB_RECURSE SRC_LIST src/*.cpp) 9 | add_executable(bin/dptest ${SRC_LIST}) 10 | -------------------------------------------------------------------------------- /dispatch_server/test/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | cp -fr ../../proto src/ 4 | 5 | protoc src/proto/connect_server.proto --cpp_out=src/proto -I=src/proto 6 | 7 | protoc src/proto/pair.proto --cpp_out=src/proto -I=src/proto 8 | mv src/proto/connect_server.pb.cc src/proto/connect_server.pb.cpp 9 | mv src/proto/pair.pb.cc src/proto/pair.pb.cpp 10 | 11 | mkdir -p bin 12 | 13 | rm -fr CMakeCache.txt 14 | rm -fr CMakeFiles 15 | 16 | cmake . 17 | make clean;make 18 | 19 | rm -fr CMakeCache.txt 20 | rm -fr CMakeFiles 21 | -------------------------------------------------------------------------------- /dispatch_server/test/src/common_logic_service.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | message CommonServiceRequest{ 4 | optional string cid = 1; 5 | optional int32 type = 2; 6 | optional string parms = 3; 7 | } 8 | 9 | message CommonServiceResponse{ 10 | optional string cid = 1; 11 | optional int32 type = 2; 12 | optional string status = 3; 13 | optional string code = 4; 14 | } -------------------------------------------------------------------------------- /dispatch_server/test/src/connect_server.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | import "pair.proto"; 4 | 5 | message LoginRequest{ 6 | required string id = 1;//id of user or logic server 7 | optional int32 type = 2;//type 0:client, > 0:logic server 8 | optional string version = 3;//client version 9 | optional string token = 4; //use for check 10 | repeated Pair kvs = 5; 11 | } 12 | 13 | 14 | message LoginResponse{ 15 | required int32 status = 1;//if 0 success, else fail 16 | optional string sessid = 2;//the session id, 17 | optional int64 server_time = 3;//server timestamp 18 | } 19 | 20 | message Address{ 21 | optional string ip = 1; 22 | optional int32 port = 2; 23 | } 24 | 25 | message RedirectResponse{ 26 | required int32 status = 1;//if 0 success, else fail 27 | repeated Address addrs = 2; 28 | } 29 | 30 | 31 | message ServiceRequest{ 32 | optional string from_sessid = 1;//from sessid 33 | optional string to_sessid = 2;//to sessid 34 | required int32 from_type = 3;// 100:push 200:peer 35 | required int32 to_type = 4;// 100:push 200:peer 36 | required string sn = 5;//each req has an unique sn 37 | optional bytes payload = 6;// payload 38 | } 39 | 40 | message ServiceResponse{ 41 | optional string from_sessid = 1;//from sessid 42 | optional string to_sessid = 2; //to sessid 43 | required int32 from_type = 3; 44 | required int32 to_type = 4; 45 | required string sn = 5;//each req has an unique sn 46 | required int32 status = 6;// 0 success 47 | optional bytes payload = 7; 48 | } 49 | 50 | message KickCliRequest{ 51 | optional string sessid = 1; 52 | optional string id = 2; 53 | optional int32 type = 3; 54 | optional bytes reason = 4; 55 | } 56 | -------------------------------------------------------------------------------- /dispatch_server/test/src/err_no.h: -------------------------------------------------------------------------------- 1 | #ifndef __ERR_NO_H__ 2 | #define __ERR_NO_H__ 3 | 4 | 5 | namespace gim{ 6 | 7 | 8 | enum{ 9 | STATUS_OK = 0, 10 | INPUT_FORMAT_ERROR = -1, 11 | CREATE_SESSION_FAIL = -10, 12 | GET_KEY_FAIL = -11, 13 | CHECK_TOKEN_FAIL = -12, 14 | NO_ENC_UNSUPPORT = -13, 15 | INVALID_SESSION_ID = -20, 16 | SESSION_TIMEOUT = -21, 17 | NO_SERVICE = -30, 18 | SERVICE_TOO_BUSY = -33, 19 | CALL_TIMEOUT = -34, 20 | INVALID_SN = -40, 21 | SN_TIMEOUT = -41, 22 | DECRYPT_FAIL = -50, 23 | INVALID_TYPE = -60, 24 | SERVICE_TYPE_ERROR = -70, 25 | ASSESS_FORBIDDEN = -80, 26 | INNER_ERROR = -100, 27 | }; 28 | 29 | inline const char* getErrStr(int e){ 30 | switch(e){ 31 | case STATUS_OK: 32 | return "STATUS_OK"; 33 | case INPUT_FORMAT_ERROR: 34 | return "INPUT_FORMAT_ERROR"; 35 | case CREATE_SESSION_FAIL: 36 | return "CREATE_SESSION_FAIL"; 37 | case GET_KEY_FAIL: 38 | return "GET_USER_KEY_FAIL"; 39 | case CHECK_TOKEN_FAIL: 40 | return "CHECK_TOKEN_FAIL"; 41 | case NO_ENC_UNSUPPORT: 42 | return "NO_ENC_UNSUPPORT"; 43 | case INVALID_SESSION_ID: 44 | return "INVALID_SESSION_ID"; 45 | case NO_SERVICE: 46 | return "NO_SERVICE"; 47 | case SESSION_TIMEOUT: 48 | return "SESSION_TIMEOUT"; 49 | case SERVICE_TOO_BUSY: 50 | return "SERVICE_TOO_BUSY"; 51 | case INVALID_SN: 52 | return "INVALID_SN"; 53 | case SN_TIMEOUT: 54 | return "SN_TIMEOUT"; 55 | case INVALID_TYPE: 56 | return "INVALID_TYPE"; 57 | case DECRYPT_FAIL: 58 | return "DECRYPT_FAIL"; 59 | case SERVICE_TYPE_ERROR: 60 | return "SERVICE_TYPE_ERROR"; 61 | case ASSESS_FORBIDDEN: 62 | return "ASSESS_FORBIDDEN"; 63 | case CALL_TIMEOUT: 64 | return "CALL_TIMEOUT"; 65 | case INNER_ERROR: 66 | return "INNER_ERROR"; 67 | } 68 | return ""; 69 | } 70 | 71 | }; 72 | 73 | 74 | #endif 75 | -------------------------------------------------------------------------------- /dispatch_server/test/src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "net/ef_sock.h" 7 | #include "proto/msg_head.h" 8 | #include "proto/connect_server.pb.h" 9 | #include "base/ef_utility.h" 10 | #include "base/ef_log.h" 11 | 12 | using namespace ef; 13 | using namespace gim; 14 | using namespace std; 15 | 16 | int main(int argc, char **argv) 17 | { 18 | LoginRequest lgreq; 19 | RedirectResponse rdresp; 20 | string lgreqbuf; 21 | SOCKET sockfd; 22 | 23 | lgreq.set_id("131aseasdlkle"); 24 | lgreq.set_type(0); 25 | lgreq.set_version("1.0.1"); 26 | lgreq.set_token("slakjflejejr"); 27 | lgreq.SerializeToString(&lgreqbuf); 28 | 29 | sockfd = tcpConnect("127.0.0.1", 13000, NULL, 0); 30 | if (sockfd < 0) { 31 | std::cout << "connect server fail" << std::endl; 32 | return -1; 33 | } 34 | 35 | head rh; 36 | rh.cmd = htonl(LOGIN_REQ); 37 | rh.magic = 20150624; 38 | rh.len = htonl(sizeof(head) + lgreqbuf.size()); 39 | string respbuf; 40 | respbuf.append((char *)&rh, sizeof(rh)); 41 | respbuf.append(lgreqbuf); 42 | tcpSend(sockfd, respbuf.data(), respbuf.size(), 100, NULL); 43 | char buf[1024]; 44 | tcpReceive(sockfd, buf, 1024, 100, NULL); 45 | head h1 = *(head*)buf; 46 | h1.cmd = htonl(h1.cmd); 47 | h1.len = htonl(h1.len); 48 | rdresp.ParseFromArray(buf + sizeof(h1), h1.len - sizeof(h1)); 49 | for (int i = 0; i < rdresp.addrs_size(); i++) { 50 | std::cout << rdresp.addrs(i).ip() << ":" << rdresp.addrs(i).port() << std::endl; 51 | } 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /dispatch_server/test/src/message.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | 4 | message Message{ 5 | optional string to = 1; 6 | optional int64 id = 2;//mssage id, did not set this field when send message 7 | optional int64 time = 3;//message send time 8 | optional string from = 4;//who send 9 | optional int32 type = 5; 10 | optional string sn = 6; 11 | optional bytes data = 7;//message data 12 | } 13 | 14 | 15 | -------------------------------------------------------------------------------- /dispatch_server/test/src/msg_head.h: -------------------------------------------------------------------------------- 1 | #ifndef __MSG_HEAD_H__ 2 | #define __MSG_HEAD_H__ 3 | 4 | #include "base/ef_btype.h" 5 | 6 | namespace gim{ 7 | 8 | using namespace ef; 9 | 10 | enum{ 11 | KEEPALIVE_REQ = 0, 12 | KEEPALIVE_RESP = KEEPALIVE_REQ + 1, 13 | LOGIN_REQ = 100, 14 | LOGIN_RESP = LOGIN_REQ + 1, 15 | SERVICE_REQ = 200, 16 | SERVICE_RESP = SERVICE_REQ + 1, 17 | KICK_CLIENT = 400, 18 | REDIRECT_RESP = 1001, 19 | SET_TIME_RESP = 1101, 20 | MAGIC_NUMBER = 0x20141228, 21 | }; 22 | 23 | struct head{ 24 | int32 magic; 25 | int32 len;//include head 26 | //0: keepalive, 1: keepalive resp 27 | //100: login, 101: login resp 28 | //200: service, 201: service resp 29 | //300: regist service 301: regist service resp 30 | //400: kick client 31 | //1001: redericet 32 | int32 cmd; 33 | }; 34 | 35 | 36 | }; 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /dispatch_server/test/src/pair.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | message Pair{ 4 | required string key = 1; 5 | optional bytes value = 2; 6 | } 7 | -------------------------------------------------------------------------------- /dispatch_server/test/src/proto/common_logic_service.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | message CommonServiceRequest{ 4 | optional string cid = 1; 5 | optional int32 type = 2; 6 | optional string parms = 3; 7 | } 8 | 9 | message CommonServiceResponse{ 10 | optional string cid = 1; 11 | optional int32 type = 2; 12 | optional string status = 3; 13 | optional string code = 4; 14 | } -------------------------------------------------------------------------------- /dispatch_server/test/src/proto/connect_server.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | import "pair.proto"; 4 | 5 | message LoginRequest{ 6 | required string id = 1;//id of user or logic server 7 | optional int32 type = 2;//type 0:client, > 0:logic server 8 | optional string version = 3;//client version 9 | optional string token = 4; //use for check 10 | repeated Pair kvs = 5; 11 | } 12 | 13 | 14 | message LoginResponse{ 15 | required int32 status = 1;//if 0 success, else fail 16 | optional string sessid = 2;//the session id, 17 | optional int64 server_time = 3;//server timestamp 18 | } 19 | 20 | message Address{ 21 | optional string ip = 1; 22 | optional int32 port = 2; 23 | } 24 | 25 | message RedirectResponse{ 26 | required int32 status = 1;//if 0 success, else fail 27 | repeated Address addrs = 2; 28 | } 29 | 30 | 31 | message ServiceRequest{ 32 | optional string from_sessid = 1;//from sessid 33 | optional string to_sessid = 2;//to sessid 34 | required int32 from_type = 3;// 100:push 200:peer 35 | required int32 to_type = 4;// 100:push 200:peer 36 | required string sn = 5;//each req has an unique sn 37 | optional bytes payload = 6;// payload 38 | } 39 | 40 | message ServiceResponse{ 41 | optional string from_sessid = 1;//from sessid 42 | optional string to_sessid = 2; //to sessid 43 | required int32 from_type = 3; 44 | required int32 to_type = 4; 45 | required string sn = 5;//each req has an unique sn 46 | required int32 status = 6;// 0 success 47 | optional bytes payload = 7; 48 | } 49 | 50 | message KickCliRequest{ 51 | optional string sessid = 1; 52 | optional string id = 2; 53 | optional int32 type = 3; 54 | optional bytes reason = 4; 55 | } 56 | -------------------------------------------------------------------------------- /dispatch_server/test/src/proto/err_no.h: -------------------------------------------------------------------------------- 1 | #ifndef __ERR_NO_H__ 2 | #define __ERR_NO_H__ 3 | 4 | 5 | namespace gim{ 6 | 7 | 8 | enum{ 9 | STATUS_OK = 0, 10 | INPUT_FORMAT_ERROR = -1, 11 | CREATE_SESSION_FAIL = -10, 12 | GET_KEY_FAIL = -11, 13 | CHECK_TOKEN_FAIL = -12, 14 | NO_ENC_UNSUPPORT = -13, 15 | INVALID_SESSION_ID = -20, 16 | SESSION_TIMEOUT = -21, 17 | NO_SERVICE = -30, 18 | SERVICE_TOO_BUSY = -33, 19 | CALL_TIMEOUT = -34, 20 | INVALID_SN = -40, 21 | SN_TIMEOUT = -41, 22 | DECRYPT_FAIL = -50, 23 | INVALID_TYPE = -60, 24 | SERVICE_TYPE_ERROR = -70, 25 | ASSESS_FORBIDDEN = -80, 26 | INNER_ERROR = -100, 27 | }; 28 | 29 | inline const char* getErrStr(int e){ 30 | switch(e){ 31 | case STATUS_OK: 32 | return "STATUS_OK"; 33 | case INPUT_FORMAT_ERROR: 34 | return "INPUT_FORMAT_ERROR"; 35 | case CREATE_SESSION_FAIL: 36 | return "CREATE_SESSION_FAIL"; 37 | case GET_KEY_FAIL: 38 | return "GET_USER_KEY_FAIL"; 39 | case CHECK_TOKEN_FAIL: 40 | return "CHECK_TOKEN_FAIL"; 41 | case NO_ENC_UNSUPPORT: 42 | return "NO_ENC_UNSUPPORT"; 43 | case INVALID_SESSION_ID: 44 | return "INVALID_SESSION_ID"; 45 | case NO_SERVICE: 46 | return "NO_SERVICE"; 47 | case SESSION_TIMEOUT: 48 | return "SESSION_TIMEOUT"; 49 | case SERVICE_TOO_BUSY: 50 | return "SERVICE_TOO_BUSY"; 51 | case INVALID_SN: 52 | return "INVALID_SN"; 53 | case SN_TIMEOUT: 54 | return "SN_TIMEOUT"; 55 | case INVALID_TYPE: 56 | return "INVALID_TYPE"; 57 | case DECRYPT_FAIL: 58 | return "DECRYPT_FAIL"; 59 | case SERVICE_TYPE_ERROR: 60 | return "SERVICE_TYPE_ERROR"; 61 | case ASSESS_FORBIDDEN: 62 | return "ASSESS_FORBIDDEN"; 63 | case CALL_TIMEOUT: 64 | return "CALL_TIMEOUT"; 65 | case INNER_ERROR: 66 | return "INNER_ERROR"; 67 | } 68 | return ""; 69 | } 70 | 71 | }; 72 | 73 | 74 | #endif 75 | -------------------------------------------------------------------------------- /dispatch_server/test/src/proto/message.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | 4 | message Message{ 5 | optional string to = 1; 6 | optional int64 id = 2;//mssage id, did not set this field when send message 7 | optional int64 time = 3;//message send time 8 | optional string from = 4;//who send 9 | optional int32 type = 5; 10 | optional string sn = 6; 11 | optional bytes data = 7;//message data 12 | } 13 | 14 | 15 | -------------------------------------------------------------------------------- /dispatch_server/test/src/proto/msg_head.h: -------------------------------------------------------------------------------- 1 | #ifndef __MSG_HEAD_H__ 2 | #define __MSG_HEAD_H__ 3 | 4 | #include "base/ef_btype.h" 5 | 6 | namespace gim{ 7 | 8 | using namespace ef; 9 | 10 | enum{ 11 | KEEPALIVE_REQ = 0, 12 | KEEPALIVE_RESP = KEEPALIVE_REQ + 1, 13 | LOGIN_REQ = 100, 14 | LOGIN_RESP = LOGIN_REQ + 1, 15 | SERVICE_REQ = 200, 16 | SERVICE_RESP = SERVICE_REQ + 1, 17 | KICK_CLIENT = 400, 18 | REDIRECT_RESP = 1001, 19 | SET_TIME_RESP = 1101, 20 | MAGIC_NUMBER = 0x20141228, 21 | }; 22 | 23 | struct head{ 24 | int32 magic; 25 | int32 len;//include head 26 | //0: keepalive, 1: keepalive resp 27 | //100: login, 101: login resp 28 | //200: service, 201: service resp 29 | //300: regist service 301: regist service resp 30 | //400: kick client 31 | //1001: redericet 32 | int32 cmd; 33 | }; 34 | 35 | 36 | }; 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /dispatch_server/test/src/proto/pair.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | message Pair{ 4 | required string key = 1; 5 | optional bytes value = 2; 6 | } 7 | -------------------------------------------------------------------------------- /dispatch_server/test/src/proto/session.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | import "pair.proto"; 4 | 5 | message Sess{ 6 | optional string id = 1; 7 | optional int32 type = 2; 8 | optional int64 lasttime = 3; 9 | optional string sessid = 4; 10 | optional int32 consvid = 5; 11 | optional string version = 6; 12 | repeated Pair kvs = 7; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /dispatch_server/test/src/session.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | import "pair.proto"; 4 | 5 | message Sess{ 6 | optional string id = 1; 7 | optional int32 type = 2; 8 | optional int64 lasttime = 3; 9 | optional string sessid = 4; 10 | optional int32 consvid = 5; 11 | optional string version = 6; 12 | repeated Pair kvs = 7; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /doc/GIM编码规范.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmxiaocong/gim/f9bac9be3d2afdc9bea69d201d1ce3c10a56639e/doc/GIM编码规范.doc -------------------------------------------------------------------------------- /doc/架构设计.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmxiaocong/gim/f9bac9be3d2afdc9bea69d201d1ce3c10a56639e/doc/架构设计.doc -------------------------------------------------------------------------------- /efnfw/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.6) 2 | project(efnfw) 3 | set(CMAKE_VERBOSE_MAKEFILE ON) 4 | add_definitions("-Wall -g") 5 | set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib) 6 | 7 | include_directories(${PROJECT_SOURCE_DIR}) 8 | file(GLOB_RECURSE EF_LIST base/*.cpp net/*.cpp) 9 | add_library(efnfw STATIC ${EF_LIST}) 10 | -------------------------------------------------------------------------------- /efnfw/base/ef_aes.h: -------------------------------------------------------------------------------- 1 | #ifndef __EF_AES_H__ 2 | #define __EF_AES_H__ 3 | 4 | #include 5 | #include "ef_btype.h" 6 | 7 | 8 | namespace ef{ 9 | 10 | /* Error Codes */ 11 | #define AES_BAD_KEY_DIR -1 /* Key direction is invalid, e.g., unknown value */ 12 | #define AES_BAD_KEY_MAT -2 /* Key material not of correct length */ 13 | #define AES_BAD_KEY_INSTANCE -3 /* Key passed is not valid */ 14 | #define AES_BAD_CIPHER_MODE -4 /* Params struct passed to cipherInit invalid */ 15 | #define AES_BAD_CIPHER_STATE -5 /* Cipher in wrong state (e.g., not initialized) */ 16 | #define AES_BAD_BLOCK_LENGTH -6 17 | #define AES_BAD_CIPHER_INSTANCE -7 18 | #define AES_BAD_DATA -8 /* Data contents are invalid, e.g., invalid padding */ 19 | #define AES_BAD_OTHER -9 /* Unknown error */ 20 | 21 | int32 aesEncrypt(const std::string& src, 22 | const std::string& key, std::string& dst); 23 | int32 aesEncrypt(const char* src, size_t len, 24 | const std::string& key, std::string& dst); 25 | int32 aesDecrypt(const std::string& src, 26 | const std::string& key, std::string& dst); 27 | int32 aesDecrypt(const char* src, size_t len, 28 | const std::string& key, std::string& dst); 29 | }; 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /efnfw/base/ef_atomic.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmxiaocong/gim/f9bac9be3d2afdc9bea69d201d1ce3c10a56639e/efnfw/base/ef_atomic.cpp -------------------------------------------------------------------------------- /efnfw/base/ef_base64.h: -------------------------------------------------------------------------------- 1 | #ifndef __EF_BASE64_H__ 2 | #define __EF_BASE64_H__ 3 | 4 | #include 5 | 6 | namespace ef{ 7 | 8 | std::string base64Encode(const std::string& s); 9 | std::string base64Decode(const std::string& s); 10 | 11 | } 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /efnfw/base/ef_btype.h: -------------------------------------------------------------------------------- 1 | #ifndef __EF_BTYPE_H__ 2 | #define __EF_BTYPE_H__ 3 | 4 | #ifdef __linux 5 | #include 6 | #endif 7 | 8 | 9 | namespace ef{ 10 | 11 | #ifdef __linux 12 | typedef u_int64_t uint64; 13 | typedef int64_t sint64; 14 | typedef int64_t int64; 15 | typedef u_int32_t uint32; 16 | typedef int32_t sint32; 17 | typedef int32_t int32; 18 | typedef u_int16_t uint16; 19 | typedef int16_t sint16; 20 | typedef int16_t int16; 21 | typedef u_int8_t uint8; 22 | typedef int8_t sint8; 23 | typedef int8_t int8; 24 | #else 25 | typedef unsigned long long uint64; 26 | typedef long long sint64; 27 | typedef long long int64; 28 | typedef unsigned long ulong; 29 | typedef unsigned int uint32; 30 | typedef int sint32; 31 | typedef int int32; 32 | typedef unsigned short int uint16; 33 | typedef short int sint16; 34 | typedef short int int16; 35 | typedef unsigned char uint8; 36 | typedef char sint8; 37 | typedef char int8; 38 | #endif 39 | 40 | }//namespace ef 41 | 42 | 43 | #endif 44 | 45 | -------------------------------------------------------------------------------- /efnfw/base/ef_deamonize.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | 10 | 11 | namespace ef{ 12 | void daemonize(){ 13 | // clear file creation mask 14 | umask(0); 15 | 16 | // fork child 17 | pid_t pid = fork(); 18 | 19 | if (pid < 0) { 20 | exit(-1); 21 | } else if (pid > 0) { 22 | exit(0); 23 | } 24 | 25 | setsid(); 26 | close(0); 27 | close(1); 28 | close(2); 29 | 30 | int fd0 = open("/dev/null", O_RDWR); 31 | int fd1 = dup(fd0); 32 | int fd2 = dup(fd0); 33 | 34 | 35 | } 36 | 37 | }; 38 | -------------------------------------------------------------------------------- /efnfw/base/ef_deamonize.h: -------------------------------------------------------------------------------- 1 | #ifndef EF_DEAMONIAE_H 2 | #define EF_DEAMONIAE_H 3 | 4 | namespace ef{ 5 | void daemonize(); 6 | }; 7 | 8 | #endif/**/ 9 | -------------------------------------------------------------------------------- /efnfw/base/ef_hex.h: -------------------------------------------------------------------------------- 1 | #ifndef __EF_HEX_H__ 2 | #define __EF_HEX_H__ 3 | 4 | #include "ef_btype.h" 5 | #include 6 | 7 | namespace ef{ 8 | 9 | 10 | char* byteToHex(uint8 c, char buf[2]); 11 | int hexToByte(char buf[2]); 12 | 13 | int32 bytesToHexs(const std::string& bytes, std::string& hex); 14 | int32 bytesToHexs(const char* bytes, int32 len, char* out, int32 outlen); 15 | int32 hexToBytes(const std::string& hex, std::string& bytes); 16 | int32 hexToBytes(const char* hex, int32 len, char* out, int32 outlen); 17 | 18 | int32 hexs_bytes_test(const std::string& input); 19 | 20 | }; 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /efnfw/base/ef_loader.h: -------------------------------------------------------------------------------- 1 | #ifndef __EF_LOADER_H__ 2 | #define __EF_LOADER_H__ 3 | 4 | #include "ef_thread.h" 5 | 6 | namespace ef{ 7 | 8 | template 9 | class DataSrc{ 10 | public: 11 | DataSrc():m_version(0){ 12 | mutexInit(&m_cs); 13 | } 14 | 15 | ~DataSrc(){ 16 | mutexDestroy(&m_cs); 17 | } 18 | 19 | int setData(const T& t){ 20 | AutoLock l(&m_cs); 21 | m_data = t; 22 | ++m_version; 23 | return m_version; 24 | } 25 | 26 | int getSersion() const{ 27 | return m_version; 28 | } 29 | 30 | void getData(T& t){ 31 | AutoLock l(&m_cs); 32 | t = m_data; 33 | } 34 | 35 | private: 36 | MUTEX m_cs; 37 | T m_data; 38 | volatile int m_version; 39 | }; 40 | 41 | template 42 | class Loader{ 43 | public: 44 | Loader(DataSrc* s = NULL):m_src(s), m_version(0){ 45 | } 46 | 47 | void setSrc(DataSrc* s){ 48 | m_version = 0; 49 | m_src = s; 50 | } 51 | 52 | bool loadData(){ 53 | int v = m_src->getSersion(); 54 | if(m_version < v){ 55 | m_src->getData(m_data); 56 | m_version = v; 57 | return true; 58 | } 59 | 60 | return false; 61 | } 62 | 63 | 64 | const T& getData() const{ 65 | return m_data; 66 | } 67 | 68 | private: 69 | DataSrc* m_src; 70 | T m_data; 71 | volatile int m_version; 72 | }; 73 | 74 | } 75 | 76 | #endif 77 | -------------------------------------------------------------------------------- /efnfw/base/ef_loop_buf.h: -------------------------------------------------------------------------------- 1 | #ifndef __EF_LOOP_BUF_H__ 2 | #define __EF_LOOP_BUF_H__ 3 | 4 | #include"ef_btype.h" 5 | 6 | namespace ef{ 7 | 8 | struct frame{ 9 | uint8 *m_buf; 10 | int32 m_cap; 11 | int32 m_size; 12 | int32 m_start; 13 | frame *m_next; 14 | int32 read(uint8 *buf, int32 len); 15 | int32 write(const uint8 *buf, int32 len); 16 | 17 | int32 peek(uint8 *buf, int32 len) const; 18 | 19 | int32 freeSize() const{ 20 | return m_cap - m_size - m_start; 21 | } 22 | 23 | int32 full() const{ 24 | return m_size + m_start == m_cap; 25 | } 26 | 27 | int32 clear(); 28 | }; 29 | 30 | class LoopBuf{ 31 | public: 32 | 33 | enum{ 34 | DEFAUTLT_ALIGN_SIZE = 2048, 35 | }; 36 | 37 | LoopBuf(); 38 | ~LoopBuf(); 39 | 40 | int32 capacity() const; 41 | int32 extend(int32 sz); 42 | //if buf is null, just drop data 43 | int32 read(uint8 *buf, int32 len); 44 | //if buf is null, just inc size 45 | int32 write(const uint8 *buf, int32 len); 46 | int32 autoResizeWrite(const uint8 *buf, int32 len); 47 | int32 peek(uint8 *buf, int32 len) const; 48 | int32 firstFrameLen() const; 49 | const uint8* firstFrameData() const; 50 | int32 freeFrameLen() const; 51 | uint8* freeFrameBuf(); 52 | int32 size() const{ 53 | return m_size; 54 | } 55 | 56 | int32 find(uint8 c) const; 57 | int32 clear(); 58 | 59 | private: 60 | 61 | int32 alignSize(int32 len); 62 | frame* constructFrame(uint8* buf, int32 len); 63 | frame* allocFrame(int32 len); 64 | 65 | frame* m_head; 66 | frame* m_w_pos; 67 | frame* m_tail; 68 | int32 m_size; 69 | }; 70 | 71 | }; 72 | 73 | 74 | #endif/**/ 75 | -------------------------------------------------------------------------------- /efnfw/base/ef_md5.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2004-2005 Sergey Lyubka 3 | * All rights reserved 4 | * 5 | * "THE BEER-WARE LICENSE" (Revision 42): 6 | * Sergey Lyubka wrote this file. As long as you retain this notice you 7 | * can do whatever you want with this stuff. If we meet some day, and you think 8 | * this stuff is worth it, you can buy me a beer in return. 9 | */ 10 | /**************************************************************** 11 | * 12 | * Thanks for Sergey Lynbka, I hope I could meet U and buy U a beer 13 | * 14 | * ***************************************************************/ 15 | 16 | #ifndef __EF_MD5_H__ 17 | #define __EF_MD5_H__ 18 | 19 | #include "ef_btype.h" 20 | #include 21 | 22 | namespace ef{ 23 | 24 | typedef struct MD5Context { 25 | uint32 buf[4]; 26 | uint32 bits[2]; 27 | uint8 in[64]; 28 | } MD5_CTX; 29 | 30 | void MD5Init(MD5_CTX *ctx); 31 | void MD5Update(MD5_CTX *ctx, uint8 const *buf, uint32 len); 32 | void MD5Final(uint8 digest[16], MD5_CTX *ctx); 33 | void MD5(uint8 digest[16], uint8 const *buf, uint32 len); 34 | void MD5Hex(std::string& hex, uint8 const *buf, uint32 len); 35 | 36 | int32 fileMD5 (char const * fname, uint8 * md5val); 37 | 38 | } 39 | 40 | #endif /*MD5_HEADER_INCLUDED */ 41 | -------------------------------------------------------------------------------- /efnfw/base/ef_no_copy.h: -------------------------------------------------------------------------------- 1 | #ifndef __EF_NO_COPY_H__ 2 | #define __EF_NO_COPY_H__ 3 | 4 | namespace ef{ 5 | 6 | /// base class to disable copy and asign construct 7 | class NoCopy 8 | { 9 | protected: 10 | NoCopy() {} 11 | virtual ~NoCopy() {} 12 | private: 13 | NoCopy(const NoCopy &); 14 | NoCopy &operator=(const NoCopy &); 15 | }; 16 | 17 | }; 18 | 19 | #endif 20 | 21 | -------------------------------------------------------------------------------- /efnfw/base/ef_singleton.h: -------------------------------------------------------------------------------- 1 | #ifndef __EF_SINGLETON_H__ 2 | #define __EF_SINGLETON_H__ 3 | 4 | #include 5 | #include 6 | 7 | #include "ef_no_copy.h" 8 | 9 | namespace ef{ 10 | 11 | template 12 | class Singleton : 13 | NoCopy 14 | { 15 | public: 16 | static T *instance() { 17 | pthread_once(&m_ponce, init); 18 | return m_ptr; 19 | } 20 | 21 | private: 22 | Singleton(); 23 | ~Singleton(); 24 | 25 | static void init() { 26 | m_ptr = new T(); 27 | ::atexit(destroy); 28 | } 29 | 30 | static void destroy() { 31 | delete m_ptr; 32 | } 33 | 34 | private: 35 | static pthread_once_t m_ponce; 36 | static T *m_ptr; 37 | }; 38 | 39 | template 40 | pthread_once_t Singleton::m_ponce = PTHREAD_ONCE_INIT; 41 | 42 | template 43 | T *Singleton::m_ptr = NULL; 44 | 45 | }; 46 | 47 | #endif 48 | 49 | -------------------------------------------------------------------------------- /efnfw/base/ef_statistic.h: -------------------------------------------------------------------------------- 1 | #ifndef __EF_STSTISTIC_H__ 2 | #define __EF_STSTISTIC_H__ 3 | 4 | #include 5 | #include 6 | 7 | namespace ef{ 8 | 9 | enum{ 10 | //10s out put 11 | DEFAULT_OUT_SPAN = 10 * 1000000, 12 | }; 13 | 14 | class TimeRecorder{ 15 | public: 16 | TimeRecorder(const std::string& op); 17 | ~TimeRecorder(); 18 | private: 19 | std::string m_op; 20 | timeval m_start; 21 | }; 22 | 23 | typedef int (*OUT_FUNC)(void* par, const std::string& l); 24 | 25 | int initStatistic(OUT_FUNC, void* par, int outspan = DEFAULT_OUT_SPAN); 26 | int uninitStatistic(); 27 | 28 | }; 29 | 30 | #endif/*EF_STSTISTIC_H*/ 31 | -------------------------------------------------------------------------------- /efnfw/base/ef_thread_pool.cpp: -------------------------------------------------------------------------------- 1 | #include "ef_thread_pool.h" 2 | 3 | namespace ef{ 4 | 5 | ThreadPool::ThreadPool(): 6 | m_run(false){ 7 | mutexInit(&m_cs); 8 | semInit(&m_sem, 0, 0x7FFFFFFF); 9 | } 10 | 11 | ThreadPool::~ThreadPool(){ 12 | stop(); 13 | semDestroy(&m_sem); 14 | mutexDestroy(&m_cs); 15 | } 16 | 17 | int32 ThreadPool::start(){ 18 | m_run = true; 19 | threadCreate(&m_thread, NULL, threadPoolProcess, this); 20 | return 0; 21 | } 22 | 23 | void* ThreadPool::threadPoolProcess(void* vpool){ 24 | ThreadPool* p = (ThreadPool*)vpool; 25 | 26 | while(true){ 27 | 28 | Task* t = p->getTask(); 29 | 30 | if(t){ 31 | t->run(); 32 | }else{ 33 | break; 34 | } 35 | 36 | } 37 | 38 | return 0; 39 | } 40 | 41 | 42 | Task* ThreadPool::getTask(){ 43 | 44 | semTake(&m_sem); 45 | 46 | AutoLock l(&m_cs); 47 | Task* t = NULL; 48 | 49 | int32 ret = m_tasks.read((uint8*)&t, sizeof(t)); 50 | 51 | if(ret < (int)sizeof(t)){ 52 | return NULL; 53 | } 54 | 55 | return t; 56 | } 57 | 58 | int32 ThreadPool::addTask(Task* t){ 59 | 60 | if(!m_run){ 61 | return -2; 62 | } 63 | 64 | 65 | AutoLock l(&m_cs); 66 | 67 | m_tasks.autoResizeWrite((const uint8*)&t, sizeof(t)); 68 | 69 | semGive(&m_sem); 70 | 71 | return 0; 72 | } 73 | 74 | int32 ThreadPool::stop(){ 75 | 76 | m_run = false; 77 | 78 | semGive(&m_sem); 79 | 80 | threadJoin(&m_thread); 81 | 82 | return 0; 83 | } 84 | 85 | } 86 | -------------------------------------------------------------------------------- /efnfw/base/ef_thread_pool.h: -------------------------------------------------------------------------------- 1 | #ifndef __EF_THREAD_POOL_H__ 2 | #define __EF_THREAD_POOL_H__ 3 | 4 | #include "ef_thread.h" 5 | #include "ef_loop_buf.h" 6 | 7 | namespace ef{ 8 | 9 | class Task{ 10 | public: 11 | virtual ~Task(){}; 12 | virtual int32 run() = 0; 13 | }; 14 | 15 | class ThreadPool{ 16 | public: 17 | ThreadPool(); 18 | ~ThreadPool(); 19 | 20 | int32 start(); 21 | int32 stop(); 22 | 23 | int32 addTask(Task* t); 24 | private: 25 | //if return NULL, thread pool exit 26 | Task* getTask(); 27 | static void* threadPoolProcess(void* vpool); 28 | 29 | int32 processOneTask(); 30 | 31 | int32 tryProcessAllTask(); 32 | 33 | bool m_run; 34 | THREADHANDLE m_thread; 35 | MUTEX m_cs; 36 | SEMAPHORE m_sem; 37 | LoopBuf m_tasks; 38 | }; 39 | 40 | }; 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /efnfw/base/ef_tsd_ptr.h: -------------------------------------------------------------------------------- 1 | #ifndef __EF_TSD_PTR_H__ 2 | #define __EF_TSD_PTR_H__ 3 | 4 | #include 5 | #include 6 | 7 | namespace ef{ 8 | 9 | /// deleter: do nothing 10 | inline void nullDeleter(void *ptr) {} 11 | 12 | /// deleter: use free ptr 13 | inline void mallocDeleter(void *ptr) { if (ptr) { free(ptr); } } 14 | 15 | /// deleter: use delete ptr 16 | template 17 | void newDeleter(void *ptr) { delete(T *)ptr; } 18 | 19 | /// deleter: use delete [] ptr; 20 | template 21 | void newArrayDeleter(void *ptr) { delete [](T *)ptr; } 22 | 23 | /// define deleter function type 24 | typedef void(*DeleterCB)(void *); 25 | 26 | /// Thread Specific Storage 27 | /// @param T pointer type tss stored 28 | template 29 | class TSDPtr 30 | { 31 | pthread_key_t m_pkey; 32 | DeleterCB m_d; 33 | 34 | TSDPtr(const TSDPtr &); 35 | TSDPtr &operator=(const TSDPtr &); 36 | public: 37 | /// @param d deleter, NewDeleter, 38 | /// NewArrayDeleter, MallocDeleter, NullDeleter 39 | explicit TSDPtr(DeleterCB d = newDeleter) 40 | : m_d(d) { pthread_key_create(&m_pkey, d); } 41 | ~TSDPtr() { 42 | T *tmp = release(); 43 | 44 | if (tmp) { m_d(tmp); } 45 | 46 | pthread_key_delete(m_pkey); 47 | } 48 | 49 | inline T *get() { return (T *)pthread_getspecific(m_pkey); } 50 | inline bool set(T *ptr) { return 0 == pthread_setspecific(m_pkey, ptr);} 51 | 52 | inline T *operator -> () { return get(); } 53 | inline T &operator * () { return *get(); } 54 | 55 | inline bool operator !() { return 0 == get(); } 56 | inline operator bool() { return 0 != get(); } 57 | 58 | T *release() { 59 | T *tmp = get(); 60 | set(0); 61 | return tmp; 62 | } 63 | 64 | void reset(T *ptr) { 65 | T *tmp = get(); 66 | 67 | if (tmp != ptr) { 68 | set(ptr); 69 | m_d(tmp); 70 | } 71 | } 72 | }; 73 | 74 | }; 75 | 76 | #endif 77 | 78 | -------------------------------------------------------------------------------- /efnfw/base/ef_utility.h: -------------------------------------------------------------------------------- 1 | #ifndef __EF_UTILITY_H__ 2 | #define __EF_UTILITY_H__ 3 | 4 | #include "ef_btype.h" 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | namespace ef{ 11 | 12 | int64 gettime_ms(); 13 | 14 | int64 tv_diff_ms(struct timeval t1, struct timeval t2); 15 | struct timeval tv_diff(struct timeval t1, struct timeval t2); 16 | 17 | int64 htonll(int64 l); 18 | 19 | int split(const std::string& str, std::vector& ret_, 20 | std::string sep = ","); 21 | 22 | std::string itostr(int64 i); 23 | 24 | void trim(std::string& str); 25 | 26 | int32 sleep_ms (int32 msec); 27 | }; 28 | 29 | #endif/*EF_UTILITY_H*/ 30 | 31 | -------------------------------------------------------------------------------- /efnfw/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | mkdir -p lib 3 | rm -fr CMakeCache.txt 4 | rm -fr CMakeFiles 5 | cmake . 6 | make clean;make 7 | rm -fr CMakeCache.txt 8 | rm -fr CMakeFiles 9 | -------------------------------------------------------------------------------- /efnfw/net/ef_acceptor.h: -------------------------------------------------------------------------------- 1 | #ifndef __EF_ACCEPTOR_H__ 2 | #define __EF_ACCEPTOR_H__ 3 | 4 | 5 | #include "ef_common.h" 6 | #include "ef_sock.h" 7 | #include "ef_device.h" 8 | 9 | namespace ef{ 10 | class EventLoop; 11 | class Connection; 12 | 13 | class ConnectionFactory{ 14 | public: 15 | virtual ~ConnectionFactory(){}; 16 | virtual Connection* createConnection(EventLoop* l, 17 | int32 fd, const std::string& addr, int32 port) = 0; 18 | }; 19 | 20 | 21 | class ConnectionDispatcher{ 22 | public: 23 | virtual ~ConnectionDispatcher(){} 24 | virtual int32 dispatchConnection(EventLoop* l, Connection* c) = 0; 25 | }; 26 | 27 | class Acceptor:public Device{ 28 | public: 29 | Acceptor(ConnectionFactory *confac = NULL, 30 | ConnectionDispatcher *dis = NULL); 31 | int32 setConnectionFactory(ConnectionFactory *confac); 32 | int32 setConnectionDispatcher(ConnectionDispatcher* condisp); 33 | virtual ~Acceptor(); 34 | 35 | int32 startListen(const char *addr, int32 port); 36 | int32 stopListen(); 37 | 38 | virtual Connection* acceptConnect(EventLoop* l); 39 | virtual int32 handleRead(EventLoop* l); 40 | private: 41 | ConnectionFactory* m_confac; 42 | ConnectionDispatcher* m_condispatch; 43 | }; 44 | }; 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /efnfw/net/ef_client.h: -------------------------------------------------------------------------------- 1 | #ifndef __EF_CLIENT_H__ 2 | #define __EF_CLIENT_H__ 3 | 4 | #include "ef_connection.h" 5 | 6 | namespace ef{ 7 | 8 | class Server; 9 | 10 | class Client:public Connection{ 11 | public: 12 | enum{ 13 | STATUS_INIT = 0, 14 | STATUS_CONNECTING = 1, 15 | STATUS_CONNECTED = 2, 16 | }; 17 | 18 | enum{ 19 | RECONNECT_TIMER = 101, 20 | KEEPALIVE_TIMER = 102, 21 | }; 22 | 23 | Client(); 24 | virtual ~Client(); 25 | int32 connectTo(const std::string& addr, int32 port); 26 | //if the span is 0, recycle when disconenct 27 | void setReconnectSpan(int32 span_ms); 28 | void setKeepAliveSpan(int32 span_ms); 29 | int32 reconnectSpan() const{ 30 | return m_reconnect_span; 31 | } 32 | int32 keepAliveSpan() const{ 33 | return m_keep_alive_span; 34 | } 35 | int32 disconnect(); 36 | 37 | int32 getStatus() const{ 38 | return m_status; 39 | } 40 | 41 | virtual int32 onConnected() = 0; 42 | virtual int32 onDisconnected() = 0; 43 | virtual int32 checkPackLen() = 0; 44 | virtual int32 keepAlive() = 0; 45 | virtual int32 onFail(); 46 | virtual int32 handlePacket(const std::string& p) = 0; 47 | 48 | //set id and dispatch to server,default maybe enough 49 | virtual int32 dispatchToServer(Server* s); 50 | virtual int32 onCreate(EventLoop* l); 51 | virtual int32 handleRead(EventLoop* l); 52 | virtual int32 handleWrite(EventLoop* l); 53 | virtual int32 handleTimer(EventLoop* l, int32 id); 54 | 55 | private: 56 | int32 reconnect(); 57 | int32 doConnect(const std::string& addr, int32 port); 58 | int32 handleConnected(EventLoop* l); 59 | 60 | int32 m_status; 61 | std::string m_addr; 62 | int32 m_port; 63 | int32 m_reconnect_span; 64 | int32 m_keep_alive_span; 65 | }; 66 | 67 | 68 | }; 69 | 70 | #endif 71 | -------------------------------------------------------------------------------- /efnfw/net/ef_common.h: -------------------------------------------------------------------------------- 1 | #ifndef EF_COMMON_H 2 | #define EF_COMMON_H 3 | 4 | #include "base/ef_btype.h" 5 | 6 | namespace ef{ 7 | 8 | enum{ 9 | READ_EVENT = 0x1, 10 | WRITE_EVENT= 0x2, 11 | TIMER_EVENT= 0x4, 12 | }; 13 | 14 | 15 | #ifdef PRINT_DETAIL_NET_LOG 16 | #define DETAIL_NET_LOG 1 17 | #else 18 | #define DETAIL_NET_LOG 0 19 | #endif 20 | }; 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /efnfw/net/ef_connector.h: -------------------------------------------------------------------------------- 1 | #ifndef EF_CONNECTOR_H 2 | #define EF_CONNECTOR_H 3 | 4 | #include "ef_common.h" 5 | #include "ef_sock.h" 6 | #include "ef_device.h" 7 | 8 | namespace ef{ 9 | 10 | class EventLoop; 11 | class Connection; 12 | class ConnectionFactory; 13 | 14 | class Connector:public Device{ 15 | public: 16 | private: 17 | Acceptor(ConnectionFactory *confac = NULL); 18 | virtual ~Acceptor(); 19 | }; 20 | 21 | }; 22 | 23 | #endif/*EF_CONNECTOR_H*/ 24 | -------------------------------------------------------------------------------- /efnfw/net/ef_device.cpp: -------------------------------------------------------------------------------- 1 | #include "ef_device.h" 2 | #include 3 | #include 4 | #include "ef_event_loop.h" 5 | #include "ef_net_log.h" 6 | #include "base/ef_utility.h" 7 | 8 | 9 | namespace ef{ 10 | 11 | Device::Device() 12 | :m_fd(EF_INVALID_SOCKET), m_noti(0) 13 | { 14 | 15 | } 16 | 17 | Device::~Device(){ 18 | clear(); 19 | } 20 | 21 | int32 Device::recycle(){ 22 | delete this; 23 | return 0; 24 | } 25 | 26 | int32 Device::clear(){ 27 | 28 | if(m_fd != EF_INVALID_SOCKET){ 29 | //NLogInfo << "close fd:" << m_fd; 30 | sock_close(m_fd); 31 | m_fd = EF_INVALID_SOCKET; 32 | 33 | } 34 | 35 | return 0; 36 | } 37 | 38 | 39 | int32 Device::addNotify(EventLoop* l, int32 noti){ 40 | int32 ret = 0; 41 | 42 | ret = l->addNotify(this, noti); 43 | if(ret < 0){ 44 | return ret; 45 | } 46 | m_noti = noti; 47 | return ret; 48 | } 49 | 50 | 51 | int32 Device::clearNotify(EventLoop* l){ 52 | int32 ret = 0; 53 | 54 | ret = l->clearNotify(this); 55 | if(ret < 0){ 56 | return ret; 57 | } 58 | m_noti = 0; 59 | return ret; 60 | } 61 | 62 | 63 | int32 Device::modifyNotify(EventLoop* l, int32 noti){ 64 | int32 ret = 0; 65 | 66 | ret = l->modifyNotify(this, noti); 67 | if(ret < 0){ 68 | return ret; 69 | } 70 | m_noti = noti; 71 | return ret; 72 | } 73 | 74 | 75 | }; 76 | 77 | 78 | -------------------------------------------------------------------------------- /efnfw/net/ef_device.h: -------------------------------------------------------------------------------- 1 | #ifndef __EF_DEVICE_H__ 2 | #define __EF_DEVICE_H__ 3 | 4 | #include 5 | #include "ef_common.h" 6 | #include "ef_sock.h" 7 | 8 | namespace ef{ 9 | 10 | class EventLoop; 11 | 12 | class Device{ 13 | public: 14 | Device(); 15 | virtual ~Device(); 16 | 17 | 18 | 19 | virtual int32 handleRead(EventLoop* l){ 20 | return 0; 21 | } 22 | virtual int32 handleWrite(EventLoop* l){ 23 | return 0; 24 | } 25 | virtual int32 recycle(); 26 | 27 | virtual int32 onFail(){ 28 | return recycle(); 29 | } 30 | 31 | void setFd(SOCKET fd){ 32 | m_fd = fd; 33 | } 34 | 35 | SOCKET getFd() const{ 36 | return m_fd; 37 | } 38 | 39 | int32 addNotify(EventLoop* l, int32 ev_type); 40 | int32 clearNotify(EventLoop* l); 41 | int32 modifyNotify(EventLoop* l, int32 ev_type); 42 | int32 getNotify() const{ 43 | return m_noti; 44 | } 45 | 46 | int32 shutDownRead(){ 47 | return shutdown(m_fd, 0); 48 | } 49 | 50 | int32 shutDownWrite(){ 51 | return shutdown(m_fd, 1); 52 | } 53 | 54 | int32 setAddr(const std::string& addr, int32 port){ 55 | m_addr = addr; 56 | m_port = port; 57 | return 0; 58 | } 59 | 60 | int32 getAddr(std::string& addr, int32 &port) const{ 61 | addr = m_addr; 62 | port = m_port; 63 | return 0; 64 | } 65 | 66 | const std::string& getIp() const{ 67 | return m_addr; 68 | } 69 | 70 | int32 getPort() const{ 71 | return m_port; 72 | } 73 | 74 | private: 75 | int32 clear(); 76 | SOCKET m_fd; 77 | std::string m_addr; 78 | int32 m_port; 79 | int32 m_noti; 80 | }; 81 | 82 | 83 | 84 | } 85 | 86 | 87 | #endif/*EF_DEVICE_H*/ 88 | -------------------------------------------------------------------------------- /efnfw/net/ef_net_log.cpp: -------------------------------------------------------------------------------- 1 | #include "ef_net_log.h" 2 | #include "ef_common.h" 3 | 4 | namespace ef{ 5 | static std::string g_nlognm = ""; 6 | 7 | 8 | void setNetLogName(const std::string& name){ 9 | g_nlognm = name; 10 | } 11 | 12 | const std::string& getNetLogName(){ 13 | return g_nlognm; 14 | } 15 | 16 | } 17 | 18 | -------------------------------------------------------------------------------- /efnfw/net/ef_net_log.h: -------------------------------------------------------------------------------- 1 | #ifndef __EF_NET_LOG_H__ 2 | #define __EF_NET_LOG_H__ 3 | 4 | #include "base/ef_btype.h" 5 | #include "base/ef_log.h" 6 | #include 7 | 8 | 9 | namespace ef{ 10 | 11 | void setNetLogName(const std::string& name); 12 | const std::string& getNetLogName(); 13 | 14 | 15 | #define getNetLog(level) getLog()(getNetLogName(), level) 16 | 17 | #define NLogDebug getNetLog(LOG_LEVEL_DEBUG) << "[PID=" \ 18 | << getpid() << "] [THREAD=" \ 19 | << pthread_self() << "] [" << __FILE__ << ":" \ 20 | << __LINE__ << "] [" << __FUNCTION__ << "] " 21 | #define NLogTrace getNetLog(LOG_LEVEL_TRACE) << "[PID=" \ 22 | << getpid() << "] [THREAD=" \ 23 | << pthread_self() << "] ["<< __FILE__ << ":" \ 24 | << __LINE__ << "] [" << __FUNCTION__ << "] " 25 | #define NLogInfo getNetLog(LOG_LEVEL_INFO) << "[PID=" \ 26 | << getpid() << "] [THREAD=" \ 27 | << pthread_self() << "] [" << __FILE__ << ":" \ 28 | << __LINE__ << "] [" << __FUNCTION__ << "] " 29 | #define NLogWarn getNetLog(LOG_LEVEL_WARN) << "[PID=" \ 30 | << getpid() << "] [THREAD=" \ 31 | << pthread_self() << "] [" << __FILE__ << ":" \ 32 | << __LINE__ << "] [" << __FUNCTION__ << "] " 33 | #define NLogError getNetLog(LOG_LEVEL_ERROR) << "[PID=" \ 34 | << getpid() << "] [THREAD=" \ 35 | << pthread_self() << "] [" << __FILE__ << ":" \ 36 | << __LINE__ << "] [" << __FUNCTION__ << "] " 37 | 38 | } 39 | 40 | #endif/*EF_NET_LOG_H*/ 41 | -------------------------------------------------------------------------------- /efnfw/net/ef_net_settings.cpp: -------------------------------------------------------------------------------- 1 | #include "ef_net_settings.h" 2 | 3 | namespace ef{ 4 | 5 | int32 NetSettings::epollSize = DEFAULT_EPOLL_SIZE; 6 | int32 NetSettings::procEventCnt = DEFAULT_PROC_EVENT_CNT; 7 | int32 NetSettings::procTimerCnt = DEFAULT_PROC_EVENT_CNT; 8 | 9 | }; 10 | -------------------------------------------------------------------------------- /efnfw/net/ef_net_settings.h: -------------------------------------------------------------------------------- 1 | #ifndef EF_NET_SETTINGS_H 2 | #define EF_NET_SETTINGS_H 3 | 4 | #include "ef_common.h" 5 | namespace ef{ 6 | 7 | enum{ 8 | DEFAULT_EPOLL_SIZE = 65536, 9 | DEFAULT_PROC_EVENT_CNT = 16, 10 | MAX_PROC_EVENT_CNT = 1024, 11 | DEFAULT_PROC_TIMER_CNT = 32, 12 | MAX_PROC_TIMER_CNT = 2048, 13 | }; 14 | 15 | class NetSettings{ 16 | public: 17 | static int32 epollSize; 18 | static int32 procEventCnt; 19 | static int32 procTimerCnt; 20 | }; 21 | 22 | } 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /efnfw/net/ef_operator.cpp: -------------------------------------------------------------------------------- 1 | #include "ef_operator.h" 2 | #include "ef_connection.h" 3 | #include "ef_event_loop.h" 4 | 5 | namespace ef{ 6 | 7 | 8 | CloseConnectionOp::CloseConnectionOp(int32 conid) 9 | :m_conid(conid){ 10 | 11 | } 12 | 13 | int32 CloseConnectionOp::process(EventLoop *thr){ 14 | return thr->closeConnection(m_conid); 15 | } 16 | 17 | int32 CloseAllConnectionsOp::process(EventLoop *thr){ 18 | return thr->closeAllConnections(); 19 | } 20 | 21 | SendMessageOp::SendMessageOp(int32 conid, const std::string& msg) 22 | :m_conid(conid), m_msg(msg){ 23 | 24 | } 25 | 26 | int32 SendMessageOp::process(EventLoop *thr){ 27 | return thr->sendMessage(m_conid, m_msg); 28 | } 29 | 30 | 31 | int32 AddConnectionOp::process(EventLoop *thr){ 32 | return thr->addConnection(m_c); 33 | } 34 | 35 | }; 36 | 37 | -------------------------------------------------------------------------------- /efnfw/net/ef_operator.h: -------------------------------------------------------------------------------- 1 | #ifndef __EF_OPERATOR_H__ 2 | #define __EF_OPERATOR_H__ 3 | 4 | #include "ef_common.h" 5 | #include 6 | 7 | namespace ef{ 8 | class EventLoop; 9 | class Connection; 10 | 11 | class NetOperator 12 | { 13 | public: 14 | virtual ~NetOperator(){ 15 | 16 | } 17 | 18 | virtual int32 process(EventLoop *thr){ 19 | return 0; 20 | } 21 | 22 | }; 23 | 24 | class CloseConnectionOp:public NetOperator 25 | { 26 | public: 27 | CloseConnectionOp(int32 conid); 28 | 29 | virtual int32 process(EventLoop *thr); 30 | 31 | protected: 32 | private: 33 | int32 m_conid; 34 | }; 35 | 36 | class CloseAllConnectionsOp:public NetOperator 37 | { 38 | public: 39 | virtual int32 process(EventLoop *thr); 40 | protected: 41 | private: 42 | }; 43 | 44 | 45 | class SendMessageOp:public NetOperator 46 | { 47 | public: 48 | SendMessageOp(int32 conid, const std::string& msg); 49 | virtual int32 process(EventLoop *thr); 50 | 51 | protected: 52 | private: 53 | int32 m_conid; 54 | std::string m_msg; 55 | }; 56 | 57 | 58 | class AddConnectionOp:public NetOperator 59 | { 60 | public: 61 | AddConnectionOp(Connection* c) 62 | :m_c(c){ 63 | } 64 | virtual int32 process(EventLoop *thr); 65 | private: 66 | Connection* m_c; 67 | }; 68 | 69 | 70 | }; 71 | 72 | #endif/*EF_OPERATOR_H*/ 73 | 74 | -------------------------------------------------------------------------------- /efnfw/net/ef_server.h: -------------------------------------------------------------------------------- 1 | #ifndef __EF_SERVER_H__ 2 | #define __EF_SERVER_H__ 3 | 4 | 5 | #include 6 | #include "ef_connection.h" 7 | #include "ef_event_loop.h" 8 | #include "ef_acceptor.h" 9 | #include "base/ef_auto_ptr.h" 10 | 11 | 12 | namespace ef{ 13 | 14 | class Server; 15 | 16 | class GDispatcher:public ConnectionDispatcher{ 17 | public: 18 | GDispatcher(Server* s):m_s(s){} 19 | int32 dispatchConnection(EventLoop*l, Connection* c); 20 | private: 21 | Server* m_s; 22 | }; 23 | 24 | 25 | class Server{ 26 | public: 27 | enum{ 28 | DEFAULT_THREAD_COUNT = 64, 29 | }; 30 | 31 | Server(int32 thread_count = DEFAULT_THREAD_COUNT) 32 | :m_thread_cnt(thread_count), 33 | m_default_disp(new GDispatcher(this)) 34 | { 35 | } 36 | 37 | virtual ~Server(); 38 | 39 | 40 | void setEventLoopCount(int32 cnt){ 41 | if(cnt <= 0) 42 | cnt = 1; 43 | m_thread_cnt = cnt; 44 | } 45 | 46 | int32 getEventLoopCount(){ 47 | return m_thread_cnt; 48 | } 49 | 50 | EventLoop& getEventLoop(int32 idx){ 51 | return m_nthreads[idx]; 52 | } 53 | 54 | EventLoop& getAcceptEventLoop(){ 55 | return m_athread; 56 | } 57 | 58 | int32 init(); 59 | int32 startListen(int32 port, ConnectionFactory*, 60 | ConnectionDispatcher* d = NULL); 61 | int32 stopListen(int32 port); 62 | int32 run(); 63 | int32 stop(); 64 | int32 clear(); 65 | 66 | private: 67 | int32 m_thread_cnt; 68 | std::map > m_acceptors; 69 | EventLoop m_athread; 70 | THREADHANDLE m_athreadid; 71 | ArrayRefCntPtr m_nthreads; 72 | ArrayRefCntPtr m_nthread_ids; 73 | ConnectionDispatcher* m_default_disp; 74 | }; 75 | 76 | 77 | 78 | } 79 | 80 | #endif 81 | -------------------------------------------------------------------------------- /efnfw/net/ef_sock.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmxiaocong/gim/f9bac9be3d2afdc9bea69d201d1ce3c10a56639e/efnfw/net/ef_sock.cpp -------------------------------------------------------------------------------- /efnfw/net/ef_timer.cpp: -------------------------------------------------------------------------------- 1 | #include "ef_timer.h" 2 | #include "ef_connection.h" 3 | 4 | namespace ef{ 5 | 6 | Timer::Timer(time_tv timeouttime) 7 | :m_timeouttime(timeouttime) 8 | { 9 | 10 | } 11 | 12 | Timer::~Timer(){ 13 | 14 | } 15 | 16 | void Timer::setTimeOutAfter(int32 ms){ 17 | timeval tv; 18 | gettimeofday(&tv, NULL); 19 | tv.tv_sec += ms / 1000; 20 | tv.tv_usec += ms % 1000 * 1000; 21 | tv.tv_sec += tv.tv_usec / 1000000; 22 | tv.tv_usec = tv.tv_usec % 1000000; 23 | setTimeoutTime(tv); 24 | } 25 | 26 | ConnectionTimer::ConnectionTimer(Connection* con, 27 | int32 id) 28 | :m_con(con), m_id(id), m_status(STATUS_INIT){ 29 | } 30 | 31 | int32 ConnectionTimer::timeout(EventLoop* l){ 32 | m_status = STATUS_STOP; 33 | return m_con->handleTimer(l, m_id); 34 | } 35 | 36 | }; 37 | 38 | -------------------------------------------------------------------------------- /img/jiagou.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmxiaocong/gim/f9bac9be3d2afdc9bea69d201d1ce3c10a56639e/img/jiagou.png -------------------------------------------------------------------------------- /logic_server/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.6) 2 | project(logic_server) 3 | set(CMAKE_VERBOSE_MAKEFILE ON) 4 | add_definitions("-Wall -pg -g") 5 | include_directories(../common/include ../efnfw) 6 | link_directories(/usr/local/lib ../common/lib/ ../efnfw/lib) 7 | link_libraries(libcommon.a libefnfw.a hiredis 8 | protobuf json pthread) 9 | AUX_SOURCE_DIRECTORY(. SRC_LIST) 10 | set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib) 11 | add_library(logic_server STATIC ${SRC_LIST}) 12 | SET_TARGET_PROPERTIES(logic_server PROPERTIES LINKER_LANGUAGE C) 13 | -------------------------------------------------------------------------------- /logic_server/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | 4 | cp -fr ../proto/* . 5 | 6 | protoc pair.proto --cpp_out=. -I=. 7 | mv -f pair.pb.cc pair.pb.cpp 8 | 9 | protoc connect_server.proto --cpp_out=. -I=. 10 | mv -f connect_server.pb.cc connect_server.pb.cpp 11 | 12 | rm -fr CMakeCache.txt 13 | rm -fr CMakeFiles 14 | rm -fr cmake_install.cmake 15 | 16 | mkdir -p lib 17 | 18 | cmake . 19 | make clean;make 20 | rm -fr CMakeCache.txt 21 | rm -fr CMakeFiles 22 | rm -fr cmake_install.cmake 23 | -------------------------------------------------------------------------------- /logic_server/dispatcher.h: -------------------------------------------------------------------------------- 1 | #ifndef __DISPATCHER_H__ 2 | #define __DISPATCHER_H__ 3 | 4 | #include 5 | #include 6 | #include "sess_cache.h" 7 | #include "base/ef_loader.h" 8 | #include "server_status.h" 9 | 10 | namespace ef{ 11 | class EventLoop; 12 | } 13 | 14 | namespace gim{ 15 | 16 | typedef std::map StatusMap; 17 | typedef ef::DataSrc DtSrc; 18 | typedef ef::Loader Loader; 19 | 20 | class SvCon; 21 | class LogicServer; 22 | class ServiceRequest; 23 | class ServiceResponse; 24 | class RPCClient; 25 | 26 | enum{ 27 | CID_OFFLINE = -20001, 28 | CONNECT_SERVER_OFFLINE = -20101, 29 | SEND_FAIL = -20201, 30 | }; 31 | 32 | class Dispatcher{ 33 | public: 34 | 35 | Dispatcher(LogicServer* s, ef::EventLoop* l, DtSrc* src); 36 | 37 | int init(void* par); 38 | 39 | ~Dispatcher(); 40 | 41 | int addConnectServer(int svid, SvCon* con); 42 | int delConnectServer(int svid); 43 | SvCon* getConnectServer(int svid); 44 | 45 | int sendToClient(const std::string& cid, const ServiceRequest& req); 46 | int sendToClient(const std::string& cid, const ServiceResponse& resp); 47 | int checkConnectServers(); 48 | 49 | int callService(const ServiceRequest& req, ServiceResponse& resp, 50 | int timeout_ms = 100); 51 | 52 | private: 53 | typedef std::pair CliSess; 54 | int getClientLoginServer(const std::string& cid, std::vector& svs); 55 | int connectServer(const ServerStatus& s); 56 | int connectIPArray(SvCon* c, const std::vector& a, int port); 57 | int reinitRPCClient(const StatusMap& m); 58 | int reinitRPCClient(const ServerStatus& s); 59 | 60 | Loader m_loader; 61 | std::map m_svcons; 62 | LogicServer* m_sv; 63 | ef::EventLoop* m_evlp; 64 | SessCache* m_sesscache; 65 | RPCClient* m_rpc_cli; 66 | }; 67 | 68 | 69 | }; 70 | 71 | #endif 72 | -------------------------------------------------------------------------------- /logic_server/logic_common.h: -------------------------------------------------------------------------------- 1 | #ifndef LOGIC_COMMON_H 2 | #define LOGIC_COMMON_H 3 | 4 | #include 5 | #include "base/ef_btype.h" 6 | #include "base/ef_log.h" 7 | #include "base/ef_utility.h" 8 | 9 | namespace ef{ 10 | class EventLoop; 11 | class Connection; 12 | }; 13 | 14 | namespace gim{ 15 | class head; 16 | using namespace ef; 17 | 18 | int32 constructPacket(const head& h, 19 | const std::string& body, std::string& pack); 20 | 21 | int32 decorationName(int32 svid, int32 conid, 22 | const std::string& n, std::string& dn); 23 | 24 | int32 getDecorationInfo(const std::string& dn, int32& svid, 25 | int32& conid, std::string& n); 26 | 27 | int32 getSvTypeFromSessid(const std::string& ssid, int32& svtype); 28 | 29 | 30 | #define ALogTrace(a) logTrace(a) << "[" << __FUNCTION__\ 31 | << "] addr[" << getIp() << ":"\ 32 | << getPort() << "] " 33 | #define ALogError(a) logError(a) << "[" << __FUNCTION__\ 34 | << "] addr[" << getIp() << ":"\ 35 | << getPort() << "] " 36 | 37 | #define PLogError(a) ALogError(a) << " " 38 | #define PLogTrace(a) ALogTrace(a) << " " 39 | 40 | }; 41 | 42 | #endif/*LOGIC_COMMON_H*/ 43 | -------------------------------------------------------------------------------- /logic_server/rpc_client.h: -------------------------------------------------------------------------------- 1 | #ifndef __RPC_CLIENT_H__ 2 | #define __RPC_CLIENT_H__ 3 | 4 | #include "net/ef_sock.h" 5 | 6 | namespace gim{ 7 | 8 | class ServiceRequest; 9 | class ServiceResponse; 10 | 11 | //typedef int (*ResponseCallBack)(void* par, const ServiceResponse& resp); 12 | 13 | class RPCClient{ 14 | public: 15 | RPCClient(); 16 | ~RPCClient(); 17 | int init(const std::string& ip, int port, int retrytime = 3); 18 | int callService(const ServiceRequest& req, ServiceResponse& resp, 19 | int timeout_ms = 100); 20 | int disconnect(); 21 | 22 | private: 23 | int doRecv(int& cmd, std::string& msg, int timeout_ms = 100); 24 | int doSend(int cmd, const std::string& req, int timeout_ms = 100); 25 | int doCallService(const ServiceRequest& req, ServiceResponse& resp, 26 | int timeout_ms = 100); 27 | 28 | int login(); 29 | int reconnect(); 30 | std::string m_server_ip; 31 | int m_server_port; 32 | ef::SOCKET m_sock; 33 | std::string m_sessid; 34 | std::string m_sn; 35 | int m_retrytime; 36 | }; 37 | 38 | }; 39 | 40 | 41 | #endif/*__RPC_CLIENT_H__*/ 42 | -------------------------------------------------------------------------------- /logic_test/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project(ConnectServer) 2 | set(CMAKE_VERBOSE_MAKEFILE ON) 3 | add_definitions("-Wall -pg -g") 4 | include_directories(${PROJECT_SOURCE_DIR}/../logic_server ${PROJECT_SOURCE_DIR}/../common/include ${PROJECT_SOURCE_DIR}/../efnfw) 5 | link_directories(/usr/local/lib ../common/lib/ ../efnfw/lib ../logic_server/lib) 6 | link_libraries(liblogic_server.a libcommon.a libefnfw.a hiredis zookeeper_mt protobuf jsoncpp pthread) 7 | file(GLOB_RECURSE SRC_LIST src/*.cpp) 8 | add_executable(bin/ConnectServer ${SRC_LIST}) 9 | -------------------------------------------------------------------------------- /logic_test/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | cd ../efnfw; 4 | sh build.sh 5 | cd - 6 | 7 | cd ../common; 8 | sh build.sh 9 | cd - 10 | 11 | cd ../logic_server; 12 | sh build.sh 13 | cd - 14 | 15 | 16 | mkdir -p bin 17 | 18 | rm -fr CMakeCache.txt 19 | rm -fr CMakeFiles 20 | 21 | 22 | cmake . 23 | make clean;make 24 | 25 | rm -fr CMakeCache.txt 26 | rm -fr CMakeFiles 27 | -------------------------------------------------------------------------------- /logic_test/etc/elog.conf: -------------------------------------------------------------------------------- 1 | { 2 | "Appenders":[ { "Name":"console_appender", "Type":"ConsoleAppender"}, 3 | { "Name":"file_appender", "Type":"FileAppender", "Path":"/opt/log/connect_server/all", "ScheduSpan":"HOUR", "ImmediatelyFlush":"false"} 4 | ], 5 | "Logs": [{ "Name":"console_logger", "Level":"ALL", "Appender":"console_appender" }, 6 | { "Name":"file_logger", "Level":"ALL", "Appender":"file_appender"}, 7 | { "Name":"file_logger1", "Level":"ALL", "Appender":"file_appender"} 8 | ], 9 | "DefaultLog":{"Level":"ALL", "Appender":"console_appender"} 10 | } 11 | -------------------------------------------------------------------------------- /logic_test/etc/settings.conf: -------------------------------------------------------------------------------- 1 | { 2 | "Daemon":1, 3 | "Type":0, 4 | 5 | "ThreadCount":2, 6 | 7 | "NetLogName":"ConnectNetLog", 8 | 9 | "CliConfigs": 10 | [{"Enc":0, "AliveMs":300000, 11 | "MinType":-1000, "MaxType":1, 12 | "ListenPort": 3000, "MaxReqQueSize": 0, 13 | "MaxPackCntPerMin": 0, "StartThreadIdx": 0, 14 | "ThreadCnt": 1}, 15 | 16 | {"Enc":0, "AliveMs":300000, 17 | "MinType":1, "MaxType":1000, 18 | "ListenPort": 3000, "MaxReqQueSize": 10000, 19 | "MaxPackCntPerMin": 0, "StartThreadIdx": 1, 20 | "ThreadCnt": 1} 21 | ], 22 | 23 | 24 | "SessCacheConfig": 25 | { 26 | "Type":"DefSessCache", 27 | "Config":{ 28 | "Expire":30, 29 | "DBGroup": 30 | { 31 | "NodeCfgs":[{"ipaddr":"127.0.0.1", "port":6379, "passwd":""} ] 32 | } 33 | } 34 | } 35 | 36 | 37 | } 38 | -------------------------------------------------------------------------------- /logic_test/src/test_conn.h: -------------------------------------------------------------------------------- 1 | #ifndef __TEST_CONN_H__ 2 | #define __TEST_CONN_H__ 3 | 4 | #include "server_conn.h" 5 | 6 | namespace gim{ 7 | 8 | class TestConn:public SvCon{ 9 | public: 10 | virtual int handleServiceRequest(const ServiceRequest& req); 11 | virtual int handleServiceResponse(const ServiceResponse& resp); 12 | }; 13 | 14 | 15 | class TestConnFact:public SvConFactory{ 16 | public: 17 | virtual SvCon* createSvCon(void* par); 18 | }; 19 | 20 | }; 21 | 22 | #endif/*__TEST_CONN_H__*/ 23 | -------------------------------------------------------------------------------- /proto/connect_server.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | import "pair.proto"; 4 | 5 | message LoginRequest{ 6 | required string id = 1;//id of user or logic server 7 | optional int32 type = 2;//type 0:client, > 0:logic server 8 | optional string version = 3;//client version 9 | optional string token = 4; //use for check 10 | repeated Pair kvs = 5; 11 | } 12 | 13 | 14 | message LoginResponse{ 15 | required int32 status = 1;//if 0 success, else fail 16 | optional string sessid = 2;//the session id, 17 | optional int64 server_time = 3;//server timestamp 18 | } 19 | 20 | message Address{ 21 | optional string ip = 1; 22 | optional int32 port = 2; 23 | } 24 | 25 | message RedirectResponse{ 26 | required int32 status = 1;//if 0 success, else fail 27 | repeated Address addrs = 2; 28 | } 29 | 30 | 31 | message ServiceRequest{ 32 | optional string from_sessid = 1;//from sessid 33 | optional string to_sessid = 2;//to sessid 34 | required int32 from_type = 3;// 100:push 200:peer 35 | required int32 to_type = 4;// 100:push 200:peer 36 | required string sn = 5;//each req has an unique sn 37 | optional bytes payload = 6;// payload 38 | } 39 | 40 | message ServiceResponse{ 41 | optional string from_sessid = 1;//from sessid 42 | optional string to_sessid = 2; //to sessid 43 | required int32 from_type = 3; 44 | required int32 to_type = 4; 45 | required string sn = 5;//each req has an unique sn 46 | required int32 status = 6;// 0 success 47 | optional bytes payload = 7; 48 | } 49 | 50 | message KickCliRequest{ 51 | optional string sessid = 1; 52 | optional string id = 2; 53 | optional int32 type = 3; 54 | optional bytes reason = 4; 55 | } 56 | -------------------------------------------------------------------------------- /proto/err_no.h: -------------------------------------------------------------------------------- 1 | #ifndef __ERR_NO_H__ 2 | #define __ERR_NO_H__ 3 | 4 | 5 | namespace gim{ 6 | 7 | 8 | enum{ 9 | STATUS_OK = 0, 10 | INPUT_FORMAT_ERROR = -1, 11 | CREATE_SESSION_FAIL = -10, 12 | GET_KEY_FAIL = -11, 13 | CHECK_TOKEN_FAIL = -12, 14 | NO_ENC_UNSUPPORT = -13, 15 | INVALID_SESSION_ID = -20, 16 | SESSION_TIMEOUT = -21, 17 | MISS_TO_SESSION_ID = -22, 18 | NO_SERVICE = -30, 19 | SERVICE_TOO_BUSY = -33, 20 | CALL_TIMEOUT = -34, 21 | INVALID_SN = -40, 22 | SN_TIMEOUT = -41, 23 | DECRYPT_FAIL = -50, 24 | INVALID_TYPE = -60, 25 | SERVICE_TYPE_ERROR = -70, 26 | ASSESS_FORBIDDEN = -80, 27 | INNER_ERROR = -100, 28 | }; 29 | 30 | inline const char* getErrStr(int e){ 31 | switch(e){ 32 | case STATUS_OK: 33 | return "STATUS_OK"; 34 | case INPUT_FORMAT_ERROR: 35 | return "INPUT_FORMAT_ERROR"; 36 | case CREATE_SESSION_FAIL: 37 | return "CREATE_SESSION_FAIL"; 38 | case GET_KEY_FAIL: 39 | return "GET_USER_KEY_FAIL"; 40 | case CHECK_TOKEN_FAIL: 41 | return "CHECK_TOKEN_FAIL"; 42 | case NO_ENC_UNSUPPORT: 43 | return "NO_ENC_UNSUPPORT"; 44 | case INVALID_SESSION_ID: 45 | return "INVALID_SESSION_ID"; 46 | case MISS_TO_SESSION_ID: 47 | return "MISS_TO_SESSION_ID"; 48 | case NO_SERVICE: 49 | return "NO_SERVICE"; 50 | case SESSION_TIMEOUT: 51 | return "SESSION_TIMEOUT"; 52 | case SERVICE_TOO_BUSY: 53 | return "SERVICE_TOO_BUSY"; 54 | case INVALID_SN: 55 | return "INVALID_SN"; 56 | case SN_TIMEOUT: 57 | return "SN_TIMEOUT"; 58 | case INVALID_TYPE: 59 | return "INVALID_TYPE"; 60 | case DECRYPT_FAIL: 61 | return "DECRYPT_FAIL"; 62 | case SERVICE_TYPE_ERROR: 63 | return "SERVICE_TYPE_ERROR"; 64 | case ASSESS_FORBIDDEN: 65 | return "ASSESS_FORBIDDEN"; 66 | case CALL_TIMEOUT: 67 | return "CALL_TIMEOUT"; 68 | case INNER_ERROR: 69 | return "INNER_ERROR"; 70 | } 71 | if(e >= 0) 72 | return "STATUS_OK"; 73 | return "UNKNOW_ERROR"; 74 | } 75 | 76 | }; 77 | 78 | 79 | #endif 80 | -------------------------------------------------------------------------------- /proto/message.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | 4 | message Message{ 5 | optional string to = 1; 6 | optional int64 id = 2;//mssage id, did not set this field when send message 7 | optional int64 time = 3;//message send time 8 | optional string from = 4;//who send 9 | optional int32 type = 5; 10 | optional string sn = 6; 11 | optional bytes data = 7;//message data 12 | optional int64 expire = 8; //expire time 13 | } 14 | 15 | 16 | -------------------------------------------------------------------------------- /proto/msg_head.h: -------------------------------------------------------------------------------- 1 | #ifndef __MSG_HEAD_H__ 2 | #define __MSG_HEAD_H__ 3 | 4 | #include "base/ef_btype.h" 5 | 6 | namespace gim{ 7 | 8 | using namespace ef; 9 | 10 | enum{ 11 | KEEPALIVE_REQ = 0, 12 | KEEPALIVE_RESP = KEEPALIVE_REQ + 1, 13 | LOGIN_REQ = 100, 14 | LOGIN_RESP = LOGIN_REQ + 1, 15 | SERVICE_REQ = 200, 16 | SERVICE_RESP = SERVICE_REQ + 1, 17 | KICK_CLIENT = 400, 18 | REDIRECT_RESP = 1001, 19 | SET_TIME_RESP = 1101, 20 | MAGIC_NUMBER = 0x20141228, 21 | }; 22 | 23 | struct head{ 24 | int32 magic; 25 | int32 len;//include head 26 | //0: keepalive, 1: keepalive resp 27 | //100: login, 101: login resp 28 | //200: service, 201: service resp 29 | //300: regist service 301: regist service resp 30 | //400: kick client 31 | //1001: redericet 32 | int32 cmd; 33 | }; 34 | 35 | 36 | }; 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /proto/pair.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | message Pair{ 4 | required string key = 1; 5 | optional bytes value = 2; 6 | } 7 | -------------------------------------------------------------------------------- /proto/peer_server.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | import "message.proto"; 4 | 5 | message GetPeerMessageRequest{ 6 | optional string cid = 1;//to who 7 | optional int64 start_msgid = 2;//mssage id 8 | optional int64 count = 3;//message count 9 | } 10 | 11 | message GetPeerMessageResponse{ 12 | optional int64 last_msgid = 1; 13 | repeated Message msgs = 2; 14 | } 15 | 16 | message SendPeerMessageRequest{ 17 | required Message msg = 1; 18 | } 19 | 20 | //use for muti-point online 21 | message SendPeerMessageResponse{ 22 | required Message msg = 1; 23 | } 24 | 25 | message RecvPeerMessageResponse{ 26 | required Message msg = 1; 27 | } 28 | 29 | message PushMessageRequest{ 30 | required string sn = 1; 31 | required Message msg = 2; 32 | } 33 | 34 | message PushMessageResponse{ 35 | required string sn = 1; 36 | required int32 status = 2; 37 | } 38 | 39 | 40 | message PeerPacket{ 41 | required int32 cmd = 1;//110: send_req, 42 | //111: send_resp, 112: get_req, 43 | //113: get_resp, 115:recv_resp 44 | optional GetPeerMessageRequest get_peer_msg_req = 2; 45 | optional GetPeerMessageResponse get_peer_msg_resp = 3; 46 | optional SendPeerMessageRequest send_peer_msg_req = 4; 47 | optional SendPeerMessageResponse send_peer_msg_resp = 5; 48 | optional RecvPeerMessageResponse recv_peer_msg_resp = 6; 49 | } 50 | -------------------------------------------------------------------------------- /proto/session.proto: -------------------------------------------------------------------------------- 1 | package gim; 2 | 3 | import "pair.proto"; 4 | 5 | message Sess{ 6 | optional string id = 1; 7 | optional int32 type = 2; 8 | optional int64 lasttime = 3; 9 | optional string sessid = 4; 10 | optional int32 consvid = 5; 11 | optional string version = 6; 12 | repeated Pair kvs = 7; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /push_server/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project(ConnectServer) 2 | set(CMAKE_VERBOSE_MAKEFILE ON) 3 | add_definitions("-Wall -pg -g") 4 | include_directories(${PROJECT_SOURCE_DIR}/../logic_server ${PROJECT_SOURCE_DIR}/../common/include ${PROJECT_SOURCE_DIR}/../efnfw) 5 | link_directories(/usr/local/lib ../common/lib/ ../efnfw/lib ../logic_server/lib) 6 | link_libraries(liblogic_server.a libcommon.a libefnfw.a hiredis zookeeper_mt protobuf jsoncpp pthread) 7 | file(GLOB_RECURSE SRC_LIST src/*.cpp) 8 | add_executable(bin/PushServer ${SRC_LIST}) 9 | -------------------------------------------------------------------------------- /push_server/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | 4 | cp -fr ../proto src/ 5 | 6 | protoc src/proto/connect_server.proto --cpp_out=src/proto -I=src/proto 7 | 8 | mv src/proto/connect_server.pb.cc src/proto/connect_server.pb.cpp 9 | 10 | rm -fr CMakeCache.txt 11 | rm -fr CMakeFiles 12 | rm -fr cmake_install.cmake 13 | 14 | mkdir -p lib 15 | 16 | cmake . 17 | make clean;make 18 | rm -fr CMakeCache.txt 19 | rm -fr CMakeFiles 20 | rm -fr cmake_install.cmake 21 | -------------------------------------------------------------------------------- /push_server/etc/elog.conf: -------------------------------------------------------------------------------- 1 | { 2 | "Appenders": [ 3 | { 4 | "Name": "console_appender", 5 | "Type": "ConsoleAppender" 6 | }, 7 | { 8 | "Name": "net_appender", 9 | "Type": "FileAppender", 10 | "Path": "/data/log/push_server/net", 11 | "ScheduSpan": "HOUR", 12 | "ImmediatelyFlush": "false" 13 | }, 14 | { 15 | "Name": "all_appender", 16 | "Type": "FileAppender", 17 | "Path": "/data/log/push_server/all", 18 | "ScheduSpan": "HOUR", 19 | "ImmediatelyFlush": "false" 20 | } 21 | ], 22 | "Logs": [ 23 | { 24 | "Name": "console_logger", 25 | "Level": "ALL", 26 | "Appender": "console_appender" 27 | }, 28 | { 29 | "Name": "PushServerNet", 30 | "Level": "ALL", 31 | "Appender": "net_appender" 32 | }, 33 | { 34 | "Name": "PushServer", 35 | "Level": "ALL", 36 | "Appender": "all_appender" 37 | } 38 | ], 39 | "DefaultLog": { 40 | "Level": "ALL", 41 | "Appender": "console_appender" 42 | } 43 | } -------------------------------------------------------------------------------- /push_server/run.sh: -------------------------------------------------------------------------------- 1 | ./bin/PushServer -p /PushConfig -s /PushStatus -e /ConnectStatus -z 10.105.12.208:2181,10.105.1.202:2181,10.247.41.169:2181 -i 9527 -t 100 -l etc/elog.conf -P 11147 2 | -------------------------------------------------------------------------------- /push_server/src/push_common.h: -------------------------------------------------------------------------------- 1 | #ifndef _PUSH_COMMON_H_ 2 | #define _PUSH_COMMON_H_ 3 | #include "base/ef_log.h" 4 | #include 5 | #include "push_def.h" 6 | #include "base/ef_btype.h" 7 | #include 8 | #include "message.pb.h" 9 | #include 10 | 11 | namespace gim{ 12 | using namespace ef; 13 | 14 | typedef struct _JsonArgs 15 | { 16 | enum{ 17 | ARGEND = 0x0147FFFF, 18 | ARGMAX = 10 19 | }; 20 | _JsonArgs(const std::string& k, ...); 21 | 22 | std::string key; 23 | std::vector types; 24 | }JsonArg; 25 | 26 | 27 | bool pushRequestJsonCheck(const Json::Value& v); 28 | 29 | void messageJsonToProtobuf(const Json::Value& v, Message& msg); 30 | void messageProtobufToJson(const Message& gm, Json::Value& vm); 31 | 32 | int64 strtoi64(const std::string& src); 33 | 34 | #define MSG_KEY(cid) (std::string("PM_") + cid) 35 | #define LAST_MSG_ID_KEY(cid) (std::string("PM_") + cid + "_last_msgid") 36 | 37 | #define PSLogTrace(a) ef::logTrace(a) << " " 39 | 40 | #define PSLogError(a) ef::logError(a) << " " 42 | 43 | 44 | } 45 | #endif //_PUSH_COMMON_H_ -------------------------------------------------------------------------------- /push_server/src/push_def.h: -------------------------------------------------------------------------------- 1 | #ifndef _PUSH_DEF_H_ 2 | #define _PUSH_DEF_H_ 3 | namespace gim{ 4 | enum{ 5 | PS_RESULT_OK = 0, 6 | PS_RESULT_ERROR = -1, 7 | PS_RESULT_JSON_ERROR = -2, 8 | PS_RESULT_ARGS_ERROR = -3, 9 | PS_RESULT_UNDEFINED_CMD = -4, 10 | PS_RESULT_INCR_MSGID_ERROR = -5, 11 | PS_RESULT_REDIS_ERROR = -6 12 | }; 13 | 14 | enum{ 15 | PS_CMD_CALL_PUSH = 11, //call push to send msg to client 16 | PS_CMD_CALL_PUSH_RESP = 12, 17 | 18 | PS_CMD_PUSH_REQUEST = 1, //push msg 19 | PS_CMD_PUSH_RESPONSE = 2, //push msg response 20 | PS_CMD_GET_PUSH_MSG = 3, 21 | PS_CMD_GET_PUSH_MSG_RESPONSE=4, 22 | PS_CMD_PEER_REQUEST = 5, 23 | PS_CMD_PEER_RESPONSE = 6 24 | }; 25 | } 26 | #endif //_PUSH_DEF_H_ -------------------------------------------------------------------------------- /push_server/src/push_msg_db.cpp: -------------------------------------------------------------------------------- 1 | #include "push_msg_db.h" 2 | #include "base/ef_tsd_ptr.h" 3 | #include 4 | 5 | namespace gim{ 6 | 7 | ef::TSDPtr g_pushMQ; 8 | Json::Value PushMsgDBM::g_push_db_cfg; 9 | 10 | void PushMsgDBM::initConfig(const Json::Value& conf){ 11 | g_push_db_cfg = conf; 12 | } 13 | 14 | RedisMQ* PushMsgDBM::MQ(){ 15 | RedisMQ* r = g_pushMQ.get(); 16 | if (!r){ 17 | std::cout << "MQ config:/n" << g_push_db_cfg.toStyledString() << std::endl; 18 | r = new RedisMQ(g_push_db_cfg); 19 | g_pushMQ.set(r); 20 | } 21 | 22 | return r; 23 | } 24 | } -------------------------------------------------------------------------------- /push_server/src/push_msg_db.h: -------------------------------------------------------------------------------- 1 | #ifndef _PUSH_MSG_DB_H_ 2 | #define _PUSH_MSG_DB_H_ 3 | #include 4 | #include "base/ef_btype.h" 5 | #include 6 | #include "redis_msg_q.h" 7 | 8 | namespace gim 9 | { 10 | class PushMsgDBM 11 | { 12 | public: 13 | static void initConfig(const Json::Value& conf); 14 | static RedisMQ* MQ(); 15 | private: 16 | static Json::Value g_push_db_cfg; 17 | }; 18 | } 19 | 20 | #endif -------------------------------------------------------------------------------- /push_server/src/push_server_conn.h: -------------------------------------------------------------------------------- 1 | #ifndef _PUSH_CONN_H_ 2 | #define _PUSH_CONN_H_ 3 | #include "server_conn.h" 4 | #include 5 | #include "push_def.h" 6 | #include "push_common.h" 7 | 8 | namespace gim{ 9 | class PushSrvConn:public SvCon{ 10 | public: 11 | virtual int handleServiceRequest(const ServiceRequest& req); 12 | virtual int handleServiceResponse(const ServiceResponse& resp); 13 | private: 14 | int handleGetMsgs(const ServiceRequest& req, const Json::Value& jargs); 15 | int sendJ2C(const Message& m); 16 | int handlePeer(const ServiceRequest& req, const Json::Value& jargs); 17 | }; 18 | 19 | class PushSrvConnFactory:public SvConFactory{ 20 | public: 21 | virtual SvCon* createSvCon(void* par){ 22 | return new PushSrvConn(); 23 | } 24 | }; 25 | 26 | } 27 | #endif //end of _PUSH_CONN_H_ -------------------------------------------------------------------------------- /thirdparty/jsoncpp-src-0.5.0.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmxiaocong/gim/f9bac9be3d2afdc9bea69d201d1ce3c10a56639e/thirdparty/jsoncpp-src-0.5.0.tar.gz -------------------------------------------------------------------------------- /thirdparty/scons-2.1.0.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmxiaocong/gim/f9bac9be3d2afdc9bea69d201d1ce3c10a56639e/thirdparty/scons-2.1.0.tar.gz --------------------------------------------------------------------------------