Changeset ed502d


Ignore:
Timestamp:
12/30/11 14:18:06 (17 months ago)
Author:
Micke Prag <micke.prag@…>
Branches:
('master', '033cf796174446f5fff5bbfad1cbf1e4af35c0d8')('controller-upgrade', '72b31cc86eeeef18f1371a3067b6e8a5ca21abfc')('windows_service_fixes', 'df6bd2788365991d36d5af2a75833b8de2a5860f')
Children:
72b31cc86eeeef18f1371a3067b6e8a5ca21abfc
Parents:
86554b26ac832b1cd56c33d3731bad312f2177b6
git-author:
Carl Nettelblad <cnettel@tele2.se>2011-12-30 14:18:06+01:00
git-committer:
Micke Prag <micke.prag@telldus.se>2011-12-30 14:18:06+01:00
Message:

Fixed for stability in the Windows Service. Mainly changes since CancelIO is
not synchronous. Big thanks to Carl Nettelblad for this.
See #93 and #94.

Location:
telldus-core
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • telldus-core/common/Socket_win.cpp

    r86554b red502d  
    3838        if (d->hPipe != INVALID_HANDLE_VALUE) { 
    3939                CloseHandle(d->hPipe); 
     40                d->hPipe = 0; 
    4041        } 
    4142        delete d; 
     
    9091        memset(&oOverlap, 0, sizeof(OVERLAPPED)); 
    9192 
    92         d->readEvent = CreateEvent(NULL, TRUE, TRUE, NULL); 
     93        d->readEvent = CreateEvent(NULL, TRUE, FALSE, NULL); 
    9394        oOverlap.hEvent = d->readEvent; 
    9495        BOOL fSuccess = false; 
     
    106107                if(!d->running){ 
    107108                        CancelIo(d->hPipe); 
    108                         CloseHandle(d->readEvent); 
     109                        WaitForSingleObject(oOverlap.hEvent, INFINITE); 
     110                        d->readEvent = 0; 
     111                        CloseHandle(oOverlap.hEvent); 
    109112                        return L""; 
    110113                } 
     
    112115                if (result == WAIT_TIMEOUT) { 
    113116                        CancelIo(d->hPipe); 
    114                         CloseHandle(d->readEvent); 
    115                         return L""; 
     117                        // Cancel, we still need to cleanup 
    116118                } 
    117                 fSuccess = GetOverlappedResult(d->hPipe, &oOverlap, &cbBytesRead, false); 
     119                fSuccess = GetOverlappedResult(d->hPipe, &oOverlap, &cbBytesRead, true); 
    118120         
    119121                if (!fSuccess) { 
     
    132134                returnString.append(buf); 
    133135        } 
    134         CancelIo(d->hPipe); 
    135         CloseHandle(d->readEvent); 
     136        d->readEvent = 0; 
     137        CloseHandle(oOverlap.hEvent); 
    136138        return returnString; 
    137139} 
     
    142144        DWORD bytesWritten = 0; 
    143145        int result; 
    144         BOOL fSuccess; 
     146        BOOL fSuccess = false; 
    145147 
    146148        memset(&oOverlap, 0, sizeof(OVERLAPPED)); 
    147149 
    148         HANDLE writeEvent = CreateEvent(NULL, TRUE, TRUE, NULL); 
     150        HANDLE writeEvent = CreateEvent(NULL, TRUE, FALSE, NULL); 
    149151        oOverlap.hEvent = writeEvent; 
    150152         
    151         WriteFile(d->hPipe, msg.data(), (DWORD)msg.length()*sizeof(wchar_t), &bytesWritten, &oOverlap); 
     153        BOOL writeSuccess = WriteFile(d->hPipe, msg.data(), (DWORD)msg.length()*sizeof(wchar_t), &bytesWritten, &oOverlap); 
     154        result = GetLastError(); 
     155        if (writeSuccess || result == ERROR_IO_PENDING) { 
     156                result = WaitForSingleObject(writeEvent, 500); 
     157                if (result == WAIT_TIMEOUT) { 
     158                        CancelIo(d->hPipe); 
     159                        WaitForSingleObject(oOverlap.hEvent, INFINITE); 
     160                        CloseHandle(writeEvent); 
     161                        CloseHandle(d->hPipe); 
     162                        d->hPipe = 0; 
     163                        d->connected = false; 
     164                        return; 
     165                } 
     166                fSuccess = GetOverlappedResult(d->hPipe, &oOverlap, &bytesWritten, TRUE); 
     167        } 
    152168 
    153         result = WaitForSingleObject(writeEvent, 500); 
    154         if (result == WAIT_TIMEOUT) { 
    155                 CancelIo(d->hPipe); 
    156                 CloseHandle(writeEvent); 
    157                 d->connected = false; 
    158                 return; 
    159         } 
    160         fSuccess = GetOverlappedResult(d->hPipe, &oOverlap, &bytesWritten, TRUE); 
    161169        CloseHandle(writeEvent); 
    162170        if (!fSuccess) { 
    163                 CancelIo(d->hPipe); 
     171                CloseHandle(d->hPipe); 
     172                d->hPipe = 0; 
    164173                d->connected = false; 
    165174                return;  
  • telldus-core/service/ConnectionListener_win.cpp

    r184d2e red502d  
    9292        d->hEvent = CreateEvent(NULL, true, false, NULL); 
    9393        oOverlap.hEvent = d->hEvent; 
     94        bool recreate = true; 
    9495         
    9596        while (1) { 
    96                 hPipe = CreateNamedPipe(  
    97                         (const wchar_t *)d->pipename.c_str(),             // pipe name  
    98                         PIPE_ACCESS_DUPLEX |      // read/write access  
    99                         FILE_FLAG_OVERLAPPED,     //Overlapped mode 
    100                         PIPE_TYPE_MESSAGE |       // message type pipe  
    101                         PIPE_READMODE_MESSAGE |   // message-read mode  
    102                         PIPE_WAIT,                // blocking mode  
    103                         PIPE_UNLIMITED_INSTANCES, // max. instances   
    104                         BUFSIZE,                  // output buffer size  
    105                         BUFSIZE,                  // input buffer size  
    106                         0,                        // client time-out  
    107                         &d->sa);                    // default security attribute  
     97                if (recreate) { 
     98                        hPipe = CreateNamedPipe( 
     99                                (const wchar_t *)d->pipename.c_str(),             // pipe name 
     100                                PIPE_ACCESS_DUPLEX |      // read/write access 
     101                                FILE_FLAG_OVERLAPPED,     //Overlapped mode 
     102                                PIPE_TYPE_MESSAGE |       // message type pipe 
     103                                PIPE_READMODE_MESSAGE |   // message-read mode 
     104                                PIPE_WAIT,                // blocking mode 
     105                                PIPE_UNLIMITED_INSTANCES, // max. instances 
     106                                BUFSIZE,                  // output buffer size 
     107                                BUFSIZE,                  // input buffer size 
     108                                0,                        // client time-out 
     109                                &d->sa);                    // default security attribute 
    108110 
    109                 if (hPipe == INVALID_HANDLE_VALUE) { 
    110                         //TelldusCore::logMessage("Could not create named pipe");  
    111                         return; 
     111                        if (hPipe == INVALID_HANDLE_VALUE) { 
     112                                return; 
     113                        } 
     114 
     115                        ConnectNamedPipe(hPipe, &oOverlap); 
     116                        recreate = false; 
    112117                } 
    113  
    114                 ConnectNamedPipe(hPipe, &oOverlap); 
    115  
    116118                DWORD result = WaitForSingleObject(oOverlap.hEvent, 1000); 
    117119 
    118120                if (!d->running) { 
     121                        CancelIo(hPipe); 
     122                        WaitForSingleObject(oOverlap.hEvent, INFINITE); 
    119123                        break; 
    120124                } 
    121125                if(result == WAIT_TIMEOUT){ 
    122                         CloseHandle(hPipe); 
     126                        //CloseHandle(hPipe); 
    123127                        continue; 
    124128                } 
     
    130134                } 
    131135                ConnectionListenerEventData *data = new ConnectionListenerEventData(); 
     136                ResetEvent(oOverlap.hEvent); 
    132137                data->socket = new TelldusCore::Socket(hPipe); 
    133138                d->waitEvent->signal(data); 
     139 
     140                recreate = true; 
    134141        } 
    135142 
Note: See TracChangeset for help on using the changeset viewer.