Sample Code

Windows Driver Samples/ Near-Field Proximity Sample Driver (UMDF Version 1)/ C++/ driver/ socketlistener.h/

/*++

Copyright (c) Microsoft Corporation.  All rights reserved.

Abstract:

    Declares a socket listener class

Author:

    Travis Martin (TravM) 06-24-2010

Environment:

    User-mode only.
    
--*/
#pragma once

struct ACCEPT_BUFFER
{
    GUID MagicPacket;
    
    SOCKADDR_STORAGE DestAddress;
    SOCKADDR_STORAGE SourceAddress;
};

interface IValidateAccept
{
    virtual void ValidateAccept(_In_ SOCKET Socket, _In_ GUID* pMagicPacket) = 0;
};

class CSocketListener
{
public:
    CSocketListener() :
        _pValidator(NULL),
        _ThreadpoolIo(NULL),
        _ListenSocket(INVALID_SOCKET),
        _ClientSocket(INVALID_SOCKET)
    {
        ZeroMemory(&_Overlapped, sizeof(_Overlapped));
    }

    ~CSocketListener()
    {
        StopAccepting();
    }

public:
    HRESULT Bind();
    HRESULT EnableAccepting(_In_ IValidateAccept* pValidator);
    void StopAccepting();

private:

    HRESULT BeginAccept();
    void AcceptThreadProc(_In_ HRESULT hr, _In_ ULONG_PTR cbReceived);
    static void CALLBACK s_AcceptThreadProc(
        _Inout_      PTP_CALLBACK_INSTANCE /*Instance*/,
        _Inout_      PVOID                 Context,
        _Inout_opt_  PVOID                 /*Overlapped*/,
        _In_         ULONG                 IoResult,
        _In_         ULONG_PTR             NumberOfBytesTransferred,
        _Inout_      PTP_IO                /*Io*/)
    {
        CSocketListener* pSocketListener = (CSocketListener*)Context;
        pSocketListener->_ThreadpoolThreadId = GetCurrentThreadId();
        pSocketListener->AcceptThreadProc(HRESULT_FROM_WIN32(IoResult), NumberOfBytesTransferred);
        pSocketListener->_ThreadpoolThreadId = 0;
    }

private:
    ACCEPT_BUFFER _AcceptBuffer;
    
    OVERLAPPED        _Overlapped;
    volatile PTP_IO   _ThreadpoolIo;
    DWORD             _ThreadpoolThreadId;
    
    IValidateAccept* _pValidator;
    
    SOCKET _ListenSocket;
    SOCKET _ClientSocket; 
};

Our Services

  • What our customers say about us?

© 2011-2024 All Rights Reserved. Joya Systems. 4425 South Mopac Building II Suite 101 Austin, TX 78735 Tel: 800-DEV-KERNEL

Privacy Policy. Terms of use. Valid XHTML & CSS