s4n_service.cpp

来自「funambol window mobile客户端源代码」· C++ 代码 · 共 260 行

CPP
260
字号
/*
 * Funambol is a mobile platform developed by Funambol, Inc. 
 * Copyright (C) 2003 - 2007 Funambol, Inc.
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License version 3 as published by
 * the Free Software Foundation with the addition of the following permission
 * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
 * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE
 * WARRANTY OF NON INFRINGEMENT  OF THIRD PARTY RIGHTS.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 
 * details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program; if not, see http://www.gnu.org/licenses or write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA.
 * 
 * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite
 * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com.
 * 
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU Affero General Public License version 3.
 * 
 * In accordance with Section 7(b) of the GNU Affero General Public License
 * version 3, these Appropriate Legal Notices must retain the display of the
 * "Powered by Funambol" logo. If the display of the logo is not reasonably
 * feasible for technical reasons, the Appropriate Legal Notices must display
 * the words "Powered by Funambol".
 */
// service.cpp : register and deregister notlstnr service.
//

#include <windows.h>
#include <service.h>
#include <winsock2.h>
#include <ws2tcpip.h>

#include "pim/utils.h"
#include "pim/SettingFunctions.h"
#include "notify/s4n_service.h"
#include "base/Log.h"
#include "pim/ClientSettings.h"


#define SERVICE_PREFIX TEXT("S4N")
#define SERVICE_DLL TEXT("notlstnr.dll")
#define SERVICE_PREFIX_INDEX TEXT("S4N0:")
#define SERVICE_INDEX 0
#define SERVICE_PORT 4745

typedef HANDLE (*PFN_REGISTER_SERVICE)(
  LPCWSTR lpszType,
  DWORD dwIndex,
  LPCWSTR lpszLib,
  DWORD dwInfo
);

typedef BOOL (*PFN_DEREGISTER_SERVICE) (
  HANDLE hDevice
);

typedef BOOL (*PFN_SERVICE_IO_CONTROL) (
  HANDLE hService,
  DWORD dwIoControlCode,
  LPVOID lpInBuf,
  DWORD nInBufSize,
  LPVOID lpOutBuf,
  DWORD nOutBufSize,
  LPDWORD lpBytesReturned,
  LPOVERLAPPED lpOverlapped
);

typedef BOOL (*PFN_SERVICE_ADD_PORT) (
  HANDLE hService,
  SOCKADDR* pSockAddr,
  INT cbSockAddr,
  INT iProtocol,
  WCHAR szRegWritePath
);

typedef HANDLE (*PFN_GET_SERVICE_HANDLE) (
  LPWSTR szPrefix,
  LPWSTR szDllName,
  DWORD pdwDllBuf
);

int getListenerPort(void)
{

//    ClientSettings cs(APPLICATION_URI);
//    ClientSettings::getInstance()->readConfig();
    string s = ClientSettings::getInstance()->getPushPort();
    if (s == "")
        return SERVICE_PORT;

    return atoi(s.c_str());

    /*
    wchar_t buf[32];

    memset(buf, 0, 32*sizeof(wchar_t));
    getClientConfigurationInternal (TEXT(""), PROPERTY_SPDM_NOTIFICATION_PORT,
                                 buf, FALSE);

    if(buf[0] == 0)
        return SERVICE_PORT;

    return wcstol(buf, NULL, 10);
    */
}

int registerService() {
    PFN_REGISTER_SERVICE pfnRegister = NULL;
    PFN_SERVICE_IO_CONTROL pfnIOControl = NULL;
    PFN_SERVICE_ADD_PORT pfnAddPort = NULL;
    SOCKADDR_IN SockAddr;
    HANDLE s = 0;
    int port = 0;
    int ret = 0;

    HMODULE h = LoadLibrary(L"COREDLL.dll");
    if (h == NULL) {
        LOG.error("registerService: library not found");
        goto finally;
    }
    pfnRegister =
        (PFN_REGISTER_SERVICE)GetProcAddress(h, L"RegisterService");

    if (pfnRegister == NULL) {
        LOG.error("registerService: function RegisterService not found");
        goto finally;
    }

    pfnIOControl =
        (PFN_SERVICE_IO_CONTROL)GetProcAddress(h, L"ServiceIoControl");

    if (pfnIOControl == NULL) {
        LOG.error("registerService: function ServiceIoControl not found");
        goto finally;
    }

    pfnAddPort =
        (PFN_SERVICE_ADD_PORT)GetProcAddress(h, L"ServiceAddPort");

    if (pfnAddPort == NULL) {
        LOG.error("registerService: function ServiceAddPort not found");
        goto finally;
    }

    port = getListenerPort();

    s = (pfnRegister)(SERVICE_PREFIX, SERVICE_INDEX, SERVICE_DLL, port);

    if (s == NULL) {
        ret = GetLastError();
        if (ret == 120) {
            LOG.debug("Service already registered");
        }
        else {
            LOG.debug("registerService: registering error (%d)", ret);
        }
        goto finally;
    }

    //
    // Now we register the super service...
    //
    memset(&SockAddr,0,sizeof(SockAddr));

    if(!(pfnIOControl)(s, IOCTL_SERVICE_REGISTER_SOCKADDR,0,0,0,0,0,0)) {
        LOG.error("registerService: the service does not support socked address registering");
        goto finally; //does not support super services
    }

    SockAddr.sin_family = AF_INET;
    SockAddr.sin_port=htons(port);
    if (! (pfnAddPort)(s,(SOCKADDR*)&SockAddr, sizeof(SockAddr), 0,0)) {
        LOG.error("registerService: I cannot listen on port %d", port);
        goto finally;
    }

    //When all sockets have been bound, the service must be informed that it
    //is ready to start.
    if(!(pfnIOControl)(s, IOCTL_SERVICE_STARTED,0,0,0,0,0,0)) {
        LOG.error("registerService: the service could not start....");
        goto finally; //if the service cannot start
    }

    LOG.debug("Service sucessfully registerd");

finally:
    if (h) {
        FreeLibrary(h);
    }
    return ret;
}

int deregisterService() {
    HMODULE h = NULL;
    HANDLE  s = NULL; // service handle
    int ret = 0;

    PFN_GET_SERVICE_HANDLE pfnGetHandle = NULL;
    PFN_DEREGISTER_SERVICE pfnDeregister = NULL;

    h = LoadLibrary(L"COREDLL.dll");
    if (h == NULL) {
        LOG.error("deregisterService: library not found");
        goto finally;
    }
    pfnDeregister =
        (PFN_DEREGISTER_SERVICE)GetProcAddress(h, L"DeregisterService");

    if (pfnDeregister == NULL) {
        LOG.error("deregisterService: function DeregisterService not found");
        goto finally;
    }

    pfnGetHandle =
        (PFN_GET_SERVICE_HANDLE)GetProcAddress(h, L"GetServiceHandle");

    if (pfnGetHandle == NULL) {
        LOG.error("deregisterService: function GetServiceHandle not found");
        goto finally;
    }

    s = (pfnGetHandle)(SERVICE_PREFIX_INDEX, NULL, 0);

    if (s == INVALID_HANDLE_VALUE) {
        LOG.error("deregisterService: Service not found");
        ret = GetLastError();
        goto finally;
    }

    if ((pfnDeregister)(s) == 0) {
        ret = GetLastError();
        LOG.error("Deregistering error: %ld", ret);
        goto finally;
    }

    s = NULL;

    LOG.debug("Service sucessfully deregisterd");

finally:
    if (h) {
        FreeLibrary(h);
    }
    return ret;
}

void activateService() {

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?