OSDN Git Service

2011/10/27 23:50:30
authorqwerty2501 <riot313@gmail.com>
Thu, 27 Oct 2011 14:50:30 +0000 (23:50 +0900)
committerqwerty2501 <riot313@gmail.com>
Thu, 27 Oct 2011 14:50:30 +0000 (23:50 +0900)
nlib/nlib.h
nlib/nlib_common.c
nlib/nlib_common.h
nlib/nlib_myList.c
nlib/nlib_nicoLive.c
nlib/nlib_nicoLive.h
nlib_driver/nlib_driver.cpp

index bd3819c..513bc25 100644 (file)
@@ -1,6 +1,7 @@
 #pragma once
 
 #include <qwerty\qwerty_common.h>
+#include <winhttp.h>
 
 #ifdef NLIB_EXPORTS
 
@@ -187,7 +188,7 @@ extern "C" {
        ///
        ///nlib\8f\89\8aú\89»\8aÖ\90\94
        ///
-       NLIB_DECLSPEC NLIB_RESULT InitializeNLIB();
+       NLIB_DECLSPEC NLIB_RESULT InitializeNLIB(HINTERNET hInternet);
 
        ///
        ///nlib\8cã\8en\96\96\8aÖ\90\94
@@ -233,46 +234,46 @@ extern "C" {
        ///
        ///\83n\81[\83g\83r\81[\83g\82Ì\8aÔ\8au\82ð\90Ý\92è\81i\83~\83\8a\95b)
        ///
-       NLIB_DECLSPEC VOID SetHeartBeatMsec(NicoLiveStream_P self,DWORD msec);
+       NLIB_DECLSPEC VOID NicoLiveStream_setHeartBeatMsec(NicoLiveStream_P self,DWORD msec);
 
 
        ///
        ///\83n\81[\83g\83r\81[\83g\82Ì\8aÔ\8au\82ð\8eæ\93¾(\83~\83\8a\95b)
        ///
-       NLIB_DECLSPEC DWORD GetHeartBeatMsec(NicoLiveStream_P self);
+       NLIB_DECLSPEC DWORD NicoLiveStream_getHeartBeatMsec(NicoLiveStream_P self);
 
        ///
        ///\83n\81[\83g\83r\81[\83g\97L\8cø\81E\96³\8cø\90Ý\92è
        ///
-       NLIB_DECLSPEC VOID SetHeartBeatFlag(NicoLiveStream_P self,BOOL flag);
+       NLIB_DECLSPEC VOID NicoLiveStream_setHeartBeatFlag(NicoLiveStream_P self,BOOL flag);
 
 
        ///
        ///\83n\81[\83g\83r\81[\83g\97L\8cø\83t\83\89\83O\8eæ\93¾
        ///
-       NLIB_DECLSPEC BOOL GetHeartBeatFlag(NicoLiveStream_P self);
+       NLIB_DECLSPEC BOOL NicoLiveStream_getHeartBeatFlag(NicoLiveStream_P self);
 
        ///
        ///\83L\81[\83v\83A\83\89\83C\83u\82Ì\8aÔ\8au\82ð\90Ý\92è(\83~\83\8a\95b)
        ///
-       NLIB_DECLSPEC VOID SetKeapALiveMsec(NicoLiveStream_P self,DWORD msec);
+       NLIB_DECLSPEC VOID NicoLiveStream_setKeapALiveMsec(NicoLiveStream_P self,DWORD msec);
 
        ///
        ///\83L\81[\83v\83A\83\89\83C\83u\82Ì\8aÔ\8au\82ð\8eæ\93¾(\83~\83\8a\95b)
        ///
-       NLIB_DECLSPEC DWORD GetKeapALiveMsec(NicoLiveStream_P self);
+       NLIB_DECLSPEC DWORD NicoLiveStream_getKeapALiveMsec(NicoLiveStream_P self);
 
 
        ///
        ///\83L\81[\83v\83A\83\89\83C\83u\97L\8cø\81E\96³\8cø\90Ý\92è
        ///
-       NLIB_DECLSPEC VOID SetKeapALiveFlag(NicoLiveStream_P self,BOOL flag);
+       NLIB_DECLSPEC VOID NicoLiveStream_setKeapALiveFlag(NicoLiveStream_P self,BOOL flag);
 
 
        ///
        ///\83L\81[\83v\83A\83\89\83C\83u\83t\83\89\83O\8eæ\93¾
        ///
-       NLIB_DECLSPEC BOOL GetKeapALiveFlag(NicoLiveStream_P self);
+       NLIB_DECLSPEC BOOL NicoLiveStream_getKeapALiveFlag(NicoLiveStream_P self);
 
 
        ///
index 25f86e2..04dde92 100644 (file)
@@ -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\82ð\8f\89\8aú\89»
-       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\83n\83\93\83h\83\8b\82Ì\8dì\90¬
-       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\82Ì\83p\83X\82ð\83Z\83b\83g
        SHGetFolderPathW(0,CSIDL_APPDATA,0,SHGFP_TYPE_CURRENT,appDataPath);
@@ -70,10 +62,10 @@ VOID FinalizeNLIB(){
 
 
        //winHttp\82Ì\8cã\8f\88\97\9d
-       WinHttpCloseHandle(hHttpSession);
+       //WinHttpCloseHandle(hHttpSession);
 
        //winsock\82Ì\8cã\8f\88\97\9d
-       WSACleanup();
+       //WSACleanup();
 
        return;
 }
index 83070fe..8fda34a 100644 (file)
@@ -21,6 +21,16 @@ typedef struct nicovideoauth_t{
 ///
 extern HINTERNET hHttpSession;                 //HINTERNET
 
+///
+///watch.live.nico\83R\83l\83N\83V\83\87\83\93\83I\83u\83W\83F\83N\83g
+///
+extern HINTERNET hWatchLiveNicoConnect;
+
+
+///
+///nicovideo\83R\83l\83N\83V\83\87\83\93\83I\83u\83W\83F\83N\83g
+///
+extern HINTERNET hNicoVideoConnect;
 
 ///
 ///appData\83t\83H\83\8b\83_
index a895100..62aa76e 100644 (file)
@@ -61,7 +61,7 @@ static NLIB_RESULT NicoMyList_getList(NicoVideoAuth_P pAuth,LPCWSTR objectName,N
 
        //\95Ï\90\94\90é\8c¾\81A\8f\89\8aú\89»
        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;
index e9a349d..4d0c94b 100644 (file)
@@ -120,7 +120,7 @@ static CRITICAL_SECTION heartBeatCs;
 ///
 ///\83v\83\8c\83C\83\84\81[\83X\83e\81[\83^\83X\8eæ\93¾
 ///
-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\8eæ\93¾
@@ -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){
 
        //\8cã\8f\88\97\9d&\94j\8aü
 
+       DeleteCriticalSection(&(*self)->sessionManager.cs);
+
        WSACloseEvent((*self)->chatManager.setHeartBeatTimeEvent);
 
        WSACloseEvent((*self)->chatManager.setKeepAliveTimeEvent);
@@ -343,7 +347,7 @@ VOID NicoLiveStream_delete(NicoLiveStream_P* self){
 ///
 ///\83n\81[\83g\83r\81[\83g\82Ì\8aÔ\8au\82ð\90Ý\92è\81i\83~\83\8a\95b)
 ///
-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){
 ///
 ///\83n\81[\83g\83r\81[\83g\82Ì\8aÔ\8au\82ð\8eæ\93¾(\83~\83\8a\95b)
 ///
-DWORD GetHeartBeatMsec(NicoLiveStream_P self){
+DWORD NicoLiveStream_getHeartBeatMsec(NicoLiveStream_P self){
 
        return self->chatManager.heartBeatTime;
 
@@ -363,7 +367,7 @@ DWORD GetHeartBeatMsec(NicoLiveStream_P self){
 ///
 ///\83n\81[\83g\83r\81[\83g\97L\8cø\81E\96³\8cø\90Ý\92è
 ///
-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){
 ///
 ///\83n\81[\83g\83r\81[\83g\97L\8cø\83t\83\89\83O\8eæ\93¾
 ///
-BOOL GetHeartBeatFlag(NicoLiveStream_P self){
+BOOL NicoLiveStream_getHeartBeatFlag(NicoLiveStream_P self){
 
        return self->chatManager.heartBeatFlag;
 
@@ -384,7 +388,7 @@ BOOL GetHeartBeatFlag(NicoLiveStream_P self){
 ///
 ///\83L\81[\83v\83A\83\89\83C\83u\82Ì\8aÔ\8au\82ð\90Ý\92è(\83~\83\8a\95b)
 ///
-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){
 ///
 ///\83L\81[\83v\83A\83\89\83C\83u\82Ì\8aÔ\8au\82ð\8eæ\93¾(\83~\83\8a\95b)
 ///
-DWORD GetKeapALiveMsec(NicoLiveStream_P self){
+DWORD NicoLiveStream_getKeapALiveMsec(NicoLiveStream_P self){
 
        return self->chatManager.keepAliveTime;
 
@@ -406,7 +410,7 @@ DWORD GetKeapALiveMsec(NicoLiveStream_P self){
 ///
 ///\83L\81[\83v\83A\83\89\83C\83u\97L\8cø\81E\96³\8cø\90Ý\92è
 ///
-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){
 ///
 ///\83L\81[\83v\83A\83\89\83C\83u\83t\83\89\83O\8eæ\93¾
 ///
-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;
 
        //\91O\82É\82Â\82È\82¢\82Å\82¢\82½\95ú\91\97\82ð\90Ø\92f
        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\82É\90Ú\91±
-       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);
+       }
 
        //\83v\83\8c\83C\83\84\81[\83X\83e\81[\83^\83X\8eæ\93¾
-       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\8eæ\93¾
-       _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\8eæ\93¾
+               _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\83R\83}\83\93\83h\8dì\90¬
-               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\83R\83}\83\93\83h\8dì\90¬
+                       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;
 
-               //\8ag\92£\91®\90«\8dì\90¬
-               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);
+                       //\8ag\92£\91®\90«\8dì\90¬
+                       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);
 
-               //\83`\83\83\83b\83g\91\97\90M\83R\83\81\83\93\83g\8dì\90¬
-               {
-                       indexPtr = buffer;
-                       //\96{\93\96\82Í\82±\82¤\82µ\82½\82¢:"<chat thread=\"%d\" ticket=\"%S\"  postkey=\"%s\" user_id=\"%S\" premium=\"%d\" locale=\"%S\" vpos=\"%ld\"%s>%S</chat>"
-                       strcpy(indexPtr,"<chat thread=\"");
-                       indexPtr += strlen(indexPtr);
-                       sprintf(indexPtr,"%u",self->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,"\"");
+
+
+
+                       }
+
+                       //\83`\83\83\83b\83g\91\97\90M\83R\83\81\83\93\83g\8dì\90¬
+                       {
+                               indexPtr = buffer;
+                               //\96{\93\96\82Í\82±\82¤\82µ\82½\82¢:"<chat thread=\"%d\" ticket=\"%S\"  postkey=\"%s\" user_id=\"%S\" premium=\"%d\" locale=\"%S\" vpos=\"%ld\"%s>%S</chat>"
+                               strcpy(indexPtr,"<chat thread=\"");
+                               indexPtr += strlen(indexPtr);
+                               sprintf(indexPtr,"%u",self->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"&amp;");
-                                               (sendBuf) += wcslen(sendBuf);
-                                               break;
-                                       case L'<':
-                                               wcscpy((sendBuf),L"&lt;");
-                                               (sendBuf) += wcslen(sendBuf);
-                                               break;
-                                       case L'>':
-                                               wcscpy((sendBuf),L"&gt;");
-                                               (sendBuf) += wcslen(sendBuf);
-                                               break;
-                                       case L'\"':
-                                               wcscpy((sendBuf),L"&quot;");
-                                               (sendBuf) += wcslen(sendBuf);
-                                               break;
-                                       case L'\'':
-                                               wcscpy((sendBuf),L"&apos;");
-                                               (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"&amp;");
+                                                       (sendBuf) += wcslen(sendBuf);
+                                                       break;
+                                               case L'<':
+                                                       wcscpy((sendBuf),L"&lt;");
+                                                       (sendBuf) += wcslen(sendBuf);
+                                                       break;
+                                               case L'>':
+                                                       wcscpy((sendBuf),L"&gt;");
+                                                       (sendBuf) += wcslen(sendBuf);
+                                                       break;
+                                               case L'\"':
+                                                       wcscpy((sendBuf),L"&quot;");
+                                                       (sendBuf) += wcslen(sendBuf);
+                                                       break;
+                                               case L'\'':
+                                                       wcscpy((sendBuf),L"&apos;");
+                                                       (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,"</chat>");
-                       indexPtr[strlen(indexPtr)] = '\0';
+                               }
+                               strcpy(indexPtr,"</chat>");
+                               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\82ªNULL\8ew\92è\82Å\93n\82³\82ê\82½\8fê\8d\87\81A\82±\82Ì\8aÖ\90\94\82Å\83n\83\93\83h\83\8b\82ð\90\90¬\82µ\82½\82Ì\82Å\95Â\82\82é
-       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\83m\81[\83h\82Ì\8fê\8d\87
                } else if((indexPtr = wcsstr(commentBuf,L"<chat_result ")) != NULL){
@@ -2026,6 +2009,9 @@ static VOID  NicoLiveParseComment(NicoLiveStream_P self,LPWSTR commentBuf,UINT_P
                                skipXMLSPACE(indexPtr);
                        }
 
+                       //\83L\81[\83v\83A\83\89\83C\83u\83\8a\83Z\83b\83g
+                       WSAResetEvent(self->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);
 
index 14c7ba6..4b42b80 100644 (file)
@@ -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;
index e55b58f..b23c7e0 100644 (file)
 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\82ð\8f\89\8aú\89»
+       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);