⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mynsp.cpp

📁 这个是网络编程
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    return NO_ERROR;
}

//
// Function: NSPSetService
//
// Description:
//    This function maps to the Winsock call WSASetService.
//    This routine is called when the user wants to register 
//    or deregister an instance of a server with our service. 
//    For registration, the user needs to associate the server 
//    with a service class. For deregistration the service class
//    is required along with the servicename. The lpqsRegInfo 
//    parameter contains a WSAQUERYSET structure defining the
//    server (such as protocol and address where it is). This 
//    routine connects to the server and writes the service
//    class and service information. The namespace service
//    returns a status code.
//
int WSPAPI NSPSetService (  
    LPGUID lpProviderId,                        
    LPWSASERVICECLASSINFOW lpServiceClassInfo,   
    LPWSAQUERYSETW lpqsRegInfo,                  
    WSAESETSERVICEOP essOperation,               
    DWORD dwControlFlags)
{
    SOCKET      s;
    BYTE        action;
    int         ret,    
                nLeft,
                retcode,
                bytesread,
                byteswritten;
    char        databuf[MARSHALL_BUFFER_SZ];

    // Set the appropriate action and connect to our namespace
    // server.
    //
    if (essOperation == RNRSERVICE_REGISTER)
        action = MYNSP_REGISTER_SERVICE;
    else if (essOperation == RNRSERVICE_DELETE)
        action = MYNSP_DEREGISTER_SERVICE;
    s = MyNspConnect();
    if (s == SOCKET_ERROR)
        return SOCKET_ERROR;

    writedata(s, (char *)&action, sizeof(action), &byteswritten);
    //
    // Marshall the WSAQUERYSET into a contiguous buffer
    //
    ret = MarshallServiceInfo(lpqsRegInfo, databuf, &nLeft);

    writedata(s, (char *)&nLeft, sizeof(nLeft), &byteswritten);
    
    writedata(s, databuf, nLeft, &byteswritten);
    //
    // Read the status code from the service
    //
    bytesread = sizeof(retcode);
    readdata(s, (char *)&retcode, sizeof(retcode), &bytesread);

    if (retcode == MYNSP_ERROR)
    {
        closesocket(s);
        WSASetLastError(WSAEINVAL);
        return SOCKET_ERROR;
    }
    closesocket(s);
    
    return NO_ERROR;
}

//
// Function: NSPInstallServiceClass
//
// Description:
//    This function maps to the Winsock call WSAInstallServiceClass.
//    This routine is used to install a service class which is
//    used to define certain characteristics for a group of
//    services. After a service class is registered, an actual
//    instance of a server may be registered. This call connects
//    to the namespace service and sends the WSASERVICECLASSINFO
//    structure. The service will return a status code.
//
int WSPAPI NSPInstallServiceClass( 
    LPGUID lpProviderId,
    LPWSASERVICECLASSINFOW lpServiceClassInfo)
{
    SOCKET      s;
    char        databuf[MARSHALL_BUFFER_SZ];
    int         ret, 
                retcode, 
                nLeft, 
                bytesread,
                byteswritten;
    BYTE        action;

#ifdef DEBUG
    printf("Registering Service Class Name: %S\n", 
        lpServiceClassInfo->lpszServiceClassName);
#endif
    // Set the action and connect to the namespace service
    //
    action = MYNSP_REGISTER_CLASS;
    s = MyNspConnect();
    if (s == SOCKET_ERROR)
        return SOCKET_ERROR;

    writedata(s, (char *)&action, sizeof(action), &byteswritten);
    //
    // Marshall the WSASERVICECLASSINFO into a contiguous buffer and
    // send to the service
    //
    ret = MarshallServiceClassInfo(lpServiceClassInfo, databuf, &nLeft);

    writedata(s, (char *)&nLeft, sizeof(nLeft), &byteswritten);

    writedata(s, databuf, nLeft, &byteswritten);
    //
    // Read the status code
    //
    bytesread = sizeof(retcode);
    readdata(s, (char *)&retcode, sizeof(retcode), &bytesread);

    if (retcode == MYNSP_ERROR)        // Service class already exists
    {
        closesocket(s);
        WSASetLastError(WSAEALREADY);
        return SOCKET_ERROR;
    }
    closesocket(s);

    return NO_ERROR;
}

//
// Function: NSPRemoveServiceClass
//
// Description:
//    This function maps to the Winsock call WSARemoveServiceClass.
//    This routine removes a previously registered service class.
//    This is accomplished by connecting to the namespace service
//    and writing the GUID which defines the given service class.
//    The namespace service removes this entry and returns a
//    status code.
//
int WSPAPI NSPRemoveServiceClass(  
    LPGUID lpProviderId,
    LPGUID lpServiceClassId)
{
    SOCKET      s;
    int         byteswritten,
                bytesread,
                retcode;
    BYTE        action;
    
    // Set the action and connect to the namespace service
    // 
    action = MYNSP_DEREGISTER_CLASS;
    s = MyNspConnect();
    if (s == SOCKET_ERROR)
        return SOCKET_ERROR;

    writedata(s, (char *)&action, sizeof(action), &byteswritten);

    writedata(s, (char *)lpServiceClassId, sizeof(GUID), &byteswritten);
    //
    // Read the return code
    //
    bytesread = sizeof(retcode);
    readdata(s, (char *)&retcode, sizeof(retcode), &bytesread);
    
    if (retcode == MYNSP_ERROR)        // service class not found
    {
        closesocket(s);
        WSASetLastError(WSATYPE_NOT_FOUND);
        return SOCKET_ERROR;
    }
    closesocket(s);    
    
    return NO_ERROR;
}

//
// Function: NSPGetServiceClassInfo
//
// Description:
//    This function maps to the Winsock call WSAGetServiceClassInfo.
//    This routine returns the information associated with a given
//    service class. This is done by connecting to the namespace
//    service and sending the guid defined for that service class.
//    The service will return the associated WSASERVICECLASSINFO
//    structure.
//
int WSPAPI NSPGetServiceClassInfo(    
    LPGUID lpProviderId,                           
    LPDWORD lpdwBufSize,
    LPWSASERVICECLASSINFOW lpServiceClassInfo)
{
    SOCKET      s;
    int         byteswritten,
                bytesread,
                bytes2follow;
    BYTE        action;
    char        databuf[MARSHALL_BUFFER_SZ];

    // Set the action and connect to the namespace service
    //
    action = MYNSP_LOOKUP_CLASS;
    s = MyNspConnect();
    if (s == SOCKET_ERROR)
        return SOCKET_ERROR; 
    
    writedata(s, (char *)&action, sizeof(action), &byteswritten);

    writedata(s, (char *)lpProviderId, sizeof(GUID), &byteswritten);
    //
    // Read the number of byets to follow. If this is -1 it means
    //  an error occured (i.e. the service class was not found).
    //
    bytesread = sizeof(bytes2follow);
    readdata(s, (char *)&bytes2follow, sizeof(bytes2follow), &bytesread);

    if (bytes2follow == -1)        // Service class not found
    {
        WSASetLastError(WSATYPE_NOT_FOUND);
        return SOCKET_ERROR;
    }
    // Make sure the supplied buffer is large enough
    //
    if ((DWORD)bytes2follow > *lpdwBufSize)
    {
        *lpdwBufSize = bytes2follow;
        WSASetLastError(WSAEFAULT);
        return SOCKET_ERROR;
    }
    readdata(s, databuf, MARSHALL_BUFFER_SZ, &bytes2follow);
    
    DeMarshallServiceClassInfo(lpServiceClassInfo, databuf);

    closesocket(s);
    return NO_ERROR;
}

//
// Function: NSPCleanup
//
// Description:
//    This function is called when our namespace DLL is unloaded.
//    Since we didn't perform any custom initialization in NSPStartup
//    we don't need to clean anything up here.
//    
int WSPAPI NSPCleanup (LPGUID lpProviderId)
{
    return NO_ERROR;
}

//
// Function: NSPStartup
//
// Description:
//    When our namespace DLL is loaded, this function is automatically
//    called. It sets up an NSP_ROUTINE structure which maps our 
//    RNR functions back to the Winsock calls. Also if we had any
//    startup or intialization specific to our name space provider
//    we could put it here.
//
int WSPAPI NSPStartup (  
    LPGUID lpProviderId,
    LPNSP_ROUTINE lpnspRoutines)
{
    // This is required since we're not implementing NSPIoctl.
    //    If you're using an older SDK that doesn't define NSPIoctl
    //    then uncomment out the second line.
    lpnspRoutines->cbSize = FIELD_OFFSET(NSP_ROUTINE, NSPIoctl);
    //lpnspRoutines->cbSize = sizeof(NSP_ROUTINE);

    lpnspRoutines->dwMajorVersion = 4;
    lpnspRoutines->dwMinorVersion = 4;
    lpnspRoutines->NSPCleanup             = NSPCleanup;
    lpnspRoutines->NSPLookupServiceBegin  = NSPLookupServiceBegin;
    lpnspRoutines->NSPLookupServiceNext   = NSPLookupServiceNext;
    lpnspRoutines->NSPLookupServiceEnd    = NSPLookupServiceEnd;
    lpnspRoutines->NSPSetService          = NSPSetService;
    lpnspRoutines->NSPInstallServiceClass = NSPInstallServiceClass;
    lpnspRoutines->NSPRemoveServiceClass  = NSPRemoveServiceClass;
    lpnspRoutines->NSPGetServiceClassInfo = NSPGetServiceClassInfo;

    return NO_ERROR;
}

⌨️ 快捷键说明

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