From 3e2a36f2a8c64ddab921c53756c44c3e5c54f5c0 Mon Sep 17 00:00:00 2001 From: qwerty2501 Date: Thu, 27 Oct 2011 23:50:30 +0900 Subject: [PATCH] 2011/10/27 23:50:30 --- nlib/nlib.h | 19 +- nlib/nlib_common.c | 36 +-- nlib/nlib_common.h | 10 + nlib/nlib_myList.c | 8 +- nlib/nlib_nicoLive.c | 726 ++++++++++++++++++++++---------------------- nlib/nlib_nicoLive.h | 8 +- nlib_driver/nlib_driver.cpp | 65 ++-- 7 files changed, 447 insertions(+), 425 deletions(-) diff --git a/nlib/nlib.h b/nlib/nlib.h index bd3819c..513bc25 100644 --- a/nlib/nlib.h +++ b/nlib/nlib.h @@ -1,6 +1,7 @@ #pragma once #include +#include #ifdef NLIB_EXPORTS @@ -187,7 +188,7 @@ extern "C" { /// ///nlib‰Šú‰»ŠÖ” /// - NLIB_DECLSPEC NLIB_RESULT InitializeNLIB(); + NLIB_DECLSPEC NLIB_RESULT InitializeNLIB(HINTERNET hInternet); /// ///nlibŒãŽn––ŠÖ” @@ -233,46 +234,46 @@ extern "C" { /// ///ƒn[ƒgƒr[ƒg‚ÌŠÔŠu‚ðÝ’èiƒ~ƒŠ•b) /// - NLIB_DECLSPEC VOID SetHeartBeatMsec(NicoLiveStream_P self,DWORD msec); + NLIB_DECLSPEC VOID NicoLiveStream_setHeartBeatMsec(NicoLiveStream_P self,DWORD msec); /// ///ƒn[ƒgƒr[ƒg‚ÌŠÔŠu‚ðŽæ“¾(ƒ~ƒŠ•b) /// - NLIB_DECLSPEC DWORD GetHeartBeatMsec(NicoLiveStream_P self); + NLIB_DECLSPEC DWORD NicoLiveStream_getHeartBeatMsec(NicoLiveStream_P self); /// ///ƒn[ƒgƒr[ƒg—LŒøE–³ŒøÝ’è /// - NLIB_DECLSPEC VOID SetHeartBeatFlag(NicoLiveStream_P self,BOOL flag); + NLIB_DECLSPEC VOID NicoLiveStream_setHeartBeatFlag(NicoLiveStream_P self,BOOL flag); /// ///ƒn[ƒgƒr[ƒg—LŒøƒtƒ‰ƒOŽæ“¾ /// - NLIB_DECLSPEC BOOL GetHeartBeatFlag(NicoLiveStream_P self); + NLIB_DECLSPEC BOOL NicoLiveStream_getHeartBeatFlag(NicoLiveStream_P self); /// ///ƒL[ƒvƒAƒ‰ƒCƒu‚ÌŠÔŠu‚ðÝ’è(ƒ~ƒŠ•b) /// - NLIB_DECLSPEC VOID SetKeapALiveMsec(NicoLiveStream_P self,DWORD msec); + NLIB_DECLSPEC VOID NicoLiveStream_setKeapALiveMsec(NicoLiveStream_P self,DWORD msec); /// ///ƒL[ƒvƒAƒ‰ƒCƒu‚ÌŠÔŠu‚ðŽæ“¾(ƒ~ƒŠ•b) /// - NLIB_DECLSPEC DWORD GetKeapALiveMsec(NicoLiveStream_P self); + NLIB_DECLSPEC DWORD NicoLiveStream_getKeapALiveMsec(NicoLiveStream_P self); /// ///ƒL[ƒvƒAƒ‰ƒCƒu—LŒøE–³ŒøÝ’è /// - NLIB_DECLSPEC VOID SetKeapALiveFlag(NicoLiveStream_P self,BOOL flag); + NLIB_DECLSPEC VOID NicoLiveStream_setKeapALiveFlag(NicoLiveStream_P self,BOOL flag); /// ///ƒL[ƒvƒAƒ‰ƒCƒuƒtƒ‰ƒOŽæ“¾ /// - NLIB_DECLSPEC BOOL GetKeapALiveFlag(NicoLiveStream_P self); + NLIB_DECLSPEC BOOL NicoLiveStream_getKeapALiveFlag(NicoLiveStream_P self); /// diff --git a/nlib/nlib_common.c b/nlib/nlib_common.c index 25f86e2..04dde92 100644 --- a/nlib/nlib_common.c +++ b/nlib/nlib_common.c @@ -1,43 +1,35 @@ #include "nlib_include.h" -static WSADATA wsaData; //wsadata + HINTERNET hHttpSession; //HINTERNET +HINTERNET hWatchLiveNicoConnect; +HINTERNET hNicoVideoConnect; WCHAR appDataPath[_MAX_PATH]; WCHAR localAppDataPath[_MAX_PATH]; -NLIB_RESULT InitializeNLIB(){ - - INT_PTR err; - INT_PTR count = 0; - - //WSAData‚ð‰Šú‰» - while((err = WSAStartup(MAKEWORD(2,0),&wsaData)) == WSASYSNOTREADY){ +NLIB_RESULT InitializeNLIB(HINTERNET hInternet){ - count++; - if(count >= 5)break; - Sleep(500); + + - - } - - if(err != ERROR_SUCCESS){ + + hHttpSession = hInternet; + if(hHttpSession == NULL){ return NLIB_INITALIZE_FAILED_NETWORK; - } + } - //httpƒnƒ“ƒhƒ‹‚̍쐬 - hHttpSession = WinHttpOpen(USER_AGENT,WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,(LPCWSTR)WINHTTP_NO_PROXY_NAME,(LPCWSTR)WINHTTP_NO_PROXY_BYPASS,0); + hWatchLiveNicoConnect = WinHttpConnect(hHttpSession,WTEXT(WATCH_LIVE_NICO_VIDEO_DOMEINNAME),INTERNET_DEFAULT_HTTP_PORT,0); if(hHttpSession == NULL){ return NLIB_INITALIZE_FAILED_NETWORK; - } - + hNicoVideoConnect = WinHttpConnect(hHttpSession,WWW_NICO_VIDEO_DOMEINNAME,INTERNET_DEFAULT_HTTP_PORT,0); //appData‚̃pƒX‚ðƒZƒbƒg SHGetFolderPathW(0,CSIDL_APPDATA,0,SHGFP_TYPE_CURRENT,appDataPath); @@ -70,10 +62,10 @@ VOID FinalizeNLIB(){ //winHttp‚̌㏈— - WinHttpCloseHandle(hHttpSession); + //WinHttpCloseHandle(hHttpSession); //winsock‚̌㏈— - WSACleanup(); + //WSACleanup(); return; } diff --git a/nlib/nlib_common.h b/nlib/nlib_common.h index 83070fe..8fda34a 100644 --- a/nlib/nlib_common.h +++ b/nlib/nlib_common.h @@ -21,6 +21,16 @@ typedef struct nicovideoauth_t{ /// extern HINTERNET hHttpSession; //HINTERNET +/// +///watch.live.nicoƒRƒlƒNƒVƒ‡ƒ“ƒIƒuƒWƒFƒNƒg +/// +extern HINTERNET hWatchLiveNicoConnect; + + +/// +///nicovideoƒRƒlƒNƒVƒ‡ƒ“ƒIƒuƒWƒFƒNƒg +/// +extern HINTERNET hNicoVideoConnect; /// ///appDataƒtƒHƒ‹ƒ_ diff --git a/nlib/nlib_myList.c b/nlib/nlib_myList.c index a895100..62aa76e 100644 --- a/nlib/nlib_myList.c +++ b/nlib/nlib_myList.c @@ -61,7 +61,7 @@ static NLIB_RESULT NicoMyList_getList(NicoVideoAuth_P pAuth,LPCWSTR objectName,N //•Ï”éŒ¾A‰Šú‰» NLIB_RESULT rslt = NLIB_ERR_OK; - HINTERNET hNicoVideoSession = NULL; + //HINTERNET hNicoVideoSession = NULL; HINTERNET hGetMyList = NULL; WCHAR sendBuffer[LENGTH_512] = {L"Cookie: "}; DWORD statusCode; @@ -75,9 +75,9 @@ static NLIB_RESULT NicoMyList_getList(NicoVideoAuth_P pAuth,LPCWSTR objectName,N ZeroMemory(&itemContainer,sizeof(itemContainer)); wcscat(sendBuffer,pAuth->userSession); - hNicoVideoSession = WinHttpConnect(hHttpSession,WWW_NICO_VIDEO_DOMEINNAME,INTERNET_DEFAULT_HTTP_PORT,0); + //hNicoVideoSession = WinHttpConnect(hHttpSession,WWW_NICO_VIDEO_DOMEINNAME,INTERNET_DEFAULT_HTTP_PORT,0); - hGetMyList = WinHttpOpenRequest(hNicoVideoSession,L"GET",objectName,L"1.1",(LPCWSTR)WINHTTP_NO_REFERER,(LPCWSTR*)WINHTTP_DEFAULT_ACCEPT_TYPES,0); + hGetMyList = WinHttpOpenRequest(hNicoVideoConnect,L"GET",objectName,L"1.1",(LPCWSTR)WINHTTP_NO_REFERER,(LPCWSTR*)WINHTTP_DEFAULT_ACCEPT_TYPES,0); if(WinHttpSendRequest(hGetMyList,sendBuffer,-1,WINHTTP_NO_REQUEST_DATA,0,0,0) == FALSE){ @@ -128,7 +128,7 @@ end: free(readBuffer); JSONParser_delete(&jsonParser); NicoMyListItem_delete(&itemContainer.pitem); - WinHttpCloseHandle(hNicoVideoSession); + //WinHttpCloseHandle(hNicoVideoSession); WinHttpCloseHandle(hGetMyList); return rslt; diff --git a/nlib/nlib_nicoLive.c b/nlib/nlib_nicoLive.c index e9a349d..4d0c94b 100644 --- a/nlib/nlib_nicoLive.c +++ b/nlib/nlib_nicoLive.c @@ -120,7 +120,7 @@ static CRITICAL_SECTION heartBeatCs; /// ///ƒvƒŒƒCƒ„[ƒXƒe[ƒ^ƒXŽæ“¾ /// -static INLINE NLIB_RESULT GetPlayerStatus(NicoLivePlayerStatus_P pPlayerStatus,LPCWSTR liveNO,LPCWSTR userSession,HINTERNET hConnect); +static INLINE NLIB_RESULT GetPlayerStatus(NicoLivePlayerStatus_P pPlayerStatus,LPCWSTR userSession,HINTERNET hConnect,LPVOID buffer); /// ///nicoLiveHistoryŽæ“¾ @@ -268,6 +268,8 @@ NicoLiveStream_P NicoLiveStream_new(){ self->chatManager.keapALiveFlag = TRUE; + InitializeCriticalSection(&self->sessionManager.cs); + dumpln(TEXT("size%d"),sizeof(NicoLiveStream)); @@ -313,6 +315,8 @@ VOID NicoLiveStream_delete(NicoLiveStream_P* self){ //Œãˆ—&”jŠü + DeleteCriticalSection(&(*self)->sessionManager.cs); + WSACloseEvent((*self)->chatManager.setHeartBeatTimeEvent); WSACloseEvent((*self)->chatManager.setKeepAliveTimeEvent); @@ -343,7 +347,7 @@ VOID NicoLiveStream_delete(NicoLiveStream_P* self){ /// ///ƒn[ƒgƒr[ƒg‚ÌŠÔŠu‚ðÝ’èiƒ~ƒŠ•b) /// -VOID SetHeartBeatMsec(NicoLiveStream_P self,DWORD msec){ +VOID NicoLiveStream_setHeartBeatMsec(NicoLiveStream_P self,DWORD msec){ WSASetEvent(self->chatManager.setHeartBeatTimeEvent); self->chatManager.heartBeatTime = msec; @@ -353,7 +357,7 @@ VOID SetHeartBeatMsec(NicoLiveStream_P self,DWORD msec){ /// ///ƒn[ƒgƒr[ƒg‚ÌŠÔŠu‚ðŽæ“¾(ƒ~ƒŠ•b) /// -DWORD GetHeartBeatMsec(NicoLiveStream_P self){ +DWORD NicoLiveStream_getHeartBeatMsec(NicoLiveStream_P self){ return self->chatManager.heartBeatTime; @@ -363,7 +367,7 @@ DWORD GetHeartBeatMsec(NicoLiveStream_P self){ /// ///ƒn[ƒgƒr[ƒg—LŒøE–³ŒøÝ’è /// -VOID SetHeartBeatFlag(NicoLiveStream_P self,BOOL flag){ +VOID NicoLiveStream_setHeartBeatFlag(NicoLiveStream_P self,BOOL flag){ self->chatManager.heartBeatFlag = flag; @@ -373,7 +377,7 @@ VOID SetHeartBeatFlag(NicoLiveStream_P self,BOOL flag){ /// ///ƒn[ƒgƒr[ƒg—LŒøƒtƒ‰ƒOŽæ“¾ /// -BOOL GetHeartBeatFlag(NicoLiveStream_P self){ +BOOL NicoLiveStream_getHeartBeatFlag(NicoLiveStream_P self){ return self->chatManager.heartBeatFlag; @@ -384,7 +388,7 @@ BOOL GetHeartBeatFlag(NicoLiveStream_P self){ /// ///ƒL[ƒvƒAƒ‰ƒCƒu‚ÌŠÔŠu‚ðÝ’è(ƒ~ƒŠ•b) /// -VOID SetKeapALiveMsec(NicoLiveStream_P self,DWORD msec){ +VOID NicoLiveStream_setKeapALiveMsec(NicoLiveStream_P self,DWORD msec){ WSASetEvent(self->chatManager.setKeepAliveTimeEvent); self->chatManager.keepAliveTime = msec; @@ -396,7 +400,7 @@ VOID SetKeapALiveMsec(NicoLiveStream_P self,DWORD msec){ /// ///ƒL[ƒvƒAƒ‰ƒCƒu‚ÌŠÔŠu‚ðŽæ“¾(ƒ~ƒŠ•b) /// -DWORD GetKeapALiveMsec(NicoLiveStream_P self){ +DWORD NicoLiveStream_getKeapALiveMsec(NicoLiveStream_P self){ return self->chatManager.keepAliveTime; @@ -406,7 +410,7 @@ DWORD GetKeapALiveMsec(NicoLiveStream_P self){ /// ///ƒL[ƒvƒAƒ‰ƒCƒu—LŒøE–³ŒøÝ’è /// -VOID SetKeapALiveFlag(NicoLiveStream_P self,BOOL flag){ +VOID NicoLiveStream_setKeapALiveFlag(NicoLiveStream_P self,BOOL flag){ self->chatManager.keapALiveFlag = flag; } @@ -415,7 +419,7 @@ VOID SetKeapALiveFlag(NicoLiveStream_P self,BOOL flag){ /// ///ƒL[ƒvƒAƒ‰ƒCƒuƒtƒ‰ƒOŽæ“¾ /// -BOOL GetKeapALiveFlag(NicoLiveStream_P self){ +BOOL NicoLiveStream_getKeapALiveFlag(NicoLiveStream_P self){ return self->chatManager.keapALiveFlag; @@ -450,7 +454,7 @@ NLIB_RESULT NicoLiveStream_connect(NicoLiveStream_P self,NicoVideoAuth_P nicoVid LPCWSTR psrech = wcsstr(url,L"http://"); LPCWSTR pInputLiveNoEnd; WCHAR liveNo[LIVENO_LENGTH] = {0}; - HINTERNET hWatchLiveNicVideo = NULL; + //HINTERNET hWatchLiveNicVideo = NULL; //‘O‚ɂ‚Ȃ¢‚Å‚¢‚½•ú‘—‚ðØ’f NicoLiveStream_disConnect(self); @@ -507,24 +511,28 @@ NLIB_RESULT NicoLiveStream_connect(NicoLiveStream_P self,NicoVideoAuth_P nicoVid + wcsncpy(self->userSession,L"Cookie: ",sizeof(self->userSession) / sizeof(self->userSession[0]) - 1); + wcsncat(self->userSession,nicoVideoAuth->userSession,sizeof(self->userSession) / sizeof(self->userSession[0]) - 1); + self->userSession[sizeof(self->userSession) / sizeof(self->userSession[0]) - 1] = L'\0'; - wcscpy(self->userSession,nicoVideoAuth->userSession); - //watch.live.nicovideo‚ɐڑ± - hWatchLiveNicVideo = WinHttpConnect(hHttpSession,WTEXT(WATCH_LIVE_NICO_VIDEO_DOMEINNAME),INTERNET_DEFAULT_HTTP_PORT,0); + { + WCHAR objectName[LENGTH_256] = {L"api/getplayerstatus?v="}; + wcsncat(objectName,liveNo,sizeof(objectName) / sizeof(objectName[0])); - if(hWatchLiveNicVideo == NULL){ - rslt = NLIB_ERR_WATCHLIVENICOVIDEO_FAILED_CONNECT; - goto end; - } + + self->sessionManager.getPlayerStatusSession = WinHttpOpenRequest(hWatchLiveNicoConnect,L"GET",objectName,L"1.1",(LPCWSTR)WINHTTP_NO_REFERER,(LPCWSTR*)WINHTTP_DEFAULT_ACCEPT_TYPES,0); + } //ƒvƒŒƒCƒ„[ƒXƒe[ƒ^ƒXŽæ“¾ - rslt = GetPlayerStatus(&self->playerStatus,liveNo,self->userSession,hWatchLiveNicVideo); + EnterCriticalSection(&self->sessionManager.cs); + rslt = GetPlayerStatus(&self->playerStatus,self->userSession,self->sessionManager.getPlayerStatusSession,self->sessionManager.buffer); + LeaveCriticalSection(&self->sessionManager.cs); if(rslt != NLIB_ERR_OK){ @@ -548,7 +556,7 @@ NLIB_RESULT NicoLiveStream_connect(NicoLiveStream_P self,NicoVideoAuth_P nicoVid //GetNicoLiveHistory(self,hWatchLiveNicVideo); end: - WinHttpCloseHandle(hWatchLiveNicVideo); +// WinHttpCloseHandle(hWatchLiveNicVideo); //freeChunkGlobal(chunk); return rslt; @@ -568,43 +576,45 @@ NLIB_RESULT NicoLiveStream_sendHeartBeat(NicoLiveStream_P self){ } + EnterCriticalSection(&self->sessionManager.cs); { - HINTERNET hWatchLiveNicoSession = NULL; - HINTERNET hGetPlayerStatusRequest = NULL; + //HINTERNET hWatchLiveNicoSession = NULL; + //HINTERNET hGetPlayerStatusRequest = NULL; const UINT_PTR buflen = LENGTH_65536; - LPVOID buffer = malloc(buflen); + LPVOID buffer = self->sessionManager.buffer; LPWSTR objectName = (LPWSTR)buffer; LPWSTR indexPtr; - LPWSTR sendBuffer = (LPWSTR)&objectName[LENGTH_4096]; - LPSTR recvBuffer; + //LPWSTR sendBuffer = (LPWSTR)&objectName[LENGTH_4096]; + LPSTR recvBuffer = (LPSTR)&objectName[LENGTH_4096]; DWORD readSize; DWORD statusCode; DWORD statusCodeSize = sizeof(statusCode); - hWatchLiveNicoSession = WinHttpConnect(hHttpSession,WTEXT(WATCH_LIVE_NICO_VIDEO_DOMEINNAME),INTERNET_DEFAULT_HTTP_PORT,0); + //hWatchLiveNicoSession = hWatchLiveNicoConnect; wcscpy(objectName,L"/api/heartbeat?v="); indexPtr = objectName + wcslen(objectName); wcscpy(indexPtr,self->playerStatus.stream.id); - wcscpy(sendBuffer,L"Cookie: "); - indexPtr = sendBuffer + wcslen(sendBuffer); + //wcscpy(sendBuffer,L"Cookie: "); + //indexPtr = sendBuffer + wcslen(sendBuffer); wcscpy(indexPtr,self->userSession); - - hGetPlayerStatusRequest = WinHttpOpenRequest(hWatchLiveNicoSession,L"GET",objectName,L"1.1",(LPCWSTR)WINHTTP_NO_REFERER,(LPCWSTR*)WINHTTP_DEFAULT_ACCEPT_TYPES,0); - - if(WinHttpSendRequest(hGetPlayerStatusRequest,sendBuffer,-1,WINHTTP_NO_REQUEST_DATA,0,0,0) == FALSE){ + if(self->sessionManager.heartBeatSession == NULL){ + self->sessionManager.heartBeatSession = WinHttpOpenRequest(hWatchLiveNicoConnect,L"GET",objectName,L"1.1",(LPCWSTR)WINHTTP_NO_REFERER,(LPCWSTR*)WINHTTP_DEFAULT_ACCEPT_TYPES,0); + + } + if(WinHttpSendRequest(self->sessionManager.heartBeatSession,self->userSession,-1,WINHTTP_NO_REQUEST_DATA,0,0,0) == FALSE){ rslt = NLIB_ERR_FAILED_NETWORK; goto connectend; } - if(WinHttpReceiveResponse(hGetPlayerStatusRequest,NULL) == FALSE){ + if(WinHttpReceiveResponse(self->sessionManager.heartBeatSession,NULL) == FALSE){ rslt = NLIB_GETPLAYERSTATUS_FAILED_CONNECT; goto connectend; @@ -612,7 +622,7 @@ NLIB_RESULT NicoLiveStream_sendHeartBeat(NicoLiveStream_P self){ } - if (WinHttpQueryHeaders(hGetPlayerStatusRequest,WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER,WINHTTP_HEADER_NAME_BY_INDEX,&statusCode,&statusCodeSize,WINHTTP_NO_HEADER_INDEX) == FALSE){ + if (WinHttpQueryHeaders(self->sessionManager.heartBeatSession,WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER,WINHTTP_HEADER_NAME_BY_INDEX,&statusCode,&statusCodeSize,WINHTTP_NO_HEADER_INDEX) == FALSE){ rslt = NLIB_GETPLAYERSTATUS_FAILED_CONNECT; goto connectend; } @@ -624,10 +634,10 @@ NLIB_RESULT NicoLiveStream_sendHeartBeat(NicoLiveStream_P self){ } - recvBuffer = (LPSTR)sendBuffer; + //recvBuffer = (LPSTR)sendBuffer; - if(ReadHttpBody(hGetPlayerStatusRequest,(LPBYTE)recvBuffer,buflen,&readSize) == FALSE){ + if(ReadHttpBody(self->sessionManager.heartBeatSession,(LPBYTE)recvBuffer,buflen,&readSize) == FALSE){ rslt = NLIB_GETPLAYERSTATUS_FAILED_CONNECT; goto connectend; @@ -660,11 +670,12 @@ NLIB_RESULT NicoLiveStream_sendHeartBeat(NicoLiveStream_P self){ } - connectend: - free(buffer); - WinHttpCloseHandle(hWatchLiveNicoSession); - WinHttpCloseHandle(hGetPlayerStatusRequest); + ZeroMemory(self->sessionManager.buffer,sizeof(self->sessionManager.buffer)); + LeaveCriticalSection(&self->sessionManager.cs); + + + } end: @@ -676,8 +687,8 @@ end: VOID NicoLiveStream_disConnect(NicoLiveStream_P self){ - WSAResetEvent(self->chatManager.startEvent); - WSASetEvent(self->chatManager.endEvent); + while(WSAResetEvent(self->chatManager.startEvent) == FALSE); + while(WSASetEvent(self->chatManager.endEvent)== FALSE); @@ -691,6 +702,10 @@ VOID NicoLiveStream_disConnect(NicoLiveStream_P self){ ZeroMemory(&self->playerStatus,sizeof(self->playerStatus)); WSAResetEvent(self->chatManager.endEvent); + WinHttpCloseHandle(self->sessionManager.getPlayerStatusSession); + self->sessionManager.getPlayerStatusSession = NULL; + WinHttpCloseHandle(self->sessionManager.heartBeatSession); + self->sessionManager.heartBeatSession = NULL; @@ -710,117 +725,102 @@ VOID NicoLiveStream_disConnect(NicoLiveStream_P self){ NLIB_RESULT NicoLiveStream_sendChat(NicoLiveStream_P self,LPCWSTR chatbuf,LPCWSTR *mail,LPCWSTR *extends){ - - const UINT_PTR buflen = LENGTH_65536 * 4; - LPVOID chunk = malloc(buflen); - - LPSTR postKey; - LPSTR postKeyBuf = (LPSTR)chunk; - LPWSTR postKeyReqBuf = (LPWSTR)&postKeyBuf[NICOLIVE_SENDCHATBUFLEN]; - LPWSTR cookieBuf = &postKeyReqBuf[NICOLIVE_SENDCHATBUFLEN]; - DWORD statusCode; - DWORD statusCodeSize = sizeof(statusCode); - DWORD readSize; - - HINTERNET hWatchLiveNicoVideo = NULL; - HINTERNET hGetPostKeySession = NULL; NLIB_RESULT rslt = NLIB_ERR_OK; + EnterCriticalSection(&self->sessionManager.cs); + { + const UINT_PTR buflen = LENGTH_65536 * 4; + LPVOID chunk = self->sessionManager.buffer; - if(chatbuf == NULL || wcslen(chatbuf) >= LENGTH_1024){ - - rslt = NLIB_ERR_BUFFER_TOOLITTLE; - goto end; - - } - - - //postkeyŽæ“¾ - _swprintf(postKeyReqBuf,L"api/getpostkey?thread=%u&block_no=%u",self->playerStatus.ms.thread,self->chatManager.commentCount / 100); + LPSTR postKey; + LPSTR postKeyBuf = (LPSTR)chunk; + LPWSTR postKeyReqBuf = (LPWSTR)&postKeyBuf[NICOLIVE_SENDCHATBUFLEN]; + LPWSTR cookieBuf = &postKeyReqBuf[NICOLIVE_SENDCHATBUFLEN]; + DWORD statusCode; + DWORD statusCodeSize = sizeof(statusCode); + DWORD readSize; - hWatchLiveNicoVideo = WinHttpConnect(hHttpSession,WTEXT(WATCH_LIVE_NICO_VIDEO_DOMEINNAME),INTERNET_DEFAULT_HTTP_PORT,0); - hGetPostKeySession = WinHttpOpenRequest(hWatchLiveNicoVideo,L"GET",postKeyReqBuf,L"1.1",(LPCWSTR)WINHTTP_NO_REFERER,(LPCWSTR*)WINHTTP_DEFAULT_ACCEPT_TYPES,0); + //HINTERNET hWatchLiveNicoVideo = NULL; + HINTERNET hGetPostKeySession = NULL; + + if(chatbuf == NULL || wcslen(chatbuf) >= LENGTH_1024){ - //_swprintf(cookieBuf,L"Cookie: %s; %s",self->userSession,self->nicoLiveHistory); - _swprintf(cookieBuf,L"Cookie: %s",self->userSession); + rslt = NLIB_ERR_BUFFER_TOOLITTLE; + goto end; - if(WinHttpSendRequest(hGetPostKeySession,cookieBuf,-1,WINHTTP_NO_REQUEST_DATA,0,0,0) == FALSE){ + } - rslt = NLIB_GETPLAYERSTATUS_FAILED_CONNECT; - goto end; - } + //postkeyŽæ“¾ + _swprintf(postKeyReqBuf,L"api/getpostkey?thread=%u&block_no=%u",self->playerStatus.ms.thread,self->chatManager.commentCount / 100); + //hWatchLiveNicoVideo = WinHttpConnect(hHttpSession,WTEXT(WATCH_LIVE_NICO_VIDEO_DOMEINNAME),INTERNET_DEFAULT_HTTP_PORT,0); + hGetPostKeySession = WinHttpOpenRequest(hWatchLiveNicoConnect,L"GET",postKeyReqBuf,L"1.1",(LPCWSTR)WINHTTP_NO_REFERER,(LPCWSTR*)WINHTTP_DEFAULT_ACCEPT_TYPES,0); - if(WinHttpReceiveResponse(hGetPostKeySession,NULL) == FALSE){ - rslt = NLIB_GETPLAYERSTATUS_FAILED_CONNECT; - goto end; + //_swprintf(cookieBuf,L"Cookie: %s; %s",self->userSession,self->nicoLiveHistory); + _swprintf(cookieBuf,L"Cookie: %s",self->userSession); - } + if(WinHttpSendRequest(hGetPostKeySession,self->userSession,-1,WINHTTP_NO_REQUEST_DATA,0,0,0) == FALSE){ + rslt = NLIB_GETPLAYERSTATUS_FAILED_CONNECT; + goto end; - if (WinHttpQueryHeaders(hGetPostKeySession,WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER,WINHTTP_HEADER_NAME_BY_INDEX,&statusCode,&statusCodeSize,WINHTTP_NO_HEADER_INDEX) == FALSE){ - rslt = NLIB_GETPLAYERSTATUS_FAILED_CONNECT; - goto end; - } + } - if (HTTP_STATUS_OK != statusCode){ - rslt = NLIB_GETPLAYERSTATUS_FAILED_CONNECT; - goto end; - } + if(WinHttpReceiveResponse(hGetPostKeySession,NULL) == FALSE){ + rslt = NLIB_GETPLAYERSTATUS_FAILED_CONNECT; + goto end; + } + if (WinHttpQueryHeaders(hGetPostKeySession,WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER,WINHTTP_HEADER_NAME_BY_INDEX,&statusCode,&statusCodeSize,WINHTTP_NO_HEADER_INDEX) == FALSE){ + rslt = NLIB_GETPLAYERSTATUS_FAILED_CONNECT; + goto end; + } - if(ReadHttpBody(hGetPostKeySession,(LPBYTE)postKeyBuf,buflen,&readSize) == FALSE){ - rslt = NLIB_GETPLAYERSTATUS_FAILED_CONNECT; - goto end; + if (HTTP_STATUS_OK != statusCode){ + rslt = NLIB_GETPLAYERSTATUS_FAILED_CONNECT; + goto end; + } - } - postKeyBuf[readSize] = '\0'; - postKey = strstr(postKeyBuf,"=") + 1; - { - LPSTR buffer = (LPSTR)&cookieBuf[NICOLIVE_SENDCHATBUFLEN]; - LPSTR indexPtr; - LPSTR mailBuf; - LPSTR extendBuf; - - //mailƒRƒ}ƒ“ƒhì¬ - if(mail == NULL){ - mailBuf = ""; - } else { - INT_PTR len; - mailBuf = (LPSTR)postKeyReqBuf; - indexPtr = mailBuf; - strcpy(indexPtr,"mail=\""); - indexPtr += strlen(indexPtr); + if(ReadHttpBody(hGetPostKeySession,(LPBYTE)postKeyBuf,buflen,&readSize) == FALSE){ - len = NICOLIVE_SENDCHATBUFLEN - (indexPtr - mailBuf) - 1; - if(GetLenToMB(CP_UTF8,mail[0]) >= len){ - rslt = NLIB_ERR_BUFFER_TOOLITTLE; - goto end; - - } + rslt = NLIB_GETPLAYERSTATUS_FAILED_CONNECT; + goto end; - WideToMB(CP_UTF8,mail[0],indexPtr,len); - indexPtr += strlen(indexPtr); + } + postKeyBuf[readSize] = '\0'; + postKey = strstr(postKeyBuf,"=") + 1; - for(mail++;mail[0] != NULL;mail++){ + { + LPSTR buffer = (LPSTR)&cookieBuf[NICOLIVE_SENDCHATBUFLEN]; + LPSTR indexPtr; + LPSTR mailBuf; + LPSTR extendBuf; + + //mailƒRƒ}ƒ“ƒhì¬ + if(mail == NULL){ - indexPtr[0] = ' '; - indexPtr++; + mailBuf = ""; - len = NICOLIVE_SENDCHATBUFLEN - (indexPtr - mailBuf) - 1; + } else { + INT_PTR len; + mailBuf = (LPSTR)postKeyReqBuf; + indexPtr = mailBuf; + strcpy(indexPtr,"mail=\""); + indexPtr += strlen(indexPtr); + len = NICOLIVE_SENDCHATBUFLEN - (indexPtr - mailBuf) - 1; if(GetLenToMB(CP_UTF8,mail[0]) >= len){ rslt = NLIB_ERR_BUFFER_TOOLITTLE; goto end; @@ -830,55 +830,42 @@ NLIB_RESULT NicoLiveStream_sendChat(NicoLiveStream_P self,LPCWSTR chatbuf,LPCWST WideToMB(CP_UTF8,mail[0],indexPtr,len); indexPtr += strlen(indexPtr); - } - strcpy(indexPtr,"\""); + for(mail++;mail[0] != NULL;mail++){ + indexPtr[0] = ' '; + indexPtr++; + len = NICOLIVE_SENDCHATBUFLEN - (indexPtr - mailBuf) - 1; - } + if(GetLenToMB(CP_UTF8,mail[0]) >= len){ + rslt = NLIB_ERR_BUFFER_TOOLITTLE; + goto end; - //Šg’£‘®«ì¬ - if(extends == NULL){ + } - extendBuf = ""; + WideToMB(CP_UTF8,mail[0],indexPtr,len); + indexPtr += strlen(indexPtr); - } else { - INT_PTR len; - extendBuf =(LPSTR)cookieBuf; - indexPtr = extendBuf; + } + strcpy(indexPtr,"\""); - len = NICOLIVE_SENDCHATBUFLEN - (indexPtr - extendBuf) - 1; - if(GetLenToMB(CP_UTF8,extends[0]) >= len){ - rslt = NLIB_ERR_BUFFER_TOOLITTLE; - goto end; } - WideToMB(CP_UTF8,extends[0],indexPtr,len); - - indexPtr += strlen(indexPtr); - strcpy(indexPtr,"=\""); - indexPtr += strlen(indexPtr); + //Šg’£‘®«ì¬ + if(extends == NULL){ + extendBuf = ""; - len = NICOLIVE_SENDCHATBUFLEN - (indexPtr - extendBuf) - 1; - - if(GetLenToMB(CP_UTF8,extends[1]) >= len){ - rslt = NLIB_ERR_BUFFER_TOOLITTLE; - goto end; - - } - WideToMB(CP_UTF8,extends[1],indexPtr,len); + } else { + INT_PTR len; + extendBuf =(LPSTR)cookieBuf; + indexPtr = extendBuf; - for(extends+= 2;extends[0] != NULL;extends+=2){ - - indexPtr[0] = ' '; - indexPtr++; - len = NICOLIVE_SENDCHATBUFLEN - (indexPtr - extendBuf) - 1; if(GetLenToMB(CP_UTF8,extends[0]) >= len){ @@ -890,8 +877,8 @@ NLIB_RESULT NicoLiveStream_sendChat(NicoLiveStream_P self,LPCWSTR chatbuf,LPCWST indexPtr += strlen(indexPtr); - indexPtr[0] = '='; - indexPtr++; + strcpy(indexPtr,"=\""); + indexPtr += strlen(indexPtr); len = NICOLIVE_SENDCHATBUFLEN - (indexPtr - extendBuf) - 1; @@ -904,154 +891,185 @@ NLIB_RESULT NicoLiveStream_sendChat(NicoLiveStream_P self,LPCWSTR chatbuf,LPCWST WideToMB(CP_UTF8,extends[1],indexPtr,len); + for(extends+= 2;extends[0] != NULL;extends+=2){ - } - indexPtr += strlen(indexPtr); - strcpy(indexPtr,"\""); + indexPtr[0] = ' '; + indexPtr++; + len = NICOLIVE_SENDCHATBUFLEN - (indexPtr - extendBuf) - 1; + if(GetLenToMB(CP_UTF8,extends[0]) >= len){ + rslt = NLIB_ERR_BUFFER_TOOLITTLE; + goto end; - } + } + WideToMB(CP_UTF8,extends[0],indexPtr,len); - //ƒ`ƒƒƒbƒg‘—MƒRƒƒ“ƒgì¬ - { - indexPtr = buffer; - //–{“–‚Í‚±‚¤‚µ‚½‚¢:"%S" - strcpy(indexPtr,"playerStatus.ms.thread); - indexPtr += strlen(indexPtr); - strcpy(indexPtr,"\" ticket=\""); - indexPtr += strlen(indexPtr); - WideToMB(CP_UTF8,self->ticket,indexPtr,NICOLIVE_SENDCHATBUFLEN - (indexPtr - buffer) - 1); - indexPtr += strlen(indexPtr); - strcpy(indexPtr,"\" postkey=\""); - indexPtr += strlen(indexPtr); - strcpy(indexPtr,postKey); - indexPtr += strlen(indexPtr); - strcpy(indexPtr,"\" user_id=\""); - indexPtr += strlen(indexPtr); - WideToMB(CP_UTF8,self->playerStatus.user.user_id,indexPtr,NICOLIVE_SENDCHATBUFLEN - (indexPtr - buffer) - 1); - indexPtr += strlen(indexPtr); - strcpy(indexPtr,"\" premium=\""); - indexPtr += strlen(indexPtr); - sprintf(indexPtr,"%u",self->playerStatus.user.is_premium); - indexPtr += strlen(indexPtr); - strcpy(indexPtr,"\" locale=\""); - indexPtr += strlen(indexPtr); - WideToMB(CP_UTF8,self->playerStatus.user.userDomain,indexPtr,NICOLIVE_SENDCHATBUFLEN - (indexPtr - buffer) - 1); - indexPtr += strlen(indexPtr); - strcpy(indexPtr,"\" vpos=\""); - indexPtr += strlen(indexPtr); + indexPtr += strlen(indexPtr); + + indexPtr[0] = '='; + indexPtr++; + + + len = NICOLIVE_SENDCHATBUFLEN - (indexPtr - extendBuf) - 1; + + if(GetLenToMB(CP_UTF8,extends[1]) >= len){ + rslt = NLIB_ERR_BUFFER_TOOLITTLE; + goto end; + + } + WideToMB(CP_UTF8,extends[1],indexPtr,len); + + + + } + indexPtr += strlen(indexPtr); + strcpy(indexPtr,"\""); + + + + } + + //ƒ`ƒƒƒbƒg‘—MƒRƒƒ“ƒgì¬ + { + indexPtr = buffer; + //–{“–‚Í‚±‚¤‚µ‚½‚¢:"%S" + strcpy(indexPtr,"playerStatus.ms.thread); + indexPtr += strlen(indexPtr); + strcpy(indexPtr,"\" ticket=\""); + indexPtr += strlen(indexPtr); + WideToMB(CP_UTF8,self->ticket,indexPtr,NICOLIVE_SENDCHATBUFLEN - (indexPtr - buffer) - 1); + indexPtr += strlen(indexPtr); + strcpy(indexPtr,"\" postkey=\""); + indexPtr += strlen(indexPtr); + strcpy(indexPtr,postKey); + indexPtr += strlen(indexPtr); + strcpy(indexPtr,"\" user_id=\""); + indexPtr += strlen(indexPtr); + WideToMB(CP_UTF8,self->playerStatus.user.user_id,indexPtr,NICOLIVE_SENDCHATBUFLEN - (indexPtr - buffer) - 1); + indexPtr += strlen(indexPtr); + strcpy(indexPtr,"\" premium=\""); + indexPtr += strlen(indexPtr); + sprintf(indexPtr,"%u",self->playerStatus.user.is_premium); + indexPtr += strlen(indexPtr); + strcpy(indexPtr,"\" locale=\""); + indexPtr += strlen(indexPtr); + WideToMB(CP_UTF8,self->playerStatus.user.userDomain,indexPtr,NICOLIVE_SENDCHATBUFLEN - (indexPtr - buffer) - 1); + indexPtr += strlen(indexPtr); + strcpy(indexPtr,"\" vpos=\""); + indexPtr += strlen(indexPtr); - timeBeginPeriod(1); - sprintf(indexPtr,"%u",(self->chatManager.baseVpos + ((timeGetTime() - self->chatManager.commentStartTime) / 10))); - timeEndPeriod(1); + timeBeginPeriod(1); + sprintf(indexPtr,"%u",(self->chatManager.baseVpos + ((timeGetTime() - self->chatManager.commentStartTime) / 10))); + timeEndPeriod(1); - indexPtr += strlen(indexPtr); - strcpy(indexPtr,"\""); - indexPtr += strlen(indexPtr); + indexPtr += strlen(indexPtr); + strcpy(indexPtr,"\""); + indexPtr += strlen(indexPtr); - if(strlen(mailBuf) > 0 && strlen(extendBuf) > 0){ + if(strlen(mailBuf) > 0 && strlen(extendBuf) > 0){ - indexPtr[0] = ' '; - indexPtr++; - strcpy(indexPtr,mailBuf); - indexPtr += strlen(indexPtr); + indexPtr[0] = ' '; + indexPtr++; + strcpy(indexPtr,mailBuf); + indexPtr += strlen(indexPtr); - indexPtr[0] = ' '; - indexPtr++; + indexPtr[0] = ' '; + indexPtr++; - strcpy(indexPtr,extendBuf); - indexPtr += strlen(indexPtr); + strcpy(indexPtr,extendBuf); + indexPtr += strlen(indexPtr); - } else if(strlen(mailBuf) > 0){ - indexPtr[0] = ' '; - indexPtr++; - strcpy(indexPtr,mailBuf); - indexPtr += strlen(indexPtr); + } else if(strlen(mailBuf) > 0){ + indexPtr[0] = ' '; + indexPtr++; + strcpy(indexPtr,mailBuf); + indexPtr += strlen(indexPtr); - } else if(strlen(extendBuf) > 0){ - indexPtr[0] = ' '; - indexPtr++; - strcpy(indexPtr,extendBuf); + } else if(strlen(extendBuf) > 0){ + indexPtr[0] = ' '; + indexPtr++; + strcpy(indexPtr,extendBuf); + indexPtr += strlen(indexPtr); + } + strcpy(indexPtr,">"); indexPtr += strlen(indexPtr); - } - strcpy(indexPtr,">"); - indexPtr += strlen(indexPtr); - { - LPWSTR sendBuf = (LPWSTR)&buffer[NICOLIVE_SENDCHATBUFLEN * 4]; - LPWSTR sendPtr = sendBuf; - LPCWSTR endPtr= chatbuf + wcslen(chatbuf); - - - //convertTEXT(indexPtr,chatbuf,indexPtr,endPtr); - - for(;(chatbuf) <= (endPtr);(chatbuf)++){ - switch(chatbuf[0]){ - case L'&': - wcscpy((sendBuf),L"&"); - (sendBuf) += wcslen(sendBuf); - break; - case L'<': - wcscpy((sendBuf),L"<"); - (sendBuf) += wcslen(sendBuf); - break; - case L'>': - wcscpy((sendBuf),L">"); - (sendBuf) += wcslen(sendBuf); - break; - case L'\"': - wcscpy((sendBuf),L"""); - (sendBuf) += wcslen(sendBuf); - break; - case L'\'': - wcscpy((sendBuf),L"'"); - (sendBuf) += wcslen(sendBuf); - break; - default: - sendBuf[0] = chatbuf[0]; - (sendBuf)++; - break; + { + LPWSTR sendBuf = (LPWSTR)&buffer[NICOLIVE_SENDCHATBUFLEN * 4]; + LPWSTR sendPtr = sendBuf; + LPCWSTR endPtr= chatbuf + wcslen(chatbuf); + + + //convertTEXT(indexPtr,chatbuf,indexPtr,endPtr); + + for(;(chatbuf) <= (endPtr);(chatbuf)++){ + switch(chatbuf[0]){ + case L'&': + wcscpy((sendBuf),L"&"); + (sendBuf) += wcslen(sendBuf); + break; + case L'<': + wcscpy((sendBuf),L"<"); + (sendBuf) += wcslen(sendBuf); + break; + case L'>': + wcscpy((sendBuf),L">"); + (sendBuf) += wcslen(sendBuf); + break; + case L'\"': + wcscpy((sendBuf),L"""); + (sendBuf) += wcslen(sendBuf); + break; + case L'\'': + wcscpy((sendBuf),L"'"); + (sendBuf) += wcslen(sendBuf); + break; + default: + sendBuf[0] = chatbuf[0]; + (sendBuf)++; + break; + } } - } - WideToMB(CP_UTF8,sendPtr,indexPtr,NICOLIVE_SENDCHATBUFLEN * 4 - (indexPtr - buffer) - 1); - indexPtr += strlen(indexPtr); + WideToMB(CP_UTF8,sendPtr,indexPtr,NICOLIVE_SENDCHATBUFLEN * 4 - (indexPtr - buffer) - 1); + indexPtr += strlen(indexPtr); - } - strcpy(indexPtr,""); - indexPtr[strlen(indexPtr)] = '\0'; + } + strcpy(indexPtr,""); + indexPtr[strlen(indexPtr)] = '\0'; - { - INT_PTR sendLen = 0; - INT_PTR sentLen = 0; - INT_PTR max = strlen(buffer) + 1; + { + INT_PTR sendLen = 0; + INT_PTR sentLen = 0; + INT_PTR max = strlen(buffer) + 1; - do{ - sendLen = send(self->chatManager.sock,buffer,max - sentLen,0); - sentLen += sendLen; - } while(sendLen > 0); + do{ + sendLen = send(self->chatManager.sock,buffer,max - sentLen,0); + sentLen += sendLen; + } while(sendLen > 0); + } } } + +end: + + WinHttpCloseHandle(hGetPostKeySession); + ZeroMemory(self->sessionManager.buffer,sizeof(self->sessionManager.buffer)); + LeaveCriticalSection(&self->sessionManager.cs); } - -end: - free(chunk); - WinHttpCloseHandle(hGetPostKeySession); - WinHttpCloseHandle(hWatchLiveNicoVideo); - return rslt; } @@ -1068,118 +1086,82 @@ end: -NLIB_RESULT GetPlayerStatus(NicoLivePlayerStatus_P self,LPCWSTR liveNo,LPCWSTR userSession,HINTERNET hConnect){ +NLIB_RESULT GetPlayerStatus(NicoLivePlayerStatus_P self,LPCWSTR userSession,HINTERNET hConnect,LPVOID buffer){ NLIB_RESULT rslt = NLIB_ERR_OK; - HINTERNET hWatchLiveNicoSession = NULL; - HINTERNET hGetPlayerStatusRequest = NULL; - const UINT_PTR buflen = LENGTH_65536; - LPVOID buffer = NULL; - LPWSTR sendBuffer; - LPSTR recvBuffer; - DWORD readSize; - DWORD statusCode; - DWORD statusCodeSize = sizeof(statusCode); - -#define GETPLAYERSTATUS_OBJECTNAME_LENGTH LENGTH_256 - - WCHAR objectName[GETPLAYERSTATUS_OBJECTNAME_LENGTH] = {L"api/getplayerstatus?v="}; - const SIZE_T objectNameLen = wcslen(objectName); - LPWSTR liveNoTmp = &objectName[objectNameLen]; - - if(wcslen(liveNo) > GETPLAYERSTATUS_OBJECTNAME_LENGTH - objectNameLen - 1){ - - rslt = NLIB_LIVENO_FAILED_TOOBIG; - goto end; - - } else if(wcslen(userSession) > LENGTH_4096){ - - rslt = NLIB_USERSESSION_FAILED_TOOBIG; - goto end; - - } - - - buffer = malloc(buflen); - sendBuffer = (LPWSTR)buffer; - wcscpy(sendBuffer,L"Cookie: "); - wcscat(sendBuffer,userSession); - - - wcscpy(liveNoTmp,liveNo); - - if(hConnect == NULL){ - hWatchLiveNicoSession = WinHttpConnect(hHttpSession,WTEXT(WATCH_LIVE_NICO_VIDEO_DOMEINNAME),INTERNET_DEFAULT_HTTP_PORT,0); - } else { - - hWatchLiveNicoSession = hConnect; + + { + //HINTERNET hWatchLiveNicoSession = NULL; + //HINTERNET hGetPlayerStatusRequest = NULL; + const UINT_PTR buflen = LENGTH_65536; + //LPVOID buffer = ; + //LPWSTR sendBuffer; + LPSTR recvBuffer = (LPSTR)buffer; + DWORD readSize; + DWORD statusCode; + DWORD statusCodeSize = sizeof(statusCode); - } - hGetPlayerStatusRequest = WinHttpOpenRequest(hWatchLiveNicoSession,L"GET",objectName,L"1.1",(LPCWSTR)WINHTTP_NO_REFERER,(LPCWSTR*)WINHTTP_DEFAULT_ACCEPT_TYPES,0); + - if(WinHttpSendRequest(hGetPlayerStatusRequest,sendBuffer,-1,WINHTTP_NO_REQUEST_DATA,0,0,0) == FALSE){ + if(WinHttpSendRequest(hConnect,userSession,-1,WINHTTP_NO_REQUEST_DATA,0,0,0) == FALSE){ - rslt = NLIB_GETPLAYERSTATUS_FAILED_CONNECT; - goto end; + rslt = NLIB_GETPLAYERSTATUS_FAILED_CONNECT; + goto end; - } + } - if(WinHttpReceiveResponse(hGetPlayerStatusRequest,NULL) == FALSE){ + if(WinHttpReceiveResponse(hConnect,NULL) == FALSE){ - rslt = NLIB_GETPLAYERSTATUS_FAILED_CONNECT; - goto end; + rslt = NLIB_GETPLAYERSTATUS_FAILED_CONNECT; + goto end; - } + } - if (WinHttpQueryHeaders(hGetPlayerStatusRequest,WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER,WINHTTP_HEADER_NAME_BY_INDEX,&statusCode,&statusCodeSize,WINHTTP_NO_HEADER_INDEX) == FALSE){ - rslt = NLIB_GETPLAYERSTATUS_FAILED_CONNECT; - goto end; - } + if (WinHttpQueryHeaders(hConnect,WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER,WINHTTP_HEADER_NAME_BY_INDEX,&statusCode,&statusCodeSize,WINHTTP_NO_HEADER_INDEX) == FALSE){ + rslt = NLIB_GETPLAYERSTATUS_FAILED_CONNECT; + goto end; + } - if (HTTP_STATUS_OK != statusCode){ - rslt = NLIB_GETPLAYERSTATUS_FAILED_CONNECT; - goto end; - } + if (HTTP_STATUS_OK != statusCode){ + rslt = NLIB_GETPLAYERSTATUS_FAILED_CONNECT; + goto end; + } - recvBuffer = (LPSTR)sendBuffer; + //recvBuffer = (LPSTR)sendBuffer; - if(ReadHttpBody(hGetPlayerStatusRequest,(LPBYTE)recvBuffer,buflen,&readSize) == FALSE){ + if(ReadHttpBody(hConnect,(LPBYTE)recvBuffer,buflen,&readSize) == FALSE){ - rslt = NLIB_GETPLAYERSTATUS_FAILED_CONNECT; - goto end; + rslt = NLIB_GETPLAYERSTATUS_FAILED_CONNECT; + goto end; - } + } - recvBuffer[readSize] = '\0'; + recvBuffer[readSize] = '\0'; - recvBuffer = strstr(recvBuffer,"<"); + recvBuffer = strstr(recvBuffer,"<"); - dumpln_mb(CP_UTF8,"%s",recvBuffer); + dumpln_mb(CP_UTF8,"%s",recvBuffer); - XMLParse(self,recvBuffer,NPlayerStatus_ElementStartHandler,NPlayerStatus_ElementEndHandler,NPlayerStatus_CharacterDataHandler); - rslt = self->error; - if(rslt != NLIB_ERR_OK){ + XMLParse(self,recvBuffer,NPlayerStatus_ElementStartHandler,NPlayerStatus_ElementEndHandler,NPlayerStatus_CharacterDataHandler); + rslt = self->error; + if(rslt != NLIB_ERR_OK){ - goto end; + goto end; + } } - end: - free(buffer); - WinHttpCloseHandle(hGetPlayerStatusRequest); + - //hConnect‚ªNULLŽw’è‚Å“n‚³‚ꂽê‡A‚±‚̊֐”‚Ńnƒ“ƒhƒ‹‚𐶐¬‚µ‚½‚̂ŕ‚¶‚é - if(hConnect == NULL){ - WinHttpCloseHandle(hWatchLiveNicoSession); - } + return rslt; @@ -1193,7 +1175,9 @@ static INLINE NLIB_RESULT GetNicoLiveHistory(NicoLiveStream_P self,HINTERNET hCo NLIB_RESULT rslt = NLIB_ERR_OK; - HINTERNET hNicoVideoConnect = NULL; + EnterCriticalSection(&self->sessionManager.cs); + { + //HINTERNET hNicoVideoConnect = NULL; HINTERNET hStreamBrowse = NULL; @@ -1202,7 +1186,7 @@ static INLINE NLIB_RESULT GetNicoLiveHistory(NicoLiveStream_P self,HINTERNET hCo DWORD statusCode; DWORD statusCodeSize = sizeof(statusCode); DWORD chunkSize = LENGTH_65536; - LPVOID chunkMemory = malloc(chunkSize); + LPVOID chunkMemory =self->sessionManager.buffer; @@ -1219,15 +1203,11 @@ static INLINE NLIB_RESULT GetNicoLiveHistory(NicoLiveStream_P self,HINTERNET hCo wcscat(objectName,self->playerStatus.stream.id); wcscat(cookie,self->userSession); - if(hConnect == NULL){ - - hNicoVideoConnect = WinHttpConnect(hHttpSession,WTEXT(WATCH_LIVE_NICO_VIDEO_DOMEINNAME),INTERNET_DEFAULT_HTTP_PORT,0); - - } else { + - hNicoVideoConnect = hConnect; + //hNicoVideoConnect = hConnect; - } + hStreamBrowse = WinHttpOpenRequest(hNicoVideoConnect,L"GET",objectName,L"1.1",(LPCWSTR)WINHTTP_NO_REFERER,(LPCWSTR*)WINHTTP_DEFAULT_ACCEPT_TYPES,0); @@ -1284,14 +1264,12 @@ static INLINE NLIB_RESULT GetNicoLiveHistory(NicoLiveStream_P self,HINTERNET hCo } end: - free(chunkMemory); - WinHttpCloseHandle(hStreamBrowse); - - if(hConnect == NULL){ - WinHttpCloseHandle(hNicoVideoConnect); + WinHttpCloseHandle(hStreamBrowse); } + ZeroMemory(self->sessionManager.buffer,sizeof(self->sessionManager.buffer)); + LeaveCriticalSection(&self->sessionManager.cs); return rslt; @@ -1363,7 +1341,7 @@ static unsigned int WINAPI NicoLiveKeapALiveThread(void* lpBuffer){ DWORD endResult; while(TRUE){ - +restart: WSASetEvent(self->chatManager.resetEvents[2]); dwResult = WSAWaitForMultipleEvents(sizeof(events) / sizeof(events[0]),events,FALSE,WSA_INFINITE,FALSE); @@ -1383,6 +1361,7 @@ static unsigned int WINAPI NicoLiveKeapALiveThread(void* lpBuffer){ goto end; case 1: + WSAResetEvent(self->chatManager.resetEvents[2]); break; case 2: @@ -1409,7 +1388,7 @@ static unsigned int WINAPI NicoLiveKeapALiveThread(void* lpBuffer){ switch(endResult - WSA_WAIT_EVENT_0){ case 0: - goto loopend; + goto restart; case 1: break; @@ -1420,7 +1399,7 @@ static unsigned int WINAPI NicoLiveKeapALiveThread(void* lpBuffer){ } } } -loopend:; + } @@ -1447,7 +1426,7 @@ static unsigned int WINAPI NicoLiveHeartBeatThread(void* lpBuffer){ DWORD endResult; while(TRUE){ - +restart: WSASetEvent(self->chatManager.resetEvents[1]); dwResult = WSAWaitForMultipleEvents(sizeof(events) / sizeof(events[0]),events,FALSE,WSA_INFINITE,FALSE); @@ -1467,6 +1446,7 @@ static unsigned int WINAPI NicoLiveHeartBeatThread(void* lpBuffer){ goto end; case 1: + WSAResetEvent(self->chatManager.resetEvents[1]); break; case 2: @@ -1492,7 +1472,7 @@ static unsigned int WINAPI NicoLiveHeartBeatThread(void* lpBuffer){ switch(endResult - WSA_WAIT_EVENT_0){ case 0: - goto loopend; + goto restart; case 1: break; @@ -1502,7 +1482,7 @@ static unsigned int WINAPI NicoLiveHeartBeatThread(void* lpBuffer){ } } } -loopend:; + @@ -1563,6 +1543,7 @@ static unsigned int WINAPI NicoLiveRecvChat(void* lpBuffer){ goto end; case 1: + WSAResetEvent(self->chatManager.resetEvents[0]); break; case 2: @@ -1990,11 +1971,13 @@ static VOID NicoLiveParseComment(NicoLiveStream_P self,LPWSTR commentBuf,UINT_P (*commentCountSum)++; isReceveChat = TRUE; - self->callBack(NICOLIVE_EVENT_RECEVE_CHAT,self,self->option,(NICOLIVE_PARAM)&nicoLiveChat,self->chatManager.commentCount); - if(wcscmp(nicoLiveChat.chatBuf,L"/disconnect") == 0 && wcscmp(nicoLiveChat.premium,L"2") == 0){ + if((wcscmp(nicoLiveChat.chatBuf,L"/disconnect") == 0 && wcscmp(nicoLiveChat.premium,L"2") == 0)){ + _beginthread(NicoLiveSelfDisconnect,0,self); } + self->callBack(NICOLIVE_EVENT_RECEVE_CHAT,self,self->option,(NICOLIVE_PARAM)&nicoLiveChat,self->chatManager.commentCount); + //chat_resultƒm[ƒh‚̏ꍇ } else if((indexPtr = wcsstr(commentBuf,L"chatManager.setKeepAliveTimeEvent); + self->callBack(NICOLIVE_EVENT_RECEVE_CHATSENDRESULT,self,self->option,(NICOLIVE_PARAM)&nicoLiveSendResultComment,(NICOLIVE_PARAM)NULL); } @@ -2072,7 +2058,7 @@ static void NicoLiveSelfDisconnect(void *buf){ NicoLiveStream_P nl = (NicoLiveStream_P)buf; - + dumpln(TEXT("selfdisconnect"),); NicoLiveStream_disConnect(nl); diff --git a/nlib/nlib_nicoLive.h b/nlib/nlib_nicoLive.h index 14c7ba6..4b42b80 100644 --- a/nlib/nlib_nicoLive.h +++ b/nlib/nlib_nicoLive.h @@ -105,7 +105,12 @@ typedef struct nicoliveplayerstatus_t{ NLIB_RESULT error; }NicoLivePlayerStatus,*NicoLivePlayerStatus_P; - +typedef struct { + HINTERNET getPlayerStatusSession; + HINTERNET heartBeatSession; + CHAR buffer[LENGTH_65536 * 5]; + CRITICAL_SECTION cs; +}SessionManager,*SessionManager_P; @@ -121,6 +126,7 @@ typedef struct nicolivestream_t{ ChatManager chatManager; NicoRecvCallBack callBack; INT_PTR res_from; + SessionManager sessionManager; NLIB_RESULT error; }NicoLiveStream; diff --git a/nlib_driver/nlib_driver.cpp b/nlib_driver/nlib_driver.cpp index e55b58f..b23c7e0 100644 --- a/nlib_driver/nlib_driver.cpp +++ b/nlib_driver/nlib_driver.cpp @@ -30,11 +30,32 @@ NicoLiveStream_P nl; NicoVideoAuth_P nva; //NlibProperty_P np; - +WSAData wsaData; +HINTERNET hInternet = WinHttpOpen(L"nicoliveTest",WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,(LPCWSTR)WINHTTP_NO_PROXY_NAME,(LPCWSTR)WINHTTP_NO_PROXY_BYPASS,0); int nicoLive_setup(void) { - InitializeNLIB(); - nl = NicoLiveStream_new(); + + INT_PTR err; + INT_PTR count = 0; + + + //WSAData‚ð‰Šú‰» + while((err = WSAStartup(MAKEWORD(2,0),&wsaData)) == WSASYSNOTREADY){ + + count++; + if(count >= 5)break; + Sleep(500); + + + + } + + if(err != ERROR_SUCCESS){ + + return NLIB_INITALIZE_FAILED_NETWORK; + } + InitializeNLIB(hInternet); + nl = NicoLiveStream_new(); nva = NicoVideoAuth_new(); //np = NlibProperty_ref(); @@ -46,6 +67,8 @@ int nicoLive_teardown(void) { NicoLiveStream_delete(&nl); NicoVideoAuth_delete(&nva); FinalizeNLIB(); + WinHttpCloseHandle(hInternet); + WSACleanup(); return 0; } @@ -155,39 +178,43 @@ void nicoLive_connect(void) { SetResFrom(1000); - SetHeartBeatMsec(nl,5000); - SetKeapALiveMsec(nl,5000); - //SetKeapALiveFlag(nl,FALSE); + NicoLiveStream_setHeartBeatMsec(nl,1); + NicoLiveStream_setKeapALiveMsec(nl,1); + //NicoLiveStream_setKeapALiveFlag(nl,FALSE); //CU_ASSERT_EQUAL(NicoLiveStream_connect(nl,nva,L"lv66406941",callback,NULL), NLIB_ERR_OK); WCHAR housoubuf[LENGTH_1024]; std::wcin.getline(housoubuf,sizeof(housoubuf) / sizeof(housoubuf[0])); - CU_ASSERT_EQUAL(NicoLiveStream_connect(nl,nva,housoubuf,callback,NULL), NLIB_ERR_OK); + WCHAR buf[1024]; - std::wcin.getline(buf,sizeof(buf) / sizeof(buf[0])); + - LPWSTR mail = L"184"; - LPWSTR exname = L"name"; - LPWSTR exvalue = L"qwerty"; + for(int index = 0;index < 5;index++){ + CU_ASSERT_EQUAL(NicoLiveStream_connect(nl,nva,housoubuf,callback,NULL), NLIB_ERR_OK); + std::wcin.getline(buf,sizeof(buf) / sizeof(buf[0])); - LPCWSTR mails[2] = {mail,NULL}; - LPCWSTR extends[3] = {exname,exvalue,NULL}; + LPWSTR mail = L"184"; + LPWSTR exname = L"name"; + LPWSTR exvalue = L"qwerty"; + LPCWSTR mails[2] = {mail,NULL}; + LPCWSTR extends[3] = {exname,exvalue,NULL}; + - while(wcslen(buf) > 0){ + while(wcslen(buf) > 0){ - NicoLiveStream_sendChat(nl,buf,mails,NULL); - std::wcin.getline(buf,sizeof(buf) / sizeof(buf[0])); + NicoLiveStream_sendChat(nl,buf,mails,NULL); + std::wcin.getline(buf,sizeof(buf) / sizeof(buf[0])); + } + NicoLiveStream_disConnect(nl); } - - //Sleep(3000); - NicoLiveStream_disConnect(nl); + /* //NlibProperty_setBrowserType(np,BT_FIRE_FOX); CU_ASSERT_EQUAL(NicoLiveStream_connect(nl,BT_FIRE_FOX,L"lv12345",callback,NULL), 0); -- 2.11.0