summaryrefslogtreecommitdiff
path: root/nfp/net/driver/FileContext.h
blob: 0247636e721ded7767145087df5cfa905d30dccc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*++

Copyright (C) Microsoft Corporation, All Rights Reserved

Module Name:

    filecontext.h

Abstract:

    This header file defines the structure type for context associated with the file object 

Environment:

    user mode only

Revision History:

--*/
#pragma once

class CMyPayload
{
public:
    CMyPayload()
    {
        m_pbPayload = NULL;
        m_cbPayload = 0;
        
        InitializeListHead(&m_ListEntry);
    }
    ~CMyPayload()
    {
        if (m_pbPayload != NULL)
        {
            delete [] m_pbPayload;            
            m_pbPayload = NULL;
        }
    }

    STDMETHOD(Initialize)(
        _In_                   DWORD cbPayload, 
        _In_reads_bytes_(cbPayload) PBYTE pbPayload
        )
    {
        HRESULT hr = S_OK;
        m_pbPayload = new BYTE[cbPayload];
        if (m_pbPayload != NULL)
        {
            m_cbPayload = cbPayload;
            CopyMemory(m_pbPayload, pbPayload, cbPayload);
        }
        else
        {
            hr = E_OUTOFMEMORY;
        }
        
        return hr;
    }

    PBYTE GetPayload()
    {
        return m_pbPayload;
    }
    DWORD GetSize()
    {
        return m_cbPayload;
    }
    
    PLIST_ENTRY GetListEntry()
    {
        return &m_ListEntry;
    }
    static CMyPayload* FromListEntry(PLIST_ENTRY pEntry)
    {
        return (CMyPayload*) CONTAINING_RECORD(pEntry, CMyPayload, m_ListEntry);
    }

private:
    PBYTE m_pbPayload;
    DWORD m_cbPayload;
    
    LIST_ENTRY m_ListEntry;
};

/*
 * Use this to refactor to only keep one copy of received messages
 *
class CMyPayloadItem
{
public:
    CMyPayloadItem(_In_ CMyPayload* pPayload)
    {
        m_spPayload = pPayload;
        InitializeListHead(&m_ListEntry);
    }
    ~CMyPayloadItem()
    {
    }

    PBYTE GetPayload()
    {
        return m_spPayload->GetPayload();
    }
    DWORD GetSize()
    {
        return m_spPayload->GetSize();
    }

    PLIST_ENTRY GetListEntry()
    {
        return &m_ListEntry;
    }
    static CMyPayloadItem* FromListEntry(PLIST_ENTRY pEntry)
    {
        return (CMyPayloadItem*) CONTAINING_RECORD(pEntry, CMyPayloadItem, m_ListEntry);
    }

private:
    CComPtr<CMyPayload> m_spPayload;
    
    LIST_ENTRY m_ListEntry;
};
*/

class CFileContext
{
public:

    CFileContext() :
        m_Role(ROLE_UNDEFINED),
        m_pszType(NULL),
        m_fEnabled(TRUE),
        m_dwQueueSize(0),
        m_cCompleteReady(0),
        m_pConnection(NULL),
        m_pWdfRequest(NULL)
    {
        InitializeListHead(&m_SubscribedMessageQueue);

        InitializeListHead(&m_ListEntry);
        
        InitializeCriticalSection(&m_RoleLock);
    }

    ~CFileContext();

    HRESULT
    Disable();
    
    HRESULT
    Enable();
    
    HRESULT
    SetType(_In_ PCWSTR pszType);

    HRESULT
    GetNextSubscribedMessage(_In_ IRequestCallbackCancel* pCallbackCancel, _In_ IWDFIoRequest* pWdfRequest);
    
    HRESULT
    SetPayload(_In_ IWDFIoRequest* pWdfRequest);

    HRESULT
    GetNextTransmittedMessage(_In_ IRequestCallbackCancel* pCallbackCancel, _In_ IWDFIoRequest* pWdfRequest);
    
    HRESULT
    BeginProximity(
        _In_ IWDFIoRequest*       pWdfRequest,
        _In_ IConnectionCallback* pCallback
        );

    VOID
    HandleArrivalEvent();
    VOID
    HandleRemovalEvent();

    void
    HandleReceivedPublication(
        _In_                   PCWSTR pszType,
        _In_                   DWORD  cbPayload, 
        _In_reads_bytes_(cbPayload) PBYTE  pbPayload
        );
    
    void
    HandleMessageTransmitted();

    void
    OnCancel();

    void
    SetRoleSubcription()
    {
        m_Role = ROLE_SUBSCRIPTION;
    }

    void
    SetRolePublication()
    {
        m_Role = ROLE_PUBLICATION;
    }

    BOOL
    SetRoleArrivedSubcription()
    {
        if (m_Role == ROLE_SUBSCRIPTION)
        {
            m_Role = ROLE_ARRIVEDSUBSCRIPTION;
            return TRUE;
        }
        return FALSE;
    }

    BOOL
    SetRoleDepartedSubcription()
    {
        if (m_Role == ROLE_SUBSCRIPTION)
        {
            m_Role = ROLE_DEPARTEDSUBSCRIPTION;
            return TRUE;
        }
        return FALSE;
    }

    BOOL
    IsNormalSubscription()
    {
        return (m_Role == ROLE_SUBSCRIPTION);
    }
    BOOL
    IsArrivedSubscription()
    {
        return (m_Role == ROLE_ARRIVEDSUBSCRIPTION);
    }
    BOOL
    IsDepartedSubscription()
    {
        return (m_Role == ROLE_DEPARTEDSUBSCRIPTION);
    }
    BOOL
    IsSubscription()
    {
        return (m_Role == ROLE_SUBSCRIPTION);
    }
    BOOL
    IsPublication()
    {
        return (m_Role == ROLE_PUBLICATION);
    }
        
    PCWSTR
    GetType()
    {
        return m_pszType;
    }

    DWORD
    GetSize()
    {
        return m_MyPayload.GetSize();
    }

    PBYTE
    GetPayload()
    {
        return m_MyPayload.GetPayload();
    }

    BOOL
    IsEnabled()
    {
        return m_fEnabled;
    }
    
    PLIST_ENTRY GetListEntry()
    {
        return &m_ListEntry;
    }
    static CFileContext* FromListEntry(PLIST_ENTRY pEntry)
    {
        return (CFileContext*) CONTAINING_RECORD(pEntry, CFileContext, m_ListEntry);
    }
    
private:
    
    VOID
    CompleteOneArrivalEvent();
    VOID
    CompleteOneRemovalEvent();
    
    bool
    CompleteOneGetNextSubscribedMessage(
        _In_                       DWORD cbPayload,
        _In_reads_bytes_opt_(cbPayload) PBYTE pbPayload
        );
    
    bool
    CompleteRequest(
        _In_ HRESULT hr,
        _In_ SIZE_T  cbSize,
        _In_ bool    fIsCancelable
        );

private:

    enum ROLE
    {
        ROLE_UNDEFINED,
        ROLE_SUBSCRIPTION,
        ROLE_ARRIVEDSUBSCRIPTION,
        ROLE_DEPARTEDSUBSCRIPTION,
        ROLE_PUBLICATION,
        ROLE_PROXIMITY
    };

    ROLE m_Role;

    PWSTR m_pszType;

    BOOL m_fEnabled;

    DWORD m_dwQueueSize;
    
    // Queue of received messages
    LIST_ENTRY m_SubscribedMessageQueue; // Unique to ROLE_SUBSCRIPTION

    CMyPayload m_MyPayload;  // Unique to ROLE_PUBLICATION
    SIZE_T m_cCompleteReady; // Unique to ROLE_PUBLICATION, ROLE_ARRIVEDSUBSCRIPTION, and ROLE_DEPARTEDSUBSCRIPTION

    CConnection* m_pConnection; // Unique to ROLE_PROXIMITY
    
    // Pended "Get Next" Request.
    IWDFIoRequest* m_pWdfRequest;

    // The Fx File object this CFileObject is a companion to
    IWDFFile* m_pWdfFile;

    CRITICAL_SECTION m_RoleLock;
    
    LIST_ENTRY m_ListEntry;
};