diff --git a/basic/.DS_Store b/basic/.DS_Store new file mode 100644 index 0000000..b7fa5d4 Binary files /dev/null and b/basic/.DS_Store differ diff --git a/basic/HMAC_SHA1.cpp b/basic/HMAC_SHA1.cpp index f8a1132..a05369a 100755 --- a/basic/HMAC_SHA1.cpp +++ b/basic/HMAC_SHA1.cpp @@ -5,7 +5,7 @@ //****************************************************************************** #include #include -#include "HMAC_SHA1.h" +#include "hmac_sha1.h" void CHMAC_SHA1::HMAC_SHA1(BYTE *text, int text_len, BYTE *key, int key_len, BYTE *digest) diff --git a/basic/HMAC_SHA1.h b/basic/HMAC_SHA1.h index 73a1152..d27188e 100755 --- a/basic/HMAC_SHA1.h +++ b/basic/HMAC_SHA1.h @@ -7,7 +7,7 @@ #ifndef __HMAC_SHA1_H__ #define __HMAC_SHA1_H__ -#include "SHA1.h" +#include "sha1.h" typedef unsigned char BYTE ; diff --git a/basic/JSON.cpp b/basic/JSON.cpp index 1f2145a..f230de8 100644 --- a/basic/JSON.cpp +++ b/basic/JSON.cpp @@ -7,7 +7,7 @@ */ #include "JSON.h" -#include "McZip.h" +#include "Zip.h" #include using namespace mc; diff --git a/basic/SHA1.cpp b/basic/SHA1.cpp index c3846ee..f14c769 100755 --- a/basic/SHA1.cpp +++ b/basic/SHA1.cpp @@ -29,7 +29,7 @@ 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F */ -#include "SHA1.h" +#include "sha1.h" #ifdef SHA1_UTILITY_FUNCTIONS #define SHA1_MAX_FILE_BUFFER 8000 diff --git a/basic/Thread.cpp b/basic/Thread.cpp index 7145d65..8eaea6c 100644 --- a/basic/Thread.cpp +++ b/basic/Thread.cpp @@ -1,4 +1,4 @@ -#include "Thread.h" +#include "thread.h" #ifdef __ANDROID__ #include #endif diff --git a/basic/ThreadPool.cpp b/basic/ThreadPool.cpp index 7eab3c6..c79f6f9 100644 --- a/basic/ThreadPool.cpp +++ b/basic/ThreadPool.cpp @@ -6,7 +6,7 @@ * history: */ -#include "ThreadPool.h" +#include "threadpool.h" #pragma --mark "工作者线程" ThreadWork::ThreadWork(pthread_mutex_t* lock, pthread_cond_t* ready, std::queue* queue){ @@ -53,24 +53,24 @@ void ThreadWork::loop(){ } #pragma --mark "线程池" -ThreadPool* ThreadPool::m_instance = NULL; +threadpool* threadpool::m_instance = NULL; -ThreadPool* ThreadPool::shareInstance(){ +threadpool* threadpool::shareInstance(){ if (m_instance==NULL) { - m_instance = new ThreadPool(); + m_instance = new threadpool(); } return m_instance; } -void ThreadPool::destroyInstance(){ +void threadpool::destroyInstance(){ if (m_instance!=NULL) { delete m_instance; m_instance = NULL; } } -ThreadPool::ThreadPool(int thread_num){ +threadpool::threadpool(int thread_num){ m_threads.resize(thread_num); pthread_mutex_init(&m_lock, NULL); pthread_cond_init(&m_ready, NULL); @@ -81,11 +81,11 @@ ThreadPool::ThreadPool(int thread_num){ } } -ThreadPool::~ThreadPool(){ +threadpool::~threadpool(){ destroy(); } -void ThreadPool::add(void* (*run)(void*),void* arg){ +void threadpool::add(void* (*run)(void*),void* arg){ pthread_mutex_lock(&m_lock); thread_task_t task = {run , arg}; m_queue.push(task); @@ -93,7 +93,7 @@ void ThreadPool::add(void* (*run)(void*),void* arg){ pthread_mutex_unlock(&m_lock); } -void ThreadPool::destroy(){ +void threadpool::destroy(){ if (m_threads.size()==0) { return; } @@ -121,5 +121,5 @@ void ThreadPool::destroy(){ } void async_run(void* (*run)(void*), void* arg){ - ThreadPool::shareInstance()->add(run, arg); + threadpool::shareInstance()->add(run, arg); } diff --git a/basic/ThreadPool.h b/basic/ThreadPool.h index 191cc28..2f621fc 100644 --- a/basic/ThreadPool.h +++ b/basic/ThreadPool.h @@ -13,7 +13,7 @@ #include #include #include "bs.h" -#include "Thread.h" +#include "thread.h" using namespace std; typedef struct thread_task_t{ @@ -36,19 +36,19 @@ class ThreadWork:public LoopThread{ pthread_cond_t m_queue_ready; }; -class ThreadPool{ +class threadpool{ public: - static ThreadPool* shareInstance(); + static threadpool* shareInstance(); static void destroyInstance(); - ThreadPool(int thread_num = 20); - ~ThreadPool(); + threadpool(int thread_num = 2); + ~threadpool(); void add(void* (*run)(void*),void* arg); void destroy(); protected: - static ThreadPool* m_instance; + static threadpool* m_instance; std::vector m_threads; std::queue m_queue; diff --git a/basic/android/McDevice.cpp b/basic/android/device.cpp similarity index 99% rename from basic/android/McDevice.cpp rename to basic/android/device.cpp index b276c54..6d77d40 100644 --- a/basic/android/McDevice.cpp +++ b/basic/android/device.cpp @@ -9,8 +9,8 @@ #include #include #include "bs.h" -#include "../McDevice.h" -#include "../McFile.h" +#include "device.h" +#include "file.h" using namespace std; diff --git a/basic/android/McResource.cpp b/basic/android/resource.cpp similarity index 98% rename from basic/android/McResource.cpp rename to basic/android/resource.cpp index c856063..07dcd26 100644 --- a/basic/android/McResource.cpp +++ b/basic/android/resource.cpp @@ -9,7 +9,7 @@ #include #include #include -#include "../McFile.h" +#include "file.h" extern JavaVM* g_jvm; diff --git a/basic/McBasic.h b/basic/basic.h similarity index 76% rename from basic/McBasic.h rename to basic/basic.h index 22f769d..9bfd47b 100644 --- a/basic/McBasic.h +++ b/basic/basic.h @@ -9,17 +9,17 @@ #ifndef __basic_h #define __basic_h -#include "JSON.h" -#include "McDevice.h" -#include "McFile.h" -#include "McHttp.h" -#include "McZip.h" -#include "Thread.h" -#include "McReference.h" -#include "SharedPreferences.h" -#include "McTask.h" -#include "ThreadPool.h" -#include "McTimer.h" +#include "json.h" +#include "device.h" +#include "file.h" +#include "http.h" +#include "zip.h" +#include "thread.h" +#include "reference.h" +#include "shared_preferences.h" +#include "task.h" +#include "threadpool.h" +#include "timer.h" // 内存管理好烦啊 // 异步删除内存,用于回调中删除自己 diff --git a/basic/McDevice.h b/basic/device.h similarity index 100% rename from basic/McDevice.h rename to basic/device.h diff --git a/basic/McFile.cpp b/basic/file.cpp similarity index 99% rename from basic/McFile.cpp rename to basic/file.cpp index dda87fe..643b1e5 100644 --- a/basic/McFile.cpp +++ b/basic/file.cpp @@ -8,8 +8,8 @@ #include #include -#include "McFile.h" -#include "McDevice.h" +#include "file.h" +#include "device.h" using namespace mc; diff --git a/basic/McFile.h b/basic/file.h similarity index 100% rename from basic/McFile.h rename to basic/file.h diff --git a/basic/McHmacAuth.cpp b/basic/hmac_auth.cpp similarity index 97% rename from basic/McHmacAuth.cpp rename to basic/hmac_auth.cpp index 99a8213..229cf1a 100644 --- a/basic/McHmacAuth.cpp +++ b/basic/hmac_auth.cpp @@ -6,9 +6,9 @@ // Copyright © 2017年 Rex. All rights reserved. // -#include "McHmacAuth.h" +#include "hmac_auth.h" #include -#include "HMAC_SHA1.h" +#include "hmac_sha1.h" #define MAX_MESSAGE_LENGTH 4096 diff --git a/basic/McHmacAuth.h b/basic/hmac_auth.h similarity index 100% rename from basic/McHmacAuth.h rename to basic/hmac_auth.h diff --git a/basic/McHttp.cpp b/basic/http.cpp similarity index 96% rename from basic/McHttp.cpp rename to basic/http.cpp index d4a7dc8..3e3954b 100644 --- a/basic/McHttp.cpp +++ b/basic/http.cpp @@ -7,14 +7,14 @@ */ #include "bs.h" -#include "McDevice.h" -#include "McHttp.h" -#include "McFile.h" -#include "McTask.h" -#include "ThreadPool.h" -#include "SharedPreferences.h" -#include "McFile.h" -#include "McZip.h" +#include "device.h" +#include "http.h" +#include "file.h" +#include "task.h" +#include "threadpool.h" +#include "shared_preferences.h" +#include "file.h" +#include "Zip.h" #include #include "JSON.h" @@ -98,7 +98,7 @@ bool HttpSession::http_show_log = false; HttpSession::HttpSession(void *threadpool) { if (threadpool == NULL) { - m_thread_pool = ThreadPool::shareInstance(); + m_thread_pool = threadpool::shareInstance(); } else { m_thread_pool = threadpool; } @@ -148,7 +148,7 @@ void HttpSession::http(const char *url, const char *method, const char *body, ui void **para = (void **) malloc(sizeof(void *) * 2); para[0] = h; para[1] = this; - ((ThreadPool *) m_thread_pool)->add(thread_http, para); + ((threadpool *) m_thread_pool)->add(thread_http, para); } void HttpSession::download(const char *url, const char *path, HttpFileCallback *callback) { @@ -261,7 +261,7 @@ void HttpSession::saveCookie(const char *cookie) { int64_t crysize = crypt.encryptByCompress((byte *) str, (uint32_t) strlen(str)); char path[1024]; - SharedPreferences::path("__CurrentCookie", path, sizeof(path)); + shared_preferences::path("__CurrentCookie", path, sizeof(path)); FileManager::write(path, crypt.bytes(), (size_t) crysize); } @@ -271,7 +271,7 @@ void HttpSession::setCookie(const char *cookie) { void HttpSession::clearCookie() { char path[1024]; - SharedPreferences::path("__CurrentCookie", path, sizeof(path)); + shared_preferences::path("__CurrentCookie", path, sizeof(path)); FileManager::remove(path); map::iterator cookie = m_headers.find("Cookie"); m_headers.erase(cookie); diff --git a/basic/McHttp.h b/basic/http.h similarity index 100% rename from basic/McHttp.h rename to basic/http.h diff --git a/basic/ios/McDevice.mm b/basic/ios/device.mm similarity index 99% rename from basic/ios/McDevice.mm rename to basic/ios/device.mm index ba4a716..d18d8d2 100644 --- a/basic/ios/McDevice.mm +++ b/basic/ios/device.mm @@ -10,11 +10,11 @@ #import #include #import -#include "McDevice.h" +#include "device.h" #import "SecureUDID.h" #include #include -#include "McDevice.h" +#include "device.h" #include "SFHFKeychainUtils.h" #include diff --git a/basic/ios/McFile.mm b/basic/ios/file.mm similarity index 97% rename from basic/ios/McFile.mm rename to basic/ios/file.mm index c8f0a54..99b1e5b 100644 --- a/basic/ios/McFile.mm +++ b/basic/ios/file.mm @@ -9,7 +9,7 @@ #import #include #include -#include "McFile.h" +#include "file.h" using namespace mc; //const char* FileManager::m_root_path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] UTF8String]; diff --git a/basic/McReference.cpp b/basic/reference.cpp similarity index 92% rename from basic/McReference.cpp rename to basic/reference.cpp index 7e189e3..508c4ca 100644 --- a/basic/McReference.cpp +++ b/basic/reference.cpp @@ -6,7 +6,7 @@ * history: */ -#include "McReference.h" +#include "reference.h" using namespace mc; @@ -33,7 +33,7 @@ ReferenceCache::~ReferenceCache(){ m_cache.clear(); } -Reference* ReferenceCache::get(){ +reference* ReferenceCache::get(){ for (int i=0; iidle()) { return m_cache[i]; diff --git a/basic/McReference.h b/basic/reference.h similarity index 90% rename from basic/McReference.h rename to basic/reference.h index 47f2e3f..79635d4 100644 --- a/basic/McReference.h +++ b/basic/reference.h @@ -14,9 +14,9 @@ #include namespace mc { - class Reference{ + class reference{ public: - Reference(){ + reference(){ m_referencecount = 0; m_is_reference = true; } @@ -68,16 +68,16 @@ namespace mc { ~ReferenceCache(); - virtual Reference* get(); + virtual reference* get(); // 必须是new出来的对象 - inline void add(Reference* r){ + inline void add(reference* r){ m_cache.push_back(r); } protected: static ReferenceCache* m_instance; - std::vector m_cache; + std::vector m_cache; }; } diff --git a/basic/SharedPreferences.cpp b/basic/shared_preferences.cpp similarity index 79% rename from basic/SharedPreferences.cpp rename to basic/shared_preferences.cpp index 39c5e22..69b75dd 100644 --- a/basic/SharedPreferences.cpp +++ b/basic/shared_preferences.cpp @@ -6,15 +6,15 @@ * history: */ -#include "SharedPreferences.h" -#include "McZip.h" +#include "shared_preferences.h" +#include "Zip.h" using namespace mc; static char g_root_path[1024]; -const char* SharedPreferences::m_root_path = NULL; +const char* shared_preferences::m_root_path = NULL; -SharedPreferences::SharedPreferences(const char* name){ +shared_preferences::shared_preferences(const char* name){ if (name != NULL) { path(name, m_path, sizeof(m_path)); if(open(m_path)){ @@ -26,7 +26,7 @@ SharedPreferences::SharedPreferences(const char* name){ } } -bool SharedPreferences::commit(){ +bool shared_preferences::commit(){ const char* b = toString(); Crypt crypt; int64_t size = crypt.encryptByCompress((byte*)b, (uint32_t)strlen(b)); @@ -34,7 +34,7 @@ bool SharedPreferences::commit(){ return true; } -const char* SharedPreferences::path(const char* name, char* path, uint32_t size){ +const char* shared_preferences::path(const char* name, char* path, uint32_t size){ if (m_root_path == NULL) { snprintf(g_root_path, sizeof(g_root_path), "%s/__shared_prefs", FileManager::rootPath()); // 如果不存在则创建文件夹 diff --git a/basic/SharedPreferences.h b/basic/shared_preferences.h similarity index 80% rename from basic/SharedPreferences.h rename to basic/shared_preferences.h index 07945e3..327776a 100644 --- a/basic/SharedPreferences.h +++ b/basic/shared_preferences.h @@ -9,14 +9,14 @@ #ifndef __SHAREDPREFERENCES_H_ #define __SHAREDPREFERENCES_H_ -#include "McFile.h" +#include "file.h" #include "JSON.h" namespace mc { /// 轻量级存储 - class SharedPreferences: public JSONObject, public File { + class shared_preferences: public JSONObject, public File { public: - SharedPreferences(const char* name); + shared_preferences(const char* name); bool commit(); diff --git a/basic/McTask.cpp b/basic/task.cpp similarity index 99% rename from basic/McTask.cpp rename to basic/task.cpp index da3e5b9..5df64f0 100644 --- a/basic/McTask.cpp +++ b/basic/task.cpp @@ -6,7 +6,7 @@ * history: */ -#include "McTask.h" +#include "task.h" using namespace mc; diff --git a/basic/McTask.h b/basic/task.h similarity index 98% rename from basic/McTask.h rename to basic/task.h index 60f3afb..d2ac36a 100644 --- a/basic/McTask.h +++ b/basic/task.h @@ -12,7 +12,7 @@ #include #include #include - #include "Thread.h" + #include "thread.h" using namespace std; typedef void* (* mc_async_task_f)(void* para); diff --git a/basic/McTimer.cpp b/basic/timer.cpp similarity index 86% rename from basic/McTimer.cpp rename to basic/timer.cpp index 71ff20c..72b0688 100644 --- a/basic/McTimer.cpp +++ b/basic/timer.cpp @@ -6,18 +6,18 @@ * history: */ -#include "McTimer.h" +#include "timer.h" using namespace mc; -Timer::Timer(long utime, void* (*callback)(void*), void* para, timer_type_t type){ +timer::timer(long utime, void* (*callback)(void*), void* para, timer_type_t type){ m_utime = utime; m_callback = callback; m_timer_type = type; m_para = para; } -void Timer::loop(){ +void timer::loop(){ switch (m_timer_type) { case TIMER_SLEEP: usleep(m_utime); diff --git a/basic/McTimer.h b/basic/timer.h similarity index 82% rename from basic/McTimer.h rename to basic/timer.h index baa0673..5a6356f 100644 --- a/basic/McTimer.h +++ b/basic/timer.h @@ -10,7 +10,7 @@ #define __MCTIMER_H_ #include "bs.h" -#include "Thread.h" +#include "thread.h" namespace mc { typedef enum{ @@ -19,9 +19,9 @@ namespace mc { TIMER_RTC, }timer_type_t; - class Timer: public LoopThread{ + class timer: public LoopThread{ public: - Timer(long utime, void* (*callback)(void*), void* para = NULL, timer_type_t type = TIMER_SLEEP); + timer(long utime, void* (*callback)(void*), void* para = NULL, timer_type_t type = TIMER_SLEEP); virtual void loop(); diff --git a/basic/McZip.cpp b/basic/zip.cpp similarity index 99% rename from basic/McZip.cpp rename to basic/zip.cpp index 54a17c5..f354803 100644 --- a/basic/McZip.cpp +++ b/basic/zip.cpp @@ -8,9 +8,9 @@ #include #include "bs.h" -#include "McZip.h" +#include "Zip.h" #include "zlib.h" -#include "McFile.h" +#include "file.h" using namespace mc; diff --git a/basic/McZip.h b/basic/zip.h similarity index 100% rename from basic/McZip.h rename to basic/zip.h diff --git a/bs/bs_socket.c b/bs/bs_socket.c index 0f113b3..92f3594 100644 --- a/bs/bs_socket.c +++ b/bs/bs_socket.c @@ -4,6 +4,7 @@ * created : 2013-11-18 15:03 * func : */ +#include #include "bs_socket.h" #include "bs_common.h" #include "bs_type.h" @@ -263,6 +264,9 @@ static int socket_create(int family, int sock_type, int is_nonblock){ int flag = 0; setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &flag, sizeof(int)); //使用KEEPALIVE setsockopt (sock, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int)); // 立刻发送 + // 设置端口复用 + int opt = SO_REUSEADDR; + setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); } if(is_nonblock){ diff --git a/model/async/AsyncFrame.h b/model/async/AsyncFrame.h index eb68f4f..7762c22 100644 --- a/model/async/AsyncFrame.h +++ b/model/async/AsyncFrame.h @@ -10,7 +10,7 @@ #define __ASYNCFRAME_H_ #include "bs.h" -#include "Thread.h" +#include "thread.h" class AsyncFrame: public LoopThread{ public: diff --git a/model/async/AsyncSocket.cpp b/model/async/AsyncSocket.cpp index 0e259b4..01532ef 100644 --- a/model/async/AsyncSocket.cpp +++ b/model/async/AsyncSocket.cpp @@ -7,7 +7,7 @@ */ #include "AsyncSocket.h" -#include "ThreadPool.h" +#include "threadpool.h" void* sock_msg_init(void* p){ sock_msg_t* msg = (sock_msg_t*)p; diff --git a/model/async/AsyncSocket.h b/model/async/AsyncSocket.h index 55d6a1f..e52e79d 100644 --- a/model/async/AsyncSocket.h +++ b/model/async/AsyncSocket.h @@ -10,7 +10,7 @@ #define __ASYNCSOCKET_H_ #include "bs.h" -#include "Thread.h" +#include "thread.h" #ifdef __ANDROID__ #include #endif diff --git a/model/async/HttpEntity.h b/model/async/HttpEntity.h index afd70c8..759b7f6 100644 --- a/model/async/HttpEntity.h +++ b/model/async/HttpEntity.h @@ -12,8 +12,8 @@ #include "bs_url.h" #include "AsyncSocket.h" #include "SocketFrame.h" -#include "ThreadPool.h" -#include "McHttp.h" +#include "threadpool.h" +#include "http.h" using namespace mc; namespace model { diff --git a/model/async/ListenSocket.h b/model/async/ListenSocket.h index bbdbd94..3ef1203 100644 --- a/model/async/ListenSocket.h +++ b/model/async/ListenSocket.h @@ -12,7 +12,7 @@ #include "bs.h" #include "AsyncSocket.h" #include "SocketFrame.h" -#include "ThreadPool.h" +#include "threadpool.h" class TCPSocket: public AsyncSocket{ public: diff --git a/model/websocket/WebSocket.cpp b/model/websocket/WebSocket.cpp index 2d166b4..3c7b0a8 100644 --- a/model/websocket/WebSocket.cpp +++ b/model/websocket/WebSocket.cpp @@ -2,10 +2,12 @@ // Created by Rex on 2018/3/17. // +#include #include "WebSocket.h" static int callback(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len); +static void* on_message(void* msg); static struct lws_protocols protocols[] = { {"chat", callback, sizeof(void*), 0}, @@ -111,7 +113,7 @@ void WebSocket::pingpong(){ } void WebSocket::loop(){ - lws_service(m_context, 20); + lws_service(m_context, 20000); // 如果已经断开则重新连接 if (!alive()){ err_log("not alive, reconnect %s:%d%s", m_host.c_str(), m_port, m_path.c_str()); @@ -128,9 +130,13 @@ static int callback(struct lws *wsi, enum lws_callback_reasons reason, void *use WebSocket* ws = WebSocket::m_instance; switch (reason){ - case LWS_CALLBACK_CLIENT_RECEIVE: - ws->message((const char*)in, len); + case LWS_CALLBACK_CLIENT_RECEIVE: { + // 消息异步处理,防止阻塞 + char* msg = (char*)calloc(1, len+1); + memcpy(msg, in ,len); + async_run(on_message, msg); break; + } case LWS_CALLBACK_CLIENT_ESTABLISHED: ws->opened(); break; @@ -149,4 +155,12 @@ static int callback(struct lws *wsi, enum lws_callback_reasons reason, void *use } return 0; +} + +void* on_message(void* msg){ + WebSocket* ws = WebSocket::m_instance; + uint32_t size = strlen((char*)msg); + ws->message((const char*)msg, size); + free(msg); + return NULL; } \ No newline at end of file diff --git a/model/websocket/WebSocket.h b/model/websocket/WebSocket.h index 5367caa..3a4d34b 100644 --- a/model/websocket/WebSocket.h +++ b/model/websocket/WebSocket.h @@ -8,7 +8,7 @@ #include #include "bs.h" #include "libwebsockets.h" -#include "Thread.h" +#include "thread.h" using namespace std; class WebSocket: public LoopThread{