├── .DS_Store ├── README.md ├── inc ├── .DS_Store ├── obd_protocol.h ├── macro_define.h ├── autozi_data_handler.h ├── obd_server_engine.h ├── Singleton.h ├── jmutexautolock.h ├── jmutex.h ├── CThread.h ├── obd_client_session.h ├── jthread.h ├── InformationCore.h └── TypeDefine.h ├── src ├── .DS_Store ├── obd_server_engine.cpp ├── obd_client_session.cpp ├── Main.cpp ├── autozi_data_handler.cpp ├── jmutex.cpp ├── jthread.cpp └── InformationCore.cpp └── Makefile /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/garywlx/obd/HEAD/.DS_Store -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # obd 2 | “车载诊断系统(On-Board Diagnostic)” 数据采集,采用C++编写 3 | -------------------------------------------------------------------------------- /inc/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/garywlx/obd/HEAD/inc/.DS_Store -------------------------------------------------------------------------------- /src/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/garywlx/obd/HEAD/src/.DS_Store -------------------------------------------------------------------------------- /inc/obd_protocol.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/garywlx/obd/HEAD/inc/obd_protocol.h -------------------------------------------------------------------------------- /src/obd_server_engine.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/garywlx/obd/HEAD/src/obd_server_engine.cpp -------------------------------------------------------------------------------- /src/obd_client_session.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/garywlx/obd/HEAD/src/obd_client_session.cpp -------------------------------------------------------------------------------- /inc/macro_define.h: -------------------------------------------------------------------------------- 1 | #ifndef _MACRO_DEFINE_H_ 2 | #define _MACRO_DEFINE_H_ 3 | 4 | const size_t SOCKET_RECEIVE_BUFFER = 1024 * 64; 5 | const size_t SOCKET_SEND_BUFFER = 1024 * 64; 6 | 7 | const size_t MAX_LOGBUF_LEN = 1024; 8 | const size_t BUFFER_LEN = 1024; 9 | 10 | const size_t STRINE_LENGHT_256 = 256; 11 | #define __FILENAME__ (strrchr(__FILE__, '/') ? (strrchr(__FILE__, '/') + 1):__FILE__) 12 | 13 | 14 | #endif //_MACRO_DEFINE_H_ 15 | 16 | 17 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | CXX = g++ 2 | CXXFLAGS = -Wall -O2 -m32 3 | TARGET = obd_server 4 | SRCDIR= src 5 | OBJDIR= obj 6 | BINDIR= bin 7 | DSTDIR= /obd 8 | INCDIR= -Iinc -I./inc -I/usr/include 9 | LIBS= -L/usr/local/lib -levent -levent_pthreads -lpthread 10 | 11 | $(shell install -d $(BINDIR) $(OBJDIR)) 12 | 13 | SRC= $(wildcard $(SRCDIR)/*.cpp) 14 | OBJ= $(SRC:$(SRCDIR)/%.cpp=$(OBJDIR)/%.o) 15 | BIN= $(BINDIR)/$(TARGET) 16 | 17 | $(TARGET) : $(OBJ) 18 | @echo 'Creating binary file: $(BIN)...' 19 | @$(CXX) $(CXXFLAGS) $(INCDIR) $(LIBS) -o $(BIN) $(OBJ) 20 | @echo 'Done.' 21 | 22 | $(OBJDIR)/%.o: $(SRCDIR)/%.cpp 23 | @echo 'Compiling object file: $@...' 24 | @$(CXX) $(CXXFLAGS) $(INCDIR) $(LIBS) -c -o $@ $< 25 | 26 | install: 27 | cp $(BIN) $(DSTDIR)/$(TARGET) 28 | 29 | .PHONY: clean 30 | clean: 31 | @echo 'Removing all objects...' 32 | @rm -f $(OBJ) $(BIN) $(DSTDIR)/$(TARGET) 33 | @echo 'Done.' 34 | -------------------------------------------------------------------------------- /inc/autozi_data_handler.h: -------------------------------------------------------------------------------- 1 | #ifndef AUTOZI_DATA_HANDlER_H_ 2 | #define AUTOZI_DATA_HANDlER_H_ 3 | 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | #include "CThread.h" 9 | 10 | 11 | typedef std::deque DEQ_AUTOZI_DATA; 12 | 13 | class obd_client_session; 14 | 15 | class autozi_data_handler : public CThread 16 | { 17 | public: 18 | autozi_data_handler(void); 19 | ~autozi_data_handler(void); 20 | 21 | int push_file_name( std::string file_name); 22 | void set_autozi_client(obd_client_session * ptr); 23 | bool start_process_data_service(); 24 | bool stop_process_data_service(); 25 | bool is_running_service(); 26 | protected: 27 | virtual UINT ThreadFunc() ; 28 | 29 | private: 30 | CCriticalSectionEx m_cs; 31 | DEQ_AUTOZI_DATA deq_autozi_data_; 32 | obd_client_session *autozi_client_; 33 | bool is_running_; 34 | }; 35 | 36 | extern autozi_data_handler *s_autozi_data_handler; 37 | #define sAutoziClientHandler (*s_autozi_data_handler) 38 | #endif 39 | 40 | -------------------------------------------------------------------------------- /inc/obd_server_engine.h: -------------------------------------------------------------------------------- 1 | #ifndef OBD_SERVER_ENGINE_ 2 | #define OBD_SERVER_ENGINE_ 3 | 4 | #include "Singleton.h" 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include "macro_define.h" 13 | #include 14 | #include 15 | #include "CThread.h" 16 | 17 | 18 | class obd_client_session; 19 | 20 | class obd_server_engine : public CThread 21 | , public Singleton 22 | { 23 | 24 | public: 25 | obd_server_engine(void); 26 | ~obd_server_engine(void); 27 | 28 | bool initialize_server_engine(); 29 | bool uninitialize_server_engine(); 30 | 31 | bool start_server(unsigned short port); 32 | bool stop_server(); 33 | 34 | bool set_send_buffer(SOCKET fd, int sz); 35 | bool set_receive_buffer(SOCKET fd, int sz); 36 | 37 | void accept_notify(obd_client_session *cli); 38 | 39 | protected: 40 | virtual UINT ThreadFunc(); 41 | 42 | private: 43 | struct event_base *event_base_ptr_; 44 | bool is_initialized_; 45 | }; 46 | 47 | #define sObdServerEngine obd_server_engine::getSingleton() 48 | //#define sObdServerEnginePtr (void *)obd_server_engine::getSingletonPtr() 49 | 50 | #endif //OBD_SERVER_ENGINE_ 51 | 52 | -------------------------------------------------------------------------------- /inc/Singleton.h: -------------------------------------------------------------------------------- 1 | #ifndef _SINGLETON_H_ 2 | #define _SINGLETON_H_ 3 | 4 | #include "TypeDefine.h" 5 | 6 | /// Should be placed in the appropriate .cpp file somewhere 7 | #define initialiseSingleton( type ) \ 8 | template <> type * Singleton < type > :: mSingleton = 0 9 | 10 | /// To be used as a replacement for initialiseSingleton( ) 11 | /// Creates a file-scoped Singleton object, to be retrieved with getSingleton 12 | #define createFileSingleton( type ) \ 13 | initialiseSingleton( type ); \ 14 | type the##type 15 | 16 | template < class type > 17 | 18 | class Singleton { 19 | public: 20 | /// Constructor 21 | Singleton( ) { 22 | /// If you hit this assert, this singleton already exists -- you can't create another one! 23 | ASSERT( mSingleton == 0 ); 24 | mSingleton = static_cast(this); 25 | } 26 | /// Destructor 27 | virtual ~Singleton( ) { 28 | mSingleton = 0; 29 | } 30 | 31 | /// Retrieve the singleton object, if you hit this assert this singleton object doesn't exist yet 32 | static type & getSingleton( ) { ASSERT( mSingleton ); return *mSingleton; } 33 | 34 | /// Retrieve a pointer to the singleton object 35 | static type * getSingletonPtr( ) { return mSingleton; } 36 | 37 | protected: 38 | 39 | /// Singleton pointer, must be set to 0 prior to creating the object 40 | static type * mSingleton; 41 | }; 42 | 43 | #endif -------------------------------------------------------------------------------- /src/Main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "InformationCore.h" 3 | #include "obd_server_engine.h" 4 | #include "autozi_data_handler.h" 5 | 6 | int main(int argc, char *argv[]) 7 | { 8 | sInfoCore.app_log_info("=====================OBD_SERVER===============================\n"); 9 | new obd_server_engine(); 10 | bool ret = sObdServerEngine.initialize_server_engine(); 11 | if(! ret) { 12 | sInfoCore.app_log_debug(NULL, "Initialize server engine failed...\n"); 13 | return -1; 14 | } 15 | sInfoCore.app_log_info("Initialize server engine Finished...\n"); 16 | 17 | s_autozi_data_handler = new autozi_data_handler(); 18 | if(s_autozi_data_handler == NULL) 19 | { 20 | sInfoCore.app_log_debug(NULL, "Initialize autozi service error... \n"); 21 | return -1; 22 | } 23 | 24 | sInfoCore.app_log_info("Initialize autozi process data service Finished...\n"); 25 | if(!sAutoziClientHandler.start_process_data_service()) 26 | { 27 | sInfoCore.app_log_debug(NULL, "Start autozi service error... \n"); 28 | return -1; 29 | 30 | } 31 | sInfoCore.app_log_info("Start autozi process data service Successfully...\n"); 32 | 33 | sInfoCore.app_log_info("Start server engine ...\n"); 34 | ret = sObdServerEngine.start_server(8090); 35 | if(! ret) { 36 | sInfoCore.app_log_debug(NULL, "Start server engine failed...\n"); 37 | return -1; 38 | } 39 | sInfoCore.app_log_info("Start server engine Finished...\n"); 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /inc/jmutexautolock.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | This file is a part of the JThread package, which contains some object- 4 | oriented thread wrappers for different thread implementations. 5 | 6 | Copyright (c) 2000-2006 Jori Liesenborgs (jori.liesenborgs@gmail.com) 7 | 8 | Permission is hereby granted, free of charge, to any person obtaining a 9 | copy of this software and associated documentation files (the "Software"), 10 | to deal in the Software without restriction, including without limitation 11 | the rights to use, copy, modify, merge, publish, distribute, sublicense, 12 | and/or sell copies of the Software, and to permit persons to whom the 13 | Software is furnished to do so, subject to the following conditions: 14 | 15 | The above copyright notice and this permission notice shall be included in 16 | all copies or substantial portions of the Software. 17 | 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 21 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 23 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 24 | DEALINGS IN THE SOFTWARE. 25 | 26 | */ 27 | 28 | #ifndef JMUTEXAUTOLOCK_H 29 | 30 | #define JMUTEXAUTOLOCK_H 31 | 32 | #include "jmutex.h" 33 | 34 | class JMutexAutoLock 35 | { 36 | public: 37 | JMutexAutoLock(JMutex &m) : mutex(m) { mutex.Lock(); } 38 | ~JMutexAutoLock() { mutex.Unlock(); } 39 | private: 40 | JMutex &mutex; 41 | }; 42 | 43 | #endif // JMUTEXAUTOLOCK_H 44 | -------------------------------------------------------------------------------- /src/autozi_data_handler.cpp: -------------------------------------------------------------------------------- 1 | #include "autozi_data_handler.h" 2 | #include "InformationCore.h" 3 | #include "obd_client_session.h" 4 | autozi_data_handler *s_autozi_data_handler = NULL; 5 | autozi_data_handler::autozi_data_handler(void) 6 | { 7 | autozi_client_ = NULL; 8 | deq_autozi_data_.clear(); 9 | is_running_ = false; 10 | } 11 | 12 | autozi_data_handler::~autozi_data_handler(void) 13 | { 14 | } 15 | 16 | UINT autozi_data_handler::ThreadFunc() 17 | { 18 | std::string file_name; 19 | while(!IsThreadCanceled() && is_running_) 20 | { 21 | __lock(m_cs); 22 | if(autozi_client_ == NULL) 23 | { 24 | Sleep(1000*10); 25 | deq_autozi_data_.clear(); 26 | } 27 | else 28 | { 29 | if (!deq_autozi_data_.empty()) 30 | { 31 | file_name = deq_autozi_data_.front(); 32 | deq_autozi_data_.pop_front(); 33 | autozi_client_->send_buffer((const void *)(file_name.c_str()), file_name.size()); 34 | } 35 | else 36 | { 37 | Sleep(50); 38 | } 39 | } 40 | Sleep(50); 41 | } 42 | return 0; 43 | } 44 | 45 | int autozi_data_handler::push_file_name( std::string file_name) 46 | { 47 | sInfoCore.app_log_info("push file_name [%s] to queue...\n", file_name.c_str()); 48 | __lock(m_cs); 49 | 50 | if (NULL == autozi_client_) 51 | return deq_autozi_data_.size(); 52 | 53 | if (deq_autozi_data_.size() > 20) 54 | { 55 | return deq_autozi_data_.size(); 56 | } 57 | 58 | deq_autozi_data_.push_back(file_name); 59 | 60 | return deq_autozi_data_.size(); 61 | } 62 | 63 | void autozi_data_handler::set_autozi_client(obd_client_session * ptr) 64 | { 65 | autozi_client_ = ptr; 66 | } 67 | 68 | bool autozi_data_handler::start_process_data_service() 69 | { 70 | is_running_ = true; 71 | return (0==Start("AutoziThread")); 72 | } 73 | bool autozi_data_handler::stop_process_data_service() 74 | { 75 | is_running_ = false; 76 | Stop(); 77 | return true; 78 | } 79 | 80 | bool autozi_data_handler::is_running_service() 81 | { 82 | return (is_running_ && IsThreadCanceled()); 83 | } -------------------------------------------------------------------------------- /src/jmutex.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | This file is a part of the JThread package, which contains some object- 4 | oriented thread wrappers for different thread implementations. 5 | 6 | Copyright (c) 2000-2006 Jori Liesenborgs (jori.liesenborgs@gmail.com) 7 | 8 | Permission is hereby granted, free of charge, to any person obtaining a 9 | copy of this software and associated documentation files (the "Software"), 10 | to deal in the Software without restriction, including without limitation 11 | the rights to use, copy, modify, merge, publish, distribute, sublicense, 12 | and/or sell copies of the Software, and to permit persons to whom the 13 | Software is furnished to do so, subject to the following conditions: 14 | 15 | The above copyright notice and this permission notice shall be included in 16 | all copies or substantial portions of the Software. 17 | 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 21 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 23 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 24 | DEALINGS IN THE SOFTWARE. 25 | 26 | */ 27 | 28 | #include "jmutex.h" 29 | 30 | JMutex::JMutex() 31 | { 32 | initialized = false; 33 | } 34 | 35 | JMutex::~JMutex() 36 | { 37 | if (initialized) 38 | pthread_mutex_destroy(&mutex); 39 | } 40 | 41 | int JMutex::Init() 42 | { 43 | if (initialized) 44 | return ERR_JMUTEX_ALREADYINIT; 45 | 46 | pthread_mutex_init(&mutex,NULL); 47 | initialized = true; 48 | return 0; 49 | } 50 | 51 | int JMutex::Lock() 52 | { 53 | if (!initialized) 54 | return ERR_JMUTEX_NOTINIT; 55 | 56 | pthread_mutex_lock(&mutex); 57 | return 0; 58 | } 59 | 60 | int JMutex::Unlock() 61 | { 62 | if (!initialized) 63 | return ERR_JMUTEX_NOTINIT; 64 | 65 | pthread_mutex_unlock(&mutex); 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /inc/jmutex.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | This file is a part of the JThread package, which contains some object- 4 | oriented thread wrappers for different thread implementations. 5 | 6 | Copyright (c) 2000-2006 Jori Liesenborgs (jori.liesenborgs@gmail.com) 7 | 8 | Permission is hereby granted, free of charge, to any person obtaining a 9 | copy of this software and associated documentation files (the "Software"), 10 | to deal in the Software without restriction, including without limitation 11 | the rights to use, copy, modify, merge, publish, distribute, sublicense, 12 | and/or sell copies of the Software, and to permit persons to whom the 13 | Software is furnished to do so, subject to the following conditions: 14 | 15 | The above copyright notice and this permission notice shall be included in 16 | all copies or substantial portions of the Software. 17 | 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 21 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 23 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 24 | DEALINGS IN THE SOFTWARE. 25 | 26 | */ 27 | 28 | #ifndef JMUTEX_H 29 | 30 | #define JMUTEX_H 31 | 32 | #if (defined(WIN32) || defined(_WIN32_WCE)) 33 | #ifndef _WIN32_WCE 34 | #include 35 | #endif // _WIN32_WCE 36 | #include 37 | #include 38 | 39 | #define JMUTEX_CRITICALSECTION 40 | #else // using pthread 41 | #include 42 | #endif // WIN32 43 | 44 | #define ERR_JMUTEX_ALREADYINIT -1 45 | #define ERR_JMUTEX_NOTINIT -2 46 | #define ERR_JMUTEX_CANTCREATEMUTEX -3 47 | 48 | class JMutex 49 | { 50 | public: 51 | JMutex(); 52 | ~JMutex(); 53 | int Init(); 54 | int Lock(); 55 | int Unlock(); 56 | bool IsInitialized() { return initialized; } 57 | protected: 58 | #if (defined(WIN32) || defined(_WIN32_WCE)) 59 | #ifdef JMUTEX_CRITICALSECTION 60 | CRITICAL_SECTION mutex; 61 | #else // Use standard mutex 62 | HANDLE mutex; 63 | #endif // JMUTEX_CRITICALSECTION 64 | #else // pthread mutex 65 | pthread_mutex_t mutex; 66 | #endif // WIN32 67 | bool initialized; 68 | }; 69 | 70 | #endif // JMUTEX_H 71 | -------------------------------------------------------------------------------- /inc/CThread.h: -------------------------------------------------------------------------------- 1 | #ifndef _THREAD_H 2 | #define _THREAD_H 3 | 4 | #include 5 | #include "jthread.h" 6 | #include "TypeDefine.h" 7 | 8 | #pragma pack(push,8) 9 | typedef struct tagTHREADNAME_INFO 10 | { 11 | DWORD dwType; // Must be 0x1000. 12 | LPCSTR szName; // Pointer to name (in user addr space). 13 | DWORD dwThreadID; // Thread ID (-1=caller thread). 14 | DWORD dwFlags; // Reserved for future use, must be zero. 15 | } THREADNAME_INFO; 16 | #pragma pack(pop) 17 | 18 | #ifndef WIN32 19 | #define EXCEPTION_EXECUTE_HANDLER 1 20 | #endif 21 | class CThread : public JThread 22 | { 23 | public: 24 | CThread() 25 | { 26 | m_bCanceled = false; 27 | m_szThreadName[0] = 0; 28 | } 29 | 30 | #define MS_VC_EXCEPTION 0x406D1388 31 | 32 | 33 | void SetThreadName( LPCTSTR threadName,DWORD dwThreadID) 34 | { 35 | Sleep(10); 36 | THREADNAME_INFO info; 37 | info.dwType = 0x1000; 38 | info.szName = threadName; 39 | info.dwThreadID = dwThreadID; 40 | info.dwFlags = 0; 41 | 42 | // __try 43 | // { 44 | // RaiseException( MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info ); 45 | // } 46 | // __except(EXCEPTION_EXECUTE_HANDLER) 47 | // { 48 | // } 49 | } 50 | 51 | virtual bool IsThreadCanceled() const 52 | { 53 | return m_bCanceled; 54 | } 55 | virtual void CancelThread() 56 | { 57 | m_bCanceled = true; 58 | } 59 | virtual void *Thread() 60 | { 61 | ThreadStarted(); 62 | 63 | if (strlen(m_szThreadName) > 0 ) 64 | { 65 | SetThreadName(m_szThreadName,getpid()); 66 | } 67 | 68 | return (void*)ThreadFunc(); 69 | } 70 | int Start(LPCTSTR lpName = NULL) 71 | { 72 | printf("CThread Start"); 73 | if (lpName != NULL) 74 | { 75 | strcpy(m_szThreadName,lpName); 76 | } 77 | return JThread::Start(); 78 | } 79 | void Stop(int nWaitSeconds = 10) 80 | { 81 | m_bCanceled = true; 82 | 83 | for ( int i = 0 ; i < nWaitSeconds * 1000/100; ++i) 84 | { 85 | if (IsRunning()) 86 | { 87 | Sleep(100); 88 | } 89 | else 90 | { 91 | break; 92 | } 93 | } 94 | Kill(); 95 | } 96 | 97 | 98 | virtual UINT ThreadFunc() = 0; 99 | 100 | protected: 101 | bool m_bCanceled; 102 | TCHAR m_szThreadName[128]; 103 | }; 104 | 105 | unsigned int _beginthread (void (* _StartAddress) (void *),unsigned int _StackSize, void * _ArgList); 106 | 107 | 108 | #endif 109 | -------------------------------------------------------------------------------- /inc/obd_client_session.h: -------------------------------------------------------------------------------- 1 | #ifndef OBD_CLIENT_SESSION_H_ 2 | #define OBD_CLIENT_SESSION_H_ 3 | #include "InformationCore.h" 4 | #include 5 | using namespace std; 6 | #include "macro_define.h" 7 | #include "obd_protocol.h" 8 | 9 | const char CLIENT_PASSWORD[] = "JBTCARAPP"; 10 | 11 | void client_read_cb(struct bufferevent *bev, void *ctx); 12 | void client_write_cb(struct bufferevent *bev, void *ctx); 13 | void client_event_cb(struct bufferevent *bev, short events, void *ctx); 14 | 15 | class obd_client_session 16 | { 17 | public: 18 | obd_client_session(void); 19 | ~obd_client_session(void); 20 | 21 | bool initialize_client_info(ListenerInfo * pInfo, struct bufferevent *pEvBuffer, struct sockaddr *pClientAddr); 22 | 23 | 24 | void read_notify(); 25 | void connect_notify(); 26 | void error_notify(); 27 | void disconnect_notify(); 28 | void timeout_notify(); 29 | 30 | bool disconnect(); 31 | void pro_data(char * data_, size_t bytes_pro); 32 | void put_half_pkg(char * pkg_data_, const size_t pkg_len_); 33 | void get_all_data(char * all_data_, size_t & all_byte_, const char * new_data_, const size_t new_byte_); 34 | bool is_have_pkg(); 35 | void remove_half_pkg(); 36 | bool get_client_live() {return is_keep_live_;} 37 | 38 | bool send_buffer(const void* buf, size_t sz); 39 | 40 | void pro_message(tagRequestInfo_t *pkg, size_t sz); 41 | 42 | void login_handle(tagLogin_t * pkg); 43 | bool login_reply_handle(LOGIN_REPLY_STATE state); 44 | void uplaod_file_head_request_handle(tagUploadFileHead_t *pkg); 45 | void upload_file_data_request_handle(tagUploadFileData_t *pkg); 46 | //IDLE_CONNECT_REQUEST 47 | void idle_connect_request_handle(); 48 | void logout_request_handle(); 49 | 50 | bool is_client_logined() { return is_logined_;} 51 | 52 | std::string save_data_file(const char * data, size_t sz); 53 | 54 | inline size_t generate_file_index() 55 | { 56 | static size_t iNo = 0; 57 | 58 | ++iNo; 59 | return iNo; 60 | } 61 | private: 62 | ListenerInfo *listener_info_ptr_; 63 | CCriticalSectionEx event_buffer_lock_; 64 | struct bufferevent *event_buffer_ptr_; 65 | struct event_base *event_base_ptr_; 66 | 67 | std::string client_ip_; 68 | unsigned short client_port_; 69 | 70 | size_t half_len_; 71 | char half_pkg_[SOCKET_RECEIVE_BUFFER]; 72 | char *all_buffer_data_; 73 | bool is_keep_live_; 74 | 75 | bool is_logined_; 76 | std::string series_numuber_; 77 | size_t verify_current_file_size_; 78 | }; 79 | 80 | #endif //OBD_CLIENT_SESSION_H_ 81 | 82 | 83 | -------------------------------------------------------------------------------- /inc/jthread.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | This file is a part of the JThread package, which contains some object- 4 | oriented thread wrappers for different thread implementations. 5 | 6 | Copyright (c) 2000-2006 Jori Liesenborgs (jori.liesenborgs@gmail.com) 7 | 8 | Permission is hereby granted, free of charge, to any person obtaining a 9 | copy of this software and associated documentation files (the "Software"), 10 | to deal in the Software without restriction, including without limitation 11 | the rights to use, copy, modify, merge, publish, distribute, sublicense, 12 | and/or sell copies of the Software, and to permit persons to whom the 13 | Software is furnished to do so, subject to the following conditions: 14 | 15 | The above copyright notice and this permission notice shall be included in 16 | all copies or substantial portions of the Software. 17 | 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 21 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 23 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 24 | DEALINGS IN THE SOFTWARE. 25 | 26 | */ 27 | 28 | #ifndef JTHREAD_H 29 | 30 | #define JTHREAD_H 31 | 32 | #include "jmutex.h" 33 | 34 | #define ERR_JTHREAD_CANTINITMUTEX -1 35 | #define ERR_JTHREAD_CANTSTARTTHREAD -2 36 | #define ERR_JTHREAD_THREADFUNCNOTSET -3 37 | #define ERR_JTHREAD_NOTRUNNING -4 38 | #define ERR_JTHREAD_ALREADYRUNNING -5 39 | 40 | class JThread 41 | { 42 | public: 43 | JThread(); 44 | virtual ~JThread(); 45 | int Start(); 46 | int Kill(); 47 | virtual void *Thread() = 0; 48 | bool IsRunning(); 49 | void *GetReturnValue(); 50 | protected: 51 | void ThreadStarted(); 52 | private: 53 | 54 | #if (defined(WIN32) || defined(_WIN32_WCE)) 55 | #ifdef _WIN32_WCE 56 | DWORD threadid; 57 | static DWORD WINAPI TheThread(void *param); 58 | #else 59 | static UINT __stdcall TheThread(void *param); 60 | UINT threadid; 61 | #endif // _WIN32_WCE 62 | HANDLE threadhandle; 63 | #else // pthread type threads 64 | static void *TheThread(void *param); 65 | 66 | pthread_t threadid; 67 | #endif // WIN32 68 | void *retval; 69 | bool running; 70 | 71 | JMutex runningmutex; 72 | JMutex continuemutex,continuemutex2; 73 | bool mutexinit; 74 | }; 75 | 76 | #endif // JTHREAD_H 77 | 78 | -------------------------------------------------------------------------------- /inc/InformationCore.h: -------------------------------------------------------------------------------- 1 | #ifndef _INFOMATION_CORE_H_ 2 | #define _INFOMATION_CORE_H_ 3 | #include "TypeDefine.h" 4 | #include "Singleton.h" 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include "macro_define.h" 16 | using namespace std; 17 | 18 | class obd_client_session; 19 | struct tagListenerInfo_t { 20 | tagListenerInfo_t (){ 21 | pServer = NULL; 22 | pListener = NULL; 23 | ZeroM (sin); 24 | } 25 | void * pServer; 26 | struct evconnlistener* pListener; 27 | struct sockaddr_in sin; 28 | }; 29 | typedef tagListenerInfo_t ListenerInfo; 30 | 31 | typedef std::set SET_ListenerInfo; 32 | typedef SET_ListenerInfo::iterator ITR_ListenerInfo; 33 | 34 | struct tagTimerInfo_t 35 | { 36 | tagTimerInfo_t() 37 | { 38 | pEvent = NULL; 39 | pServer = NULL; 40 | pClient = NULL; 41 | nIDEvent = 0; 42 | } 43 | 44 | struct event *pEvent; 45 | void *pServer; 46 | void *pClient; 47 | UINT nIDEvent; 48 | }; 49 | typedef struct tagTimerInfo_t TimerInfo; 50 | 51 | typedef std::map MAP_Timer; 52 | typedef MAP_Timer::iterator ITR_Timer; 53 | 54 | 55 | typedef std::set SET_Client; 56 | typedef SET_Client::iterator ITR_Client; 57 | 58 | class InformationCore : public Singleton 59 | { 60 | public: 61 | InformationCore(void); 62 | ~InformationCore(void); 63 | 64 | void AddListenerInfo(ListenerInfo *pInfo); 65 | void RemoveListenerInfo(WORD nPort); 66 | void RemoveAllListenerInfo(); 67 | ListenerInfo * GetListenerInfo(WORD nPort); 68 | 69 | void AddServerTimer(UINT nIDEvent, TimerInfo* pInfo); 70 | void RemoveServerTimer(UINT nIDEvent); 71 | TimerInfo* GetServerTimer(UINT nIDEvent); 72 | 73 | void AddClientTimer(UINT nIDEvent, TimerInfo* pInfo); 74 | void RemoveClientTimer(UINT nIDEvent); 75 | TimerInfo* GetClientTimer(UINT nIDEvent); 76 | 77 | void AddClient(obd_client_session * pCli); 78 | void RemoveClient(obd_client_session * pCli); 79 | void VerifyClientLive(); 80 | 81 | int app_log_debug(FILE * logfp, const char * fmt, ...); 82 | int app_log_info( const char * fmt, ...); 83 | 84 | std::string get_datetime_string(); 85 | 86 | 87 | private: 88 | SET_ListenerInfo m_setListenerInfo; 89 | CCriticalSectionEx m_csListenerInfo; 90 | MAP_Timer m_mapServerTimer; 91 | CCriticalSectionEx m_csServerTimer; 92 | 93 | MAP_Timer m_mapClientTimer; 94 | CCriticalSectionEx m_csClientTimer; 95 | 96 | SET_Client m_setClient; 97 | CCriticalSectionEx m_csClient; 98 | }; 99 | 100 | #define sInfoCore InformationCore::getSingleton() 101 | 102 | //#if defined(_WIN32) || defined(_WIN64) 103 | //#define log(fmt, ...) sInfoCore.app_log("<%s>\t<%d>\t<%s>,"fmt, __FILENAME__, __LINE__, __FUNCTION__, ##__VA_ARGS__) 104 | //#else 105 | //#define log(fmt, args...) sInfoCore.app_log("<%s>|<%d>|<%s>," fmt, __FILENAME__, __LINE__, __FUNCTION__, ##args) 106 | //#endif 107 | 108 | #endif //_INFOMATION_CORE_H_ 109 | -------------------------------------------------------------------------------- /src/jthread.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | This file is a part of the JThread package, which contains some object- 4 | oriented thread wrappers for different thread implementations. 5 | 6 | Copyright (c) 2000-2006 Jori Liesenborgs (jori.liesenborgs@gmail.com) 7 | 8 | Permission is hereby granted, free of charge, to any person obtaining a 9 | copy of this software and associated documentation files (the "Software"), 10 | to deal in the Software without restriction, including without limitation 11 | the rights to use, copy, modify, merge, publish, distribute, sublicense, 12 | and/or sell copies of the Software, and to permit persons to whom the 13 | Software is furnished to do so, subject to the following conditions: 14 | 15 | The above copyright notice and this permission notice shall be included in 16 | all copies or substantial portions of the Software. 17 | 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 21 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 23 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 24 | DEALINGS IN THE SOFTWARE. 25 | 26 | */ 27 | 28 | #include "jthread.h" 29 | #include 30 | #include 31 | #include 32 | #include 33 | 34 | JThread::JThread() 35 | { 36 | retval = NULL; 37 | mutexinit = false; 38 | running = false; 39 | } 40 | 41 | JThread::~JThread() 42 | { 43 | Kill(); 44 | } 45 | 46 | int JThread::Start() 47 | { 48 | int status; 49 | 50 | if (!mutexinit) 51 | { 52 | if (!runningmutex.IsInitialized()) 53 | { 54 | if (runningmutex.Init() < 0) 55 | return ERR_JTHREAD_CANTINITMUTEX; 56 | } 57 | if (!continuemutex.IsInitialized()) 58 | { 59 | if (continuemutex.Init() < 0) 60 | return ERR_JTHREAD_CANTINITMUTEX; 61 | } 62 | if (!continuemutex2.IsInitialized()) 63 | { 64 | if (continuemutex2.Init() < 0) 65 | return ERR_JTHREAD_CANTINITMUTEX; 66 | } 67 | mutexinit = true; 68 | } 69 | 70 | runningmutex.Lock(); 71 | if (running) 72 | { 73 | runningmutex.Unlock(); 74 | return ERR_JTHREAD_ALREADYRUNNING; 75 | } 76 | runningmutex.Unlock(); 77 | 78 | pthread_attr_t attr; 79 | pthread_attr_init(&attr); 80 | pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED); 81 | 82 | continuemutex.Lock(); 83 | status = pthread_create(&threadid,&attr,TheThread,this); 84 | pthread_attr_destroy(&attr); 85 | if (status != 0) 86 | { 87 | continuemutex.Unlock(); 88 | return ERR_JTHREAD_CANTSTARTTHREAD; 89 | } 90 | 91 | /* Wait until 'running' is set */ 92 | //printf("---------------test jthread-----------------\n"); 93 | runningmutex.Lock(); 94 | while (!running) 95 | { 96 | //printf("---------------test jthread-----------------\n"); 97 | runningmutex.Unlock(); 98 | 99 | struct timespec req,rem; 100 | 101 | req.tv_sec = 0; 102 | req.tv_nsec = 1000000; 103 | nanosleep(&req,&rem); 104 | 105 | runningmutex.Lock(); 106 | } 107 | runningmutex.Unlock(); 108 | 109 | continuemutex.Unlock(); 110 | 111 | continuemutex2.Lock(); 112 | continuemutex2.Unlock(); 113 | return 0; 114 | } 115 | 116 | int JThread::Kill() 117 | { 118 | runningmutex.Lock(); 119 | if (!running) 120 | { 121 | runningmutex.Unlock(); 122 | return ERR_JTHREAD_NOTRUNNING; 123 | } 124 | pthread_cancel(threadid); 125 | running = false; 126 | runningmutex.Unlock(); 127 | return 0; 128 | } 129 | 130 | bool JThread::IsRunning() 131 | { 132 | bool r; 133 | 134 | runningmutex.Lock(); 135 | r = running; 136 | runningmutex.Unlock(); 137 | return r; 138 | } 139 | 140 | void *JThread::GetReturnValue() 141 | { 142 | void *val; 143 | 144 | runningmutex.Lock(); 145 | if (running) 146 | val = NULL; 147 | else 148 | val = retval; 149 | runningmutex.Unlock(); 150 | return val; 151 | } 152 | 153 | void *JThread::TheThread(void *param) 154 | { 155 | JThread *jthread; 156 | void *ret; 157 | 158 | jthread = (JThread *)param; 159 | 160 | jthread->continuemutex2.Lock(); 161 | jthread->runningmutex.Lock(); 162 | jthread->running = true; 163 | jthread->runningmutex.Unlock(); 164 | 165 | jthread->continuemutex.Lock(); 166 | jthread->continuemutex.Unlock(); 167 | 168 | ret = jthread->Thread(); 169 | 170 | jthread->runningmutex.Lock(); 171 | jthread->running = false; 172 | jthread->retval = ret; 173 | jthread->runningmutex.Unlock(); 174 | 175 | return NULL; 176 | } 177 | 178 | void JThread::ThreadStarted() 179 | { 180 | continuemutex2.Unlock(); 181 | } 182 | 183 | -------------------------------------------------------------------------------- /src/InformationCore.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "InformationCore.h" 3 | #include "obd_client_session.h" 4 | 5 | createFileSingleton(InformationCore); 6 | 7 | InformationCore::InformationCore(void) 8 | { 9 | 10 | } 11 | 12 | InformationCore::~InformationCore(void) 13 | { 14 | } 15 | 16 | void InformationCore::AddListenerInfo(ListenerInfo* pInfo) 17 | { 18 | __lock(m_csListenerInfo); 19 | m_setListenerInfo.insert(pInfo); 20 | } 21 | 22 | ListenerInfo * InformationCore::GetListenerInfo(WORD nPort) 23 | { 24 | ListenerInfo * pInfo = NULL; 25 | __lock(m_csListenerInfo); 26 | ITR_ListenerInfo itr = m_setListenerInfo.begin(); 27 | for (; itr != m_setListenerInfo.end(); ++ itr) 28 | { 29 | if((*itr)->sin.sin_port == htons(nPort)) 30 | { 31 | pInfo = (*itr); 32 | break; 33 | } 34 | } 35 | return pInfo; 36 | } 37 | 38 | void InformationCore::RemoveListenerInfo(WORD nPort) 39 | { 40 | __lock(m_csListenerInfo); 41 | ITR_ListenerInfo itr, itr2; 42 | for (itr = m_setListenerInfo.begin(); itr != m_setListenerInfo.end(); ++ itr) 43 | { 44 | itr2 = itr; 45 | if( (*itr2)->sin.sin_port == htons(nPort)) 46 | { 47 | 48 | if((*itr2)->pListener != NULL) 49 | { 50 | evconnlistener_free((*itr2)->pListener); 51 | } 52 | delete (*itr2); 53 | m_setListenerInfo.erase(itr2); 54 | } 55 | } 56 | } 57 | 58 | void InformationCore::RemoveAllListenerInfo() 59 | { 60 | ListenerInfo * p = NULL; 61 | { 62 | __lock(m_csListenerInfo); 63 | ITR_ListenerInfo itr; 64 | for (itr = m_setListenerInfo.begin(); itr != m_setListenerInfo.end(); ++ itr) 65 | { p = (*itr); 66 | if(p != NULL && p->pListener != NULL) 67 | { 68 | evconnlistener_free(p->pListener); 69 | delete p; 70 | p = NULL; 71 | } 72 | } 73 | m_setListenerInfo.clear(); 74 | } 75 | } 76 | 77 | void InformationCore::AddServerTimer(UINT nIDEvent, TimerInfo* pInfo) 78 | { 79 | __lock(m_csServerTimer); 80 | m_mapServerTimer.insert(std::make_pair(nIDEvent, pInfo)); 81 | } 82 | 83 | void InformationCore::RemoveServerTimer(UINT nIDEvent) 84 | { 85 | __lock(m_csServerTimer); 86 | m_mapServerTimer.erase(nIDEvent); 87 | } 88 | 89 | TimerInfo* InformationCore::GetServerTimer(UINT nIDEvent) 90 | { 91 | TimerInfo *pInfo = NULL; 92 | __lock(m_csServerTimer); 93 | ITR_Timer itr = m_mapServerTimer.find(nIDEvent); 94 | if (itr != m_mapServerTimer.end()) 95 | { 96 | pInfo = itr->second; 97 | } 98 | return pInfo; 99 | } 100 | 101 | 102 | void InformationCore::AddClientTimer(UINT nIDEvent, TimerInfo* pInfo) 103 | { 104 | __lock(m_csClientTimer); 105 | m_mapClientTimer.insert(std::make_pair(nIDEvent, pInfo)); 106 | } 107 | 108 | void InformationCore::RemoveClientTimer(UINT nIDEvent) 109 | { 110 | __lock(m_csClientTimer); 111 | m_mapClientTimer.erase(nIDEvent); 112 | } 113 | 114 | TimerInfo* InformationCore::GetClientTimer(UINT nIDEvent) 115 | { 116 | TimerInfo *pInfo = NULL; 117 | __lock(m_csClientTimer); 118 | ITR_Timer itr = m_mapClientTimer.find(nIDEvent); 119 | if (itr != m_mapClientTimer.end()) 120 | { 121 | pInfo = itr->second; 122 | } 123 | return pInfo; 124 | } 125 | 126 | //////////////////////////////////////////////////////////////// 127 | // 128 | void InformationCore::AddClient(obd_client_session * pCli) 129 | { 130 | __lock(m_csClient); 131 | m_setClient.insert(pCli); 132 | 133 | } 134 | void InformationCore::RemoveClient(obd_client_session * pCli) 135 | { 136 | __lock(m_csClient); 137 | m_setClient.erase(pCli); 138 | delete (pCli); 139 | pCli = NULL; 140 | 141 | } 142 | 143 | void InformationCore::VerifyClientLive() 144 | { 145 | __lock(m_csClient); 146 | ITR_Client itr, itr2; 147 | obd_client_session *p = NULL; 148 | for (itr = m_setClient.begin(); itr != m_setClient.end(); ) 149 | { 150 | p = *itr; 151 | itr2 = itr; 152 | ++ itr; 153 | if( ! (p->get_client_live())) 154 | { 155 | p->disconnect(); 156 | delete p; 157 | p = NULL; 158 | m_setClient.erase(itr2); 159 | printf("The closed socket was removed from from SET...\n"); 160 | } 161 | } 162 | } 163 | 164 | /* 165 | * 166 | */ 167 | int InformationCore::app_log_debug(FILE * logfp, const char * fmt, ...) 168 | { 169 | 170 | time_t t = time(NULL); 171 | struct tm * gmt = localtime(&t); 172 | int length = 0; 173 | va_list ap; 174 | va_start(ap, fmt); 175 | char log_buf[MAX_LOGBUF_LEN]; 176 | length += snprintf(log_buf , MAX_LOGBUF_LEN, "[ %4d-%02d-%02d %02d:%02d:%02d | <%s>\t<%d>\t<%s>]", gmt->tm_year+1900, gmt->tm_mon+1, gmt->tm_mday, gmt->tm_hour, gmt->tm_min, gmt->tm_sec,__FILENAME__, __LINE__, __FUNCTION__); 177 | length += vsnprintf(&log_buf[length], MAX_LOGBUF_LEN - length, fmt, ap); 178 | if(NULL != logfp) { 179 | fprintf(logfp, "%s", log_buf ); 180 | } 181 | va_end(ap); 182 | printf("%s", log_buf); 183 | return 1; 184 | } 185 | 186 | int InformationCore::app_log_info( const char * fmt, ...) 187 | { 188 | 189 | time_t t = time(NULL); 190 | struct tm * gmt = localtime(&t); 191 | int length = 0; 192 | va_list ap; 193 | va_start(ap, fmt); 194 | char log_buf[MAX_LOGBUF_LEN]; 195 | length += snprintf(log_buf , MAX_LOGBUF_LEN, "[ %4d-%02d-%02d %02d:%02d:%02d ]", gmt->tm_year+1900, gmt->tm_mon+1, gmt->tm_mday, gmt->tm_hour, gmt->tm_min, gmt->tm_sec); 196 | length += vsnprintf(&log_buf[length], MAX_LOGBUF_LEN - length, fmt, ap); 197 | va_end(ap); 198 | printf("%s", log_buf); 199 | 200 | return 1; 201 | } 202 | 203 | std::string InformationCore::get_datetime_string() 204 | { 205 | time_t t = time(NULL); 206 | struct tm * gmt = localtime(&t); 207 | 208 | char date_buf[STRINE_LENGHT_256] = {0}; 209 | sprintf(date_buf, "%4d_%02d_%02d_%02d_%02d_%02d", gmt->tm_year+1900, gmt->tm_mon+1, gmt->tm_mday, gmt->tm_hour, gmt->tm_min, gmt->tm_sec); 210 | 211 | return std::string(date_buf); 212 | } 213 | 214 | 215 | 216 | -------------------------------------------------------------------------------- /inc/TypeDefine.h: -------------------------------------------------------------------------------- 1 | // 2 | // TypeDefine.h 3 | // VeiPhoneVideo 4 | // 5 | // Created by wei laixi on 13-6-21. 6 | // Copyright (c) 2013年 garywlx. All rights reserved. 7 | // 8 | 9 | #ifndef VeiPhoneVideo_TypeDefine_h 10 | #define VeiPhoneVideo_TypeDefine_h 11 | 12 | 13 | #ifndef WIN32 14 | 15 | extern int g_nSaveLog; 16 | extern char g_pLogPath[255]; 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include "jthread.h" 25 | #include "jmutexautolock.h" 26 | 27 | # define LOGI(...) (void)0 28 | # define LOGE(...) (void)0 29 | 30 | //#define interface struct 31 | 32 | #ifndef ASSERT_RET 33 | 34 | #ifndef ASSERT 35 | #define ASSERT assert 36 | #endif 37 | 38 | 39 | #define ASSERT_RET(x) { ASSERT((x));if(!(x)) return ; } 40 | #define ASSERT_RETF(x) { ASSERT((x));if(!(x)) return false; } 41 | #define ASSERT_RETT(x) { ASSERT((x));if(!(x)) return true; } 42 | #define ASSERT_RET0(x) { ASSERT((x));if(!(x)) return 0; } 43 | #define ASSERT_RET1(x) { ASSERT((x));if(!(x)) return 0; } 44 | #define ASSERT_RETN1(x) { ASSERT((x));if(!(x)) return -1; } 45 | #define ASSERT_RETCSTR(x) { ASSERT((x));if(!(x)) return CString(); } 46 | #define ASSERT_RETVAL(x,v) { ASSERT((x));if(!(x)) return v; } 47 | 48 | #define ASSERT_RETFHR(x) { ASSERT(SUCCEEDED(x));if(!(SUCCEEDED(x))) return FALSE; } 49 | #define ASSERT_RETHR(x) { ASSERT(SUCCEEDED(x));if(!(SUCCEEDED(x))) return; } 50 | 51 | #define VERIFY ASSERT 52 | #define VERIFY_RET(x) { VERIFY((x));if(!(x)) return ; } 53 | #define VERIFY_RETF(x) { VERIFY((x));if(!(x)) return FALSE; } 54 | #define VERIFY_RETT(x) { VERIFY((x));if(!(x)) return TRUE; } 55 | #define VERIFY_RET0(x) { VERIFY((x));if(!(x)) return 0; } 56 | #define VERIFY_RET1(x) { VERIFY((x));if(!(x)) return 0; } 57 | #define VERIFY_RETN1(x) { VERIFY((x));if(!(x)) return -1; } 58 | #define VERIFY_RETCSTR(x) { VERIFY((x));if(!(x)) return CString(); } 59 | #define VERIFY_RETVAL(x,v) { VERIFY((x));if(!(x)) return v; } 60 | 61 | #define VERIFY_RETFHR(x) { VERIFY(SUCCEEDED(x));if(!(SUCCEEDED(x))) return FALSE; } 62 | #define VERIFY_RETHR(x) { VERIFY(SUCCEEDED(x));if(!(SUCCEEDED(x))) return; } 63 | 64 | #endif 65 | 66 | #ifndef TYPE_DEFINE 67 | #define TYPE_DEFINE 68 | 69 | 70 | typedef unsigned long ULONG_PTR; 71 | typedef const char* LPCSTR; 72 | typedef const char* LPCTSTR; 73 | //typedef unsigned char byte; 74 | typedef unsigned char BYTE; 75 | typedef unsigned short WORD; 76 | typedef unsigned long DWORD; 77 | typedef unsigned int UINT; 78 | typedef unsigned long ULONG; 79 | typedef unsigned short USHORT; 80 | typedef unsigned int UINT32; 81 | typedef long LONG; 82 | typedef long HRESULT; 83 | typedef long long __int64; 84 | typedef char TCHAR; 85 | typedef char CHAR; 86 | typedef void* PVOID; 87 | typedef void* LPVOID; 88 | typedef int SOCKET; 89 | typedef unsigned short u_short; 90 | 91 | #define INVALID_SOCKET -1 92 | #define SOCKET_ERROR -1 93 | 94 | 95 | typedef unsigned char byte; 96 | 97 | 98 | #ifndef INVALID_SOCKET 99 | #define INVALID_SOCKET 0 100 | #endif 101 | 102 | #ifndef FALSE 103 | #define FALSE 0 104 | #endif 105 | 106 | #ifndef TRUE 107 | #define TRUE 1 108 | #endif 109 | 110 | #define CALLBACK 111 | #define ARRAYSIZE(A) (sizeof(A)/sizeof((A)[0])) 112 | 113 | 114 | typedef struct { 115 | unsigned long Data1; 116 | unsigned short Data2; 117 | unsigned short Data3; 118 | byte Data4[ 8 ]; 119 | } GUID; 120 | 121 | 122 | #define S_OK ((HRESULT)0L) 123 | #define S_FALSE ((HRESULT)1L) 124 | #define SUCCEEDED(hr) (((HRESULT)(hr)) >= 0) 125 | #define FAILED(hr) (((HRESULT)(hr)) < 0) 126 | 127 | const int MAX_LOG4_SIZE = 4096; 128 | 129 | void LogInfo(LPCTSTR message,unsigned int w = 0,unsigned int l = 0); 130 | void LogWarn(const char* message,unsigned int w = 0,unsigned int l = 0); 131 | 132 | inline void AddLogError(const char* lpFmt,unsigned int l /* = 0 */) 133 | { 134 | 135 | } 136 | /* 137 | void LOGI(char* pBuffer) 138 | { 139 | NSString* str = [NSString stringWithCString:pBuffer encoding:NSUTF8StringEncoding]; 140 | NSLog(@"Info %s", str); 141 | } 142 | void LOGE(char* pBuffer) 143 | { 144 | NSString* str = [NSString stringWithCString:pBuffer encoding:NSUTF8StringEncoding]; 145 | NSLog(@"Error %s", str); 146 | } 147 | */ 148 | inline void AddLogV(const char* lpFmt,...) 149 | { 150 | char buf[MAX_LOG4_SIZE]={0}; 151 | va_list argptr; 152 | va_start(argptr, lpFmt); 153 | ::vsnprintf(buf,MAX_LOG4_SIZE - 1,lpFmt, argptr); 154 | va_end(argptr); 155 | 156 | strcat(buf, "\n"); 157 | LOGI(buf); 158 | //LOGI("Open path..., g_nSaveLog = %d ", g_nSaveLog); 159 | // if (g_nSaveLog) 160 | // { 161 | // FILE *fd; 162 | // //LOGI("Open path, %s ", g_pLogPath); 163 | // fd = fopen(g_pLogPath, "a+"); 164 | // if (fd == NULL) 165 | // { 166 | // LOGI("Open File %s Error", g_pLogPath); 167 | // return; 168 | // } 169 | // fwrite(buf, strlen(buf), 1, fd); 170 | // fclose(fd); 171 | // } 172 | } 173 | 174 | inline void LogError(const char* message,unsigned int w /*= 0*/,unsigned int l /*= 0*/) 175 | { 176 | 177 | } 178 | 179 | inline void LogErrorV(const char* lpFmt,...) 180 | { 181 | char buf[MAX_LOG4_SIZE]={0}; 182 | va_list argptr; 183 | va_start(argptr, lpFmt); 184 | ::vsnprintf(buf,MAX_LOG4_SIZE - 1,lpFmt, argptr); 185 | va_end(argptr); 186 | LOGE(buf); 187 | } 188 | 189 | inline void LogWarn(const char* message,unsigned int w /*= 0*/,unsigned int l /*= 0*/) 190 | { 191 | 192 | } 193 | 194 | inline void LogWarnV(const char* lpFmt,...) 195 | { 196 | char buf[MAX_LOG4_SIZE]={0}; 197 | va_list argptr; 198 | va_start(argptr, lpFmt); 199 | ::vsnprintf(buf,MAX_LOG4_SIZE - 1,lpFmt, argptr); 200 | va_end(argptr); 201 | LOGI(buf); 202 | } 203 | 204 | inline void LogInfo(LPCTSTR message,unsigned int w /*= 0*/,unsigned int l /*= 0*/) 205 | { 206 | 207 | } 208 | inline void LogInfoV(LPCTSTR lpFmt,...) 209 | { 210 | char buf[MAX_LOG4_SIZE]={0}; 211 | va_list argptr; 212 | va_start(argptr, lpFmt); 213 | ::vsnprintf(buf,MAX_LOG4_SIZE - 1,lpFmt, argptr); 214 | va_end(argptr); 215 | LOGI(buf); 216 | } 217 | 218 | inline void LogDebugV(LPCTSTR lpFmt,...) 219 | { 220 | char buf[MAX_LOG4_SIZE]={0}; 221 | va_list argptr; 222 | va_start(argptr, lpFmt); 223 | ::vsnprintf(buf,MAX_LOG4_SIZE - 1,lpFmt, argptr); 224 | va_end(argptr); 225 | LOGI(buf); 226 | } 227 | 228 | inline ULONG GetTickCount(void) 229 | { 230 | ULONG currentTime; 231 | struct timeval current; 232 | gettimeofday(¤t, NULL); 233 | 234 | currentTime = current.tv_sec * 1000 + current.tv_usec/1000; 235 | 236 | return currentTime; 237 | } 238 | #define GetCurrentTime() GetTickCount() 239 | 240 | inline void Sleep(unsigned int millseconds ) 241 | { 242 | // 1 毫秒(milisecond) = 1000 微秒 (microsecond). 243 | // Windows 的 Sleep 使用毫秒(miliseconds) 244 | // Linux 的 usleep 使用微秒(microsecond) 245 | // 由于原来的代码是在 Windows 中使用的,所以参数要有一个毫秒到微秒的转换。 246 | usleep( millseconds * 1000 ); 247 | } 248 | 249 | inline static void ZeroMemory(PVOID Destination,int Length) 250 | { 251 | memset((void*)Destination, 0, Length); 252 | return; 253 | }; 254 | 255 | #ifndef ZeroM 256 | #define ZeroM(x) ZeroMemory(&x,sizeof(x)) 257 | #define ZeroA(x) { ZeroMemory(x,sizeof(x));ASSERT(x[0] == x[1]); } 258 | #endif 259 | 260 | class JMutexInit :public JMutex 261 | { 262 | public: 263 | JMutexInit() 264 | { 265 | ZeroM(attr); 266 | InitEx(); 267 | } 268 | ~JMutexInit() 269 | { 270 | pthread_mutexattr_destroy(&attr); 271 | ZeroM(attr); 272 | } 273 | 274 | protected: 275 | int InitEx() 276 | { 277 | if (initialized) 278 | return ERR_JMUTEX_ALREADYINIT; 279 | 280 | int nRet= pthread_mutexattr_init(&attr); 281 | assert(nRet == 0); 282 | if (nRet != 0) 283 | { 284 | //printf("\nErr:pthread_mutexattr_init err=%d",nRet); 285 | } 286 | else 287 | { 288 | int nRet = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); 289 | if(nRet != 0) 290 | { 291 | printf("\nErr:pthread_mutexattr_settype err=%d",nRet); 292 | } 293 | else 294 | { 295 | //printf("\npthread_mutexattr_settype ok"); 296 | } 297 | } 298 | 299 | pthread_mutex_init(&mutex,&attr); 300 | initialized = true; 301 | return 0; 302 | } 303 | 304 | pthread_mutexattr_t attr; 305 | }; 306 | 307 | 308 | #endif 309 | 310 | #ifdef __cplusplus 311 | #ifndef __lock 312 | #define __lock(x) JMutexAutoLock lock(x); 313 | typedef JMutexInit CCriticalSectionEx; 314 | #endif 315 | #endif //__cplusplus 316 | 317 | 318 | #endif 319 | 320 | #endif 321 | 322 | 323 | 324 | --------------------------------------------------------------------------------