From c5aeb77268b2042f8ced02616c3fcfdff148c22a Mon Sep 17 00:00:00 2001 From: qwerty2501 Date: Sat, 29 Oct 2011 21:57:54 +0900 Subject: [PATCH] 2011/10/29 21:57:54 --- JSONParser/json_parser.c | 4 +- nlib/nlib.h | 6 +- nlib/nlib_myList.c | 12 +-- nlib/nlib_nicoLive.c | 200 +++++++++++++++++--------------------------- nlib/nlib_nicoLive.h | 3 + nlib_driver/nlib_driver.cpp | 12 +-- 6 files changed, 95 insertions(+), 142 deletions(-) diff --git a/JSONParser/json_parser.c b/JSONParser/json_parser.c index 4ba6dd2..8b7b9d1 100644 --- a/JSONParser/json_parser.c +++ b/JSONParser/json_parser.c @@ -197,7 +197,7 @@ static JSONResult JSONParser_ParseUTF8(JSONParser_P parser,const JSONText *jsont parser->structHandler(parser->option,JSONTypeRoot,JSONTypeObject,NULL); - ParseObjectUTF8(parser,jsontext,&jsontext); + rslt = ParseObjectUTF8(parser,jsontext,&jsontext); parser->dataEndHandler(parser->option,JSONTypeRoot,JSONTypeObject,NULL); @@ -208,7 +208,7 @@ static JSONResult JSONParser_ParseUTF8(JSONParser_P parser,const JSONText *jsont parser->structHandler(parser->option,JSONTypeRoot,JSONTypeArray,NULL); - ParseArrayUTF8(parser,jsontext,&jsontext); + rslt = ParseArrayUTF8(parser,jsontext,&jsontext); parser->dataEndHandler(parser->option,JSONTypeRoot,JSONTypeArray,NULL); diff --git a/nlib/nlib.h b/nlib/nlib.h index 45ad1e8..2153a3d 100644 --- a/nlib/nlib.h +++ b/nlib/nlib.h @@ -44,10 +44,6 @@ typedef UINT_PTR NICOLIVE_PARAM; -/// -///nlib‚̐ݒèƒIƒuƒWƒFƒNƒgƒ|ƒCƒ“ƒ^ -/// -//typedef struct nlibproperty_t *NlibProperty_P; /// ///ƒjƒRƒjƒR¶•ú‘—ƒIƒuƒWƒFƒNƒgƒ|ƒCƒ“ƒ^ @@ -101,7 +97,7 @@ typedef NLIB_RESULT (*NicoRecvCallBack)(NICOLIVE_EVENT eventType,NicoLiveStream_ /// ///ƒjƒRƒjƒRƒ}ƒCƒŠƒXƒgƒCƒxƒ“ƒgƒnƒ“ƒhƒ‰ /// -typedef NLIB_RESULT (*NicoMyListCallBack)(LPVOID option,NicoMyListItem_P pVideo,DWORD code); +typedef NLIB_RESULT (*NicoMyListCallBack)(LPVOID option,NicoMyListItem_P pVideo,NLIB_RESULT rslt); diff --git a/nlib/nlib_myList.c b/nlib/nlib_myList.c index ae563a2..2f4bf2f 100644 --- a/nlib/nlib_myList.c +++ b/nlib/nlib_myList.c @@ -37,7 +37,7 @@ static JSONVoid myListItemDataEndEventHandler(JSONVoid *option,JSONUInt thisHier static VOID CALLBACK myListItemCallBack(HINTERNET handle,DWORD_PTR context,DWORD code,LPVOID info,DWORD length); -static NLIB_RESULT recvMyList(HINTERNET hGetMyList,NicoMyListItemContainer_P itemContainer); +static NLIB_RESULT recvMyList(HINTERNET hGetMyList,NicoMyListItemContainer_P itemContainer,NLIB_RESULT rslt); /// ///‚Æ‚è‚ ‚¦‚¸ƒ}ƒCƒŠƒXƒgˆê——Žæ“¾ @@ -46,7 +46,7 @@ NLIB_RESULT NicoMyList_getDefList(NicoVideoAuth_P pAuth,NicoMyListCallBack callb NLIB_RESULT rslt = NLIB_ERR_OK; - rslt = NicoMyList_getList(pAuth,L"api/deflist/list",callback,mtFlag); + rslt = NicoMyList_getList(pAuth,L"api/deflist/list",callback,mtFlag,option); @@ -103,7 +103,7 @@ static NLIB_RESULT NicoMyList_getList(NicoVideoAuth_P pAuth,LPCWSTR objectName,N if(mtFlag == FALSE){ - rslt = recvMyList(hGetMyList,&itemContainer); + rslt = recvMyList(hGetMyList,&itemContainer,NLIB_ERR_OK); } @@ -124,9 +124,9 @@ static VOID CALLBACK myListItemCallBack(HINTERNET handle,DWORD_PTR context,DWORD } -static NLIB_RESULT recvMyList(HINTERNET hGetMyList,NicoMyListItemContainer_P itemContainer){ +static NLIB_RESULT recvMyList(HINTERNET hGetMyList,NicoMyListItemContainer_P itemContainer,NLIB_RESULT rslt){ - NLIB_RESULT rslt; + const UINT_PTR readBufSize = LENGTH_65536 * 30; @@ -169,7 +169,7 @@ static NLIB_RESULT recvMyList(HINTERNET hGetMyList,NicoMyListItemContainer_P ite readBuffer[readSize] = '\0'; - if(JSONParser_Parse(jsonParser,readBuffer,JSON_ENCODE_UTF8,&itemContainer) != JSON_ERR_OK){ + if(JSONParser_Parse(jsonParser,readBuffer,JSON_ENCODE_UTF8,itemContainer) != JSON_ERR_OK){ goto jsonerr; } diff --git a/nlib/nlib_nicoLive.c b/nlib/nlib_nicoLive.c index 19945f2..6068e8e 100644 --- a/nlib/nlib_nicoLive.c +++ b/nlib/nlib_nicoLive.c @@ -61,44 +61,6 @@ static CRITICAL_SECTION heartBeatCs; }\ (convPtr)[0] = L'\0' - -/* -#define convertTEXT(buf,basePtr,convPtr,endPtr)\ - (buf) = (convPtr);\ - for(;(basePtr) < (endPtr);(basePtr)++){\ - switch(basePtr[0]){\ - case L'&':\ - strcpy((convPtr),"&");\ - (convPtr) += strlen(convPtr);\ - break;\ - case L'<':\ - strcpy((convPtr),"<");\ - (convPtr) += strlen(convPtr);\ - break;\ - case L'>':\ - strcpy((convPtr),">");\ - (convPtr) += strlen(convPtr);\ - break;\ - case L'\"':\ - strcpy((convPtr),""");\ - (convPtr) += strlen(convPtr);\ - break;\ - case L'\'':\ - strcpy((convPtr),"'");\ - (convPtr) += strlen(convPtr);\ - break;\ - default:\ - WideCharToMultiByte(CP_UTF8,0,(basePtr),1,(convPtr),LENGTH_1024,(LPCSTR)NULL,(LPBOOL)NULL);\ - (convPtr) += strlen(convPtr);\ - break;\ - }\ - }\ - (convPtr)[0] = '\0' -*/ - - - - #define getBaseVpos(server_time,base_time) (((server_time) - (base_time)) * 100) #define setMsec(mSec)\ @@ -166,11 +128,18 @@ static unsigned int WINAPI NicoLiveHeartBeatThread(void* lpBuffer); /// static unsigned int WINAPI NicoLiveKeapALiveThread(void* lpBuffer); + + + /// ///ƒ`ƒƒƒbƒgŽóMƒ‹[ƒvŠÖ” /// static BOOL NicoLiveRecvChatLoop(NicoLiveStream_P self); +/// +///ƒ‹[ƒv +/// +static unsigned int loopCommon(NicoLiveStream_P self,WSAEVENT sendEvent,WSAEVENT resetEvent ,NicoLiveOnSend nicoLiveOnSend); /// ///ƒRƒƒ“ƒg‰ðÍŠÖ” @@ -180,6 +149,10 @@ static VOID NicoLiveParseComment(NicoLiveStream_P self,LPWSTR commentBuf,UINT_P static void NicoLiveError(void *buf); static void NicoLiveSelfDisconnect(void *buf); + +static VOID OnSendHeartBeat(NicoLiveStream_P self); + +static VOID OnSendKeepAllive(NicoLiveStream_P self); /// ///ƒ\ƒPƒbƒgŽæ“¾ŠÖ” /// @@ -238,6 +211,21 @@ NicoLiveStream_P NicoLiveStream_new(){ //¶¬&‰Šú‰» NicoLiveStream_P self = (NicoLiveStream_P)calloc(1,sizeof(NicoLiveStream)); + //‰Šú’l‚̐ݒè + self->chatManager.onHeartBeatSend = OnSendHeartBeat; + + self->chatManager.onKeepAlliveSend = OnSendKeepAllive; + + InitializeCriticalSection(&self->sessionManager.cs); + + self->chatManager.heartBeatTime = 300000; + + self->chatManager.heartBeatFlag = TRUE; + + self->chatManager.keepAliveTime = 300000; + + self->chatManager.keapALiveFlag = TRUE; + self->chatManager.endEvent = WSACreateEvent(); self->chatManager.startEvent = WSACreateEvent(); @@ -254,21 +242,22 @@ NicoLiveStream_P NicoLiveStream_new(){ self->chatManager.resetEvents[2] = WSACreateEvent(); + + + + + //ƒXƒŒƒbƒh‚ÌŠJŽn self->chatManager.hRecvChatHandle = (HANDLE)_beginthreadex(NULL,0,NicoLiveRecvChat,self,0,&self->chatManager.recvChatThreadID); self->chatManager.hHeartBeatHandle = (HANDLE)_beginthreadex(NULL,0,NicoLiveHeartBeatThread,self,0,&self->chatManager.heartBeatThreadID); self->chatManager.hKeapALiveHandle = (HANDLE)_beginthreadex(NULL,0,NicoLiveKeapALiveThread,self,0,&self->chatManager.keapALiveThreadID); - self->chatManager.heartBeatTime = 300000; - - self->chatManager.heartBeatFlag = TRUE; - self->chatManager.keepAliveTime = 300000; - self->chatManager.keapALiveFlag = TRUE; + - InitializeCriticalSection(&self->sessionManager.cs); + dumpln(TEXT("size%d"),sizeof(NicoLiveStream)); @@ -1335,99 +1324,39 @@ static unsigned int WINAPI NicoLiveKeapALiveThread(void* lpBuffer){ NicoLiveStream_P self = (NicoLiveStream_P)lpBuffer; - WSAEVENT events[3] = {self->chatManager.deleteEvent,self->chatManager.startEvent,self->chatManager.endEvent}; //ƒCƒxƒ“ƒg”z—ñ - WSAEVENT streamEvent[2] = {self->chatManager.endEvent,self->chatManager.setKeepAliveTimeEvent}; - DWORD dwResult; //ƒCƒxƒ“ƒgŽóMŒ‹‰Ê - DWORD endResult; - - while(TRUE){ -restart: - WSASetEvent(self->chatManager.resetEvents[2]); - - dwResult = WSAWaitForMultipleEvents(sizeof(events) / sizeof(events[0]),events,FALSE,WSA_INFINITE,FALSE); - - - //ƒGƒ‰[‚̏ꍇ - if(dwResult == WSA_WAIT_FAILED){ - - goto err; - } - - - switch(dwResult - WSA_WAIT_EVENT_0){ + rslt = loopCommon(self,self->chatManager.setKeepAliveTimeEvent,self->chatManager.resetEvents[2],self->chatManager.onKeepAlliveSend); - case 0: - - goto end; - - case 1: - WSAResetEvent(self->chatManager.resetEvents[2]); - break; - - case 2: - continue; - - - default: - - goto err; - - } - - while(TRUE){ - WSAResetEvent(self->chatManager.setKeepAliveTimeEvent); - endResult = WSAWaitForMultipleEvents(sizeof(streamEvent) / sizeof(streamEvent[0]),streamEvent,FALSE,self->chatManager.keepAliveTime,FALSE); - - if(endResult == WSA_WAIT_TIMEOUT){ + _endthreadex(rslt); - if(self->chatManager.keapALiveFlag){ - LPCWSTR mail[2] = {L"184",NULL}; - NicoLiveStream_sendChat(self,L"/keapalive",mail,NULL); - } - } else{ - switch(endResult - WSA_WAIT_EVENT_0){ + return rslt; +} - case 0: - goto restart; +static unsigned int WINAPI NicoLiveHeartBeatThread(void* lpBuffer){ - case 1: - break; + unsigned int rslt = 0; - default: - goto err; + NicoLiveStream_P self = (NicoLiveStream_P)lpBuffer; - } - } - } + rslt = loopCommon(self,self->chatManager.setHeartBeatTimeEvent,self->chatManager.resetEvents[1],self->chatManager.onHeartBeatSend); - } - -err: - _beginthread(NicoLiveError,0,self); - -end: _endthreadex(rslt); return rslt; } +static unsigned int loopCommon(NicoLiveStream_P self,WSAEVENT sendEvent,WSAEVENT resetEvent ,NicoLiveOnSend nicoLiveOnSend){ -static unsigned int WINAPI NicoLiveHeartBeatThread(void* lpBuffer){ - - unsigned int rslt = 0; - - NicoLiveStream_P self = (NicoLiveStream_P)lpBuffer; - + unsigned int rslt; WSAEVENT events[3] = {self->chatManager.deleteEvent,self->chatManager.startEvent,self->chatManager.endEvent}; //ƒCƒxƒ“ƒg”z—ñ - WSAEVENT streamEvent[2] = {self->chatManager.endEvent,self->chatManager.setHeartBeatTimeEvent}; + WSAEVENT streamEvent[2] = {self->chatManager.endEvent,sendEvent}; DWORD dwResult; //ƒCƒxƒ“ƒgŽóMŒ‹‰Ê DWORD endResult; while(TRUE){ restart: - WSASetEvent(self->chatManager.resetEvents[1]); + WSASetEvent(resetEvent); dwResult = WSAWaitForMultipleEvents(sizeof(events) / sizeof(events[0]),events,FALSE,WSA_INFINITE,FALSE); @@ -1446,7 +1375,7 @@ restart: goto end; case 1: - WSAResetEvent(self->chatManager.resetEvents[1]); + WSAResetEvent(resetEvent); break; case 2: @@ -1460,14 +1389,15 @@ restart: } while(TRUE){ - WSAResetEvent(self->chatManager.setHeartBeatTimeEvent); + WSAResetEvent(sendEvent); endResult = WSAWaitForMultipleEvents(sizeof(streamEvent) / sizeof(streamEvent[0]),streamEvent,FALSE,self->chatManager.heartBeatTime,FALSE); if(endResult == WSA_WAIT_TIMEOUT){ - if(self->chatManager.heartBeatFlag){ - NicoLiveStream_sendHeartBeat(self); - } + nicoLiveOnSend(self); + //dumpln(TEXT("onSend")); + + _heapmin(); } else{ switch(endResult - WSA_WAIT_EVENT_0){ @@ -1488,16 +1418,31 @@ restart: } +end: + return rslt; + err: _beginthread(NicoLiveError,0,self); + rslt = 1; + goto end; +} -end: +static VOID OnSendHeartBeat(NicoLiveStream_P self){ - _endthreadex(rslt); + if(self->chatManager.heartBeatFlag){ + NicoLiveStream_sendHeartBeat(self); + } - return rslt; } +static VOID OnSendKeepAllive(NicoLiveStream_P self){ + + if(self->chatManager.keapALiveFlag){ + LPCWSTR mail[2] = {L"184",NULL}; + NicoLiveStream_sendChat(self,L"/keapalive",mail,NULL); + } + +} #define NICOLIVE_RECEVE_CHAT_LENGTH LENGTH_2048 #define NICOLIVE_REQUEST_CHAT_LENGTH LENGTH_256 @@ -1637,6 +1582,9 @@ end: } + + + static BOOL NicoLiveRecvChatLoop(NicoLiveStream_P self){ @@ -1753,6 +1701,8 @@ end: } + + static VOID NicoLiveParseComment(NicoLiveStream_P self,LPWSTR commentBuf,UINT_PTR endSize,LPWSTR* mailCountBuf,SIZE_T* mailCountBufSize,SIZE_T* commentCountSum,SIZE_T* commentBufSize){ LPWSTR indexPtr; //‰ðÍŽž‚ÌŽw•Wƒ|ƒCƒ“ƒ^ @@ -2043,6 +1993,8 @@ static VOID NicoLiveParseComment(NicoLiveStream_P self,LPWSTR commentBuf,UINT_P } + + static void NicoLiveError(void *buf){ NicoLiveStream_P nl = (NicoLiveStream_P)buf; diff --git a/nlib/nlib_nicoLive.h b/nlib/nlib_nicoLive.h index 4b42b80..c6b073a 100644 --- a/nlib/nlib_nicoLive.h +++ b/nlib/nlib_nicoLive.h @@ -8,6 +8,7 @@ typedef UINT_PTR PROVIDERTYPE; +typedef VOID(*NicoLiveOnSend)(NicoLiveStream_P self); // //ƒjƒRƒjƒR¶•ú‘—ƒIƒuƒWƒFƒNƒg @@ -63,9 +64,11 @@ typedef struct { WSAEVENT setHeartBeatTimeEvent; DWORD heartBeatTime; BOOL heartBeatFlag; + NicoLiveOnSend onHeartBeatSend; WSAEVENT setKeepAliveTimeEvent; DWORD keepAliveTime; BOOL keapALiveFlag; + NicoLiveOnSend onKeepAlliveSend; WSAEVENT deleteEvent; WSAEVENT startEvent; WSAEVENT endEvent; diff --git a/nlib_driver/nlib_driver.cpp b/nlib_driver/nlib_driver.cpp index b23c7e0..52dd688 100644 --- a/nlib_driver/nlib_driver.cpp +++ b/nlib_driver/nlib_driver.cpp @@ -161,7 +161,7 @@ NLIB_RESULT callback(NICOLIVE_EVENT eventType,NicoLiveStream_P pNicoLiveStream,L return 0; } -NLIB_RESULT myListCallBack(LPVOID option,NicoMyListItem_P pVideo){ +NLIB_RESULT myListCallBack(LPVOID option,NicoMyListItem_P pVideo,NLIB_RESULT rslt){ @@ -178,8 +178,8 @@ void nicoLive_connect(void) { SetResFrom(1000); - NicoLiveStream_setHeartBeatMsec(nl,1); - NicoLiveStream_setKeapALiveMsec(nl,1); + NicoLiveStream_setHeartBeatMsec(nl,5000); + NicoLiveStream_setKeapALiveMsec(nl,5000); //NicoLiveStream_setKeapALiveFlag(nl,FALSE); //CU_ASSERT_EQUAL(NicoLiveStream_connect(nl,nva,L"lv66406941",callback,NULL), NLIB_ERR_OK); @@ -210,7 +210,7 @@ void nicoLive_connect(void) { NicoLiveStream_disConnect(nl); } - + CU_ASSERT_EQUAL(_heapchk(),_HEAPOK); //Sleep(3000); @@ -232,7 +232,9 @@ void nicoLive_mylist(void) { - CU_ASSERT_EQUAL(NicoMyList_getDefList(nva,myListCallBack), NLIB_ERR_OK); + CU_ASSERT_EQUAL(NicoMyList_getDefList(nva,myListCallBack,FALSE,NULL), NLIB_ERR_OK); + check(_heapchk() == _HEAPOK,TEXT("heapƒGƒ‰[‚Å‚·")); + dumpln(TEXT("heapchk:%d"),_heapchk()); } -- 2.11.0