summaryrefslogtreecommitdiffstats
path: root/fastos/src/vespa/fastos/unix_ipc.cpp
blob: f8cde75bc7a7578614f21772715e50c0ae7e4879 (plain) (blame)
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "unix_ipc.h"
#include "ringbuffer.h"
#include <cassert>
#include <cstring>
#include <unistd.h>
#include <fcntl.h>

FastOS_UNIX_IPCHelper::
FastOS_UNIX_IPCHelper (FastOS_ApplicationInterface *app, int descriptor)
    : _lock(),
      _exitFlag(false),
      _app(app),
      _appParentIPCDescriptor()
{
    _appParentIPCDescriptor._fd = descriptor;
    _wakeupPipe[0] = -1;
    _wakeupPipe[1] = -1;

    if(pipe(_wakeupPipe) != 0) {
        perror("pipe wakeuppipe");
        exit(1);
    } else {
        SetBlocking(_wakeupPipe[0], false);
        SetBlocking(_wakeupPipe[1], true);
    }

    if(_appParentIPCDescriptor._fd != -1) {
        _appParentIPCDescriptor._readBuffer.reset(new FastOS_RingBuffer(16384));
        _appParentIPCDescriptor._writeBuffer.reset(new FastOS_RingBuffer(16384));

        SetBlocking(_appParentIPCDescriptor._fd, false);
    }
}

FastOS_UNIX_IPCHelper::~FastOS_UNIX_IPCHelper ()
{
    if(_wakeupPipe[0] != -1) {
        close(_wakeupPipe[0]);
    }
    if(_wakeupPipe[1] != -1) {
        close(_wakeupPipe[1]);
    }
    if(_appParentIPCDescriptor._fd != -1) {
        close(_appParentIPCDescriptor._fd);
    }
    _appParentIPCDescriptor._readBuffer.reset();
    _appParentIPCDescriptor._writeBuffer.reset();
}


bool FastOS_UNIX_IPCHelper::
DoWrite(FastOS_UNIX_Process::DescriptorHandle &desc)
{
    bool rc = true;
    FastOS_RingBuffer *buffer = desc._writeBuffer.get();

    buffer->Lock();
    int writeBytes = buffer->GetReadSpace();
    if(writeBytes > 0)
    {
        int bytesWritten;
        do
        {
            bytesWritten = write(desc._fd,
                                 buffer->GetReadPtr(),
                                 writeBytes);
        } while(bytesWritten < 0 && errno == EINTR);

        if(bytesWritten > 0)
            buffer->Consume(bytesWritten);
        else if(bytesWritten < 0)
        {
            desc.CloseHandle();
            perror("FastOS_UNIX_IPCHelper::DoWrite");
            rc = false;
        }
        else if(bytesWritten == 0)
            desc.CloseHandle();
    }
    buffer->Unlock();

    return rc;
}

bool FastOS_UNIX_IPCHelper::
DoRead (FastOS_UNIX_Process::DescriptorHandle &desc)
{
    bool rc = true;

    FastOS_RingBuffer *buffer = desc._readBuffer.get();

    buffer->Lock();
    int readBytes = buffer->GetWriteSpace();
    if(readBytes > 0) {
        int bytesRead;
        do {
            bytesRead = read(desc._fd, buffer->GetWritePtr(), readBytes);
        } while(bytesRead < 0 && errno == EINTR);

        if (bytesRead > 0) {
            buffer->Produce(bytesRead);
        } else if(bytesRead < 0) {
            desc.CloseHandle();
            perror("FastOS_UNIX_IPCHelper::DoRead");
            rc = false;
        } else if(bytesRead == 0) {
            desc.CloseHandle();
        }
    }
    buffer->Unlock();

    return rc;
}

bool FastOS_UNIX_IPCHelper::
SetBlocking (int fileDescriptor, bool doBlock)
{
    bool rc=false;

    int flags = fcntl(fileDescriptor, F_GETFL, NULL);
    if (flags != -1)
    {
        if(doBlock)
            flags &= ~O_NONBLOCK;
        else
            flags |= O_NONBLOCK;
        rc = (fcntl(fileDescriptor, F_SETFL, flags) != -1);
    }
    return rc;
}

void FastOS_UNIX_IPCHelper::
BuildPollCheck(bool isRead, int filedes,
               FastOS_RingBuffer *buffer, bool *check)
{
    if(buffer == NULL ||
       filedes < 0 ||
       buffer->GetCloseFlag()) {
        *check = false;
        return;
    }

    bool setIt = false;
    if(isRead)
        setIt = (buffer->GetWriteSpace() > 0);
    else
        setIt = (buffer->GetReadSpace() > 0);
    *check = setIt;
}


void FastOS_UNIX_IPCHelper::
PerformAsyncIO(void)
{
    FastOS_ProcessInterface *node;
    for(node = _app->GetProcessList(); node != NULL; node = node->_next)
    {
        FastOS_UNIX_Process *xproc = static_cast<FastOS_UNIX_Process *>(node);

        for(int type=0; type < int(FastOS_UNIX_Process::TYPE_READCOUNT); type++)
        {
            FastOS_UNIX_Process::DescriptorType type_ =
                FastOS_UNIX_Process::DescriptorType(type);
            FastOS_UNIX_Process::DescriptorHandle &desc =
                xproc->GetDescriptorHandle(type_);
            if (desc._canRead)
                (void) DoRead(desc);
            if (desc._canWrite)
                (void) DoWrite(desc);
        }
    }
}

void FastOS_UNIX_IPCHelper::
PerformAsyncIPCIO(void)
{
    FastOS_UNIX_Process::DescriptorHandle &desc = _appParentIPCDescriptor;
    if (desc._canRead)
        (void) DoRead(desc);
    if (desc._canWrite)
        (void) DoWrite(desc);
}


void FastOS_UNIX_IPCHelper::
BuildPollChecks(void)
{
    FastOS_ProcessInterface *node;
    for(node = _app->GetProcessList(); node != NULL; node = node->_next)
    {
        FastOS_UNIX_Process *xproc = static_cast<FastOS_UNIX_Process *>(node);

        for(int type=0; type < int(FastOS_UNIX_Process::TYPE_READCOUNT); type++)
        {
            FastOS_UNIX_Process::DescriptorType type_ =
                FastOS_UNIX_Process::DescriptorType(type);
            FastOS_UNIX_Process::DescriptorHandle &desc =
                xproc->GetDescriptorHandle(type_);
            BuildPollCheck(false, desc._fd, desc._writeBuffer.get(), &desc._wantWrite);
            BuildPollCheck(true,  desc._fd, desc._readBuffer.get(), &desc._wantRead);
        }
    }

    if(_appParentIPCDescriptor._writeBuffer.get() != NULL)
        BuildPollCheck(false, _appParentIPCDescriptor._fd,
                       _appParentIPCDescriptor._writeBuffer.get(),
                       &_appParentIPCDescriptor._wantWrite);
    if(_appParentIPCDescriptor._readBuffer.get() != NULL)
        BuildPollCheck(true, _appParentIPCDescriptor._fd,
                       _appParentIPCDescriptor._readBuffer.get(),
                       &_appParentIPCDescriptor._wantRead);
}


static pollfd *
__attribute__((__noinline__))
ResizePollArray(pollfd **fds, unsigned int *allocnfds)
{
    pollfd *newfds;
    unsigned int newallocnfds;

    if (*allocnfds == 0)
        newallocnfds = 16;
    else
        newallocnfds = *allocnfds * 2;
    newfds = static_cast<pollfd *>(malloc(newallocnfds * sizeof(pollfd)));
    assert(newfds != NULL);

    if (*allocnfds > 0)
        memcpy(newfds, *fds, sizeof(pollfd) * *allocnfds);

    if (*fds != NULL)
        free(*fds);

    *fds = newfds;
    newfds += *allocnfds;
    *allocnfds = newallocnfds;
    return newfds;
}

void
FastOS_UNIX_IPCHelper::
BuildPollArray(pollfd **fds, unsigned int *nfds, unsigned int *allocnfds)
{
    FastOS_ProcessInterface *node;
    pollfd *rfds;
    const pollfd *rfdsEnd;
    int pollIdx;

    rfds = *fds;
    rfdsEnd = *fds + *allocnfds;

    if (rfds >= rfdsEnd) {
        rfds = ResizePollArray(fds,
                               allocnfds);
        rfdsEnd = *fds + *allocnfds;
    }
    rfds->fd = _wakeupPipe[0];
    rfds->events = POLLIN;
    rfds->revents = 0;
    rfds++;
    pollIdx = 1;
    for(node = _app->GetProcessList(); node != NULL; node = node->_next)
    {
        FastOS_UNIX_Process *xproc = static_cast<FastOS_UNIX_Process *>(node);

        for(int type=0; type < int(FastOS_UNIX_Process::TYPE_READCOUNT); type++)
        {
            FastOS_UNIX_Process::DescriptorType type_ =
                FastOS_UNIX_Process::DescriptorType(type);
            FastOS_UNIX_Process::DescriptorHandle &desc =
                xproc->GetDescriptorHandle(type_);

            if (desc._fd >= 0 &&
                (desc._wantRead || desc._wantWrite)) {
                if (rfds >= rfdsEnd) {
                    rfds = ResizePollArray(fds,
                            allocnfds);
                    rfdsEnd = *fds + *allocnfds;
                }
                rfds->fd = desc._fd;
                rfds->events = 0;
                if (desc._wantRead)
                    rfds->events |= POLLRDNORM;
                if (desc._wantWrite)
                    rfds->events |= POLLWRNORM;
                rfds->revents = 0;
                desc._pollIdx = pollIdx;
                rfds++;
                pollIdx++;
            } else {
                desc._pollIdx = -1;
                desc._canRead = false;
                desc._canWrite = false;
            }
        }
    }

    FastOS_UNIX_Process::DescriptorHandle &desc2 = _appParentIPCDescriptor;

    if (desc2._fd >= 0 &&
        (desc2._wantRead || desc2._wantWrite)) {
        if (rfds >= rfdsEnd) {
            rfds = ResizePollArray(fds,
                                   allocnfds);
            rfdsEnd = *fds + *allocnfds;
        }
        rfds->fd = desc2._fd;
        rfds->events = 0;
        if (desc2._wantRead)
            rfds->events |= POLLRDNORM;
        if (desc2._wantWrite)
            rfds->events |= POLLWRNORM;
        rfds->revents = 0;
        desc2._pollIdx = pollIdx;
        rfds++;
        pollIdx++;
    } else {
        desc2._pollIdx = -1;
        desc2._canRead = false;
        desc2._canWrite = false;
    }
    *nfds = rfds - *fds;
}


bool
FastOS_UNIX_IPCHelper::
SavePollArray(pollfd *fds, unsigned int nfds)
{
    FastOS_ProcessInterface *node;

    for(node = _app->GetProcessList(); node != NULL; node = node->_next)
    {
        FastOS_UNIX_Process *xproc = static_cast<FastOS_UNIX_Process *>(node);

        for(int type=0; type < int(FastOS_UNIX_Process::TYPE_READCOUNT); type++)
        {
            FastOS_UNIX_Process::DescriptorType type_ =
                FastOS_UNIX_Process::DescriptorType(type);
            FastOS_UNIX_Process::DescriptorHandle &desc =
                xproc->GetDescriptorHandle(type_);

            if (desc._fd >= 0 &&
                static_cast<unsigned int>(desc._pollIdx) < nfds) {
                int revents = fds[desc._pollIdx].revents;

                if (desc._wantRead &&
                    (revents &
                     (POLLIN | POLLRDNORM | POLLERR | POLLHUP | POLLNVAL)) != 0)
                    desc._canRead = true;
                else
                    desc._canRead = false;
                if (desc._wantWrite &&
                    (revents &
                     (POLLOUT | POLLWRNORM | POLLWRBAND | POLLERR | POLLHUP |
                      POLLNVAL)) != 0)
                    desc._canWrite = true;
                else
                    desc._canWrite = false;
            }
        }
    }

    FastOS_UNIX_Process::DescriptorHandle &desc2 = _appParentIPCDescriptor;

    if (desc2._fd >= 0 &&
        static_cast<unsigned int>(desc2._pollIdx) < nfds) {
        int revents = fds[desc2._pollIdx].revents;

        if ((revents &
             (POLLIN | POLLRDNORM | POLLERR | POLLHUP | POLLNVAL)) != 0)
            desc2._canRead = true;
        else
            desc2._canRead = false;
        if ((revents &
             (POLLOUT | POLLWRNORM | POLLWRBAND | POLLERR | POLLHUP |
              POLLNVAL)) != 0)
            desc2._canWrite = true;
        else
            desc2._canWrite = false;
    }

    if ((fds[0].revents & (POLLIN | POLLERR | POLLHUP)) != 0)
        return true;
    else
        return false;
}


void FastOS_UNIX_IPCHelper::
RemoveClosingProcesses(void)
{
    // We assume that not updating maxFD isn't harmless.

    FastOS_ProcessInterface *node, *next;

    for(node = _app->GetProcessList(); node != NULL; node = next)
    {
        int type;

        next = node->_next;
        FastOS_UNIX_Process *xproc = static_cast<FastOS_UNIX_Process *>(node);

        bool stillBusy = false;
        if(!xproc->GetKillFlag())
            for(type=0; type < FastOS_UNIX_Process::TYPE_READCOUNT; type++)
            {
                FastOS_UNIX_Process::DescriptorType type_;

                type_ = static_cast<FastOS_UNIX_Process::DescriptorType>(type);

                FastOS_UNIX_Process::DescriptorHandle &desc =
                    xproc->GetDescriptorHandle(type_);

                if (desc._fd != -1)
                {
                    if((type_ == FastOS_UNIX_Process::TYPE_STDOUT) ||
                       (type_ == FastOS_UNIX_Process::TYPE_STDERR) ||
                       desc._wantWrite)
                    {
                        // We still want to use this socket.
                        // Make sure we don't close the socket yet.
                        stillBusy = true;
                        break;
                    }
                }
            }

        if(!stillBusy)
        {
            if(xproc->_closing != NULL)
            {
                // We already have the process lock at this point,
                // so modifying the list is safe.
                _app->RemoveChildProcess(node);

                for(type=0; type < FastOS_UNIX_Process::TYPE_READCOUNT; type++)
                {
                    FastOS_UNIX_Process::DescriptorHandle &desc =
                        xproc->GetDescriptorHandle(FastOS_UNIX_Process::DescriptorType(type));
                    if(desc._fd != -1)
                    {
                        // No more select on this one.
                        // We already know wantWrite is not set
                        if (desc._wantRead)
                            desc._wantRead = false;
                    }
                }

                // The process destructor can now proceed
                xproc->_closing->ClearBusy();
            }
        }
    }
}


void FastOS_UNIX_IPCHelper::
Run(FastOS_ThreadInterface *thisThread, void *arg)
{
    (void)arg;
    (void)thisThread;

    FastOS_ProcessInterface *node;
    pollfd *fds;
    unsigned int nfds;
    unsigned int allocnfds;

    fds = NULL;
    nfds = 0;
    allocnfds = 0;
    for(;;)
    {
        // Deliver messages to from child processes and parent.
        _app->ProcessLock();
        for(node = _app->GetProcessList(); node != NULL; node = node->_next)
        {
            FastOS_UNIX_Process *xproc = static_cast<FastOS_UNIX_Process *>(node);
            FastOS_UNIX_Process::DescriptorHandle &desc =
                xproc->GetDescriptorHandle(FastOS_UNIX_Process::TYPE_IPC);
            DeliverMessages(desc._readBuffer.get());
            PipeData(xproc, FastOS_UNIX_Process::TYPE_STDOUT);
            PipeData(xproc, FastOS_UNIX_Process::TYPE_STDERR);
        }
        DeliverMessages(_appParentIPCDescriptor._readBuffer.get());

        // Setup file descriptor sets for the next select() call
        BuildPollChecks();

        // Close and signal closing processes
        RemoveClosingProcesses();

        BuildPollArray(&fds, &nfds, &allocnfds);

        _app->ProcessUnlock();

        _lock.Lock();
        bool exitFlag(_exitFlag);
        _lock.Unlock();
        if (exitFlag)
        {
            if (_appParentIPCDescriptor._fd != -1)
            {
                if(_appParentIPCDescriptor._wantWrite)
                {
                    //               printf("still data to write\n");
                }
                else
                {
                    //               printf("no more data to write, exitting\n");
                    break;
                }
            }
            else
                break;
        }

        for (;;)
        {
            int pollRc =
                poll(fds, nfds, -1);

            if(pollRc == -1)
            {
                int wasErrno = errno;

                if(wasErrno == EINTR)
                {
                    continue;
                }

                perror("FastOS_UNIX_IPCHelper::RunAsync select failure");
                printf("errno = %d\n", wasErrno);
                for(unsigned int i = 0; i < nfds; i++)
                {
                    if ((fds[i].events & POLLIN) != 0)
                        printf("Read %d\n", fds[i].fd);
                    if ((fds[i].events & POLLOUT) != 0)
                        printf("Write %d\n", fds[i].fd);
                }
                exit(1);
            }
            else
                break;
        }

        _app->ProcessLock();
        bool woken = SavePollArray(fds, nfds);
        // Do actual IO (based on file descriptor sets and buffer contents)
        PerformAsyncIO();
        _app->ProcessUnlock();
        PerformAsyncIPCIO();

        // Did someone want to wake us up from the poll() call?
        if (woken) {
            char dummy;
	    ssize_t nbrfp = read(_wakeupPipe[0], &dummy, 1);
	    if (nbrfp != 1) {
	        perror("FastOS_UNIX_IPCHelper wakeupPipe read failed");
	    }
        }
    }
    free(fds);

    delete this;
    //      printf("IPCHelper exits\n");
}


bool FastOS_UNIX_IPCHelper::
SendMessage (FastOS_UNIX_Process *xproc, const void *buffer,
             int length)
{
    bool rc = false;

    FastOS_RingBuffer *ipcBuffer;

    FastOS_UNIX_Process::DescriptorHandle &desc =
        xproc != NULL ?
        xproc->GetDescriptorHandle(FastOS_UNIX_Process::TYPE_IPC) :
        _appParentIPCDescriptor;
    ipcBuffer = desc._writeBuffer.get();

    if(ipcBuffer != NULL) {
        ipcBuffer->Lock();

        if(ipcBuffer->GetWriteSpace() >= int((length + sizeof(int)))) {
            memcpy(ipcBuffer->GetWritePtr(), &length, sizeof(int));
            ipcBuffer->Produce(sizeof(int));
            memcpy(ipcBuffer->GetWritePtr(), buffer, length);
            ipcBuffer->Produce(length);

            NotifyProcessListChange();
            rc = true;
        }
        ipcBuffer->Unlock();
    }
    return rc;
}

void FastOS_UNIX_IPCHelper::NotifyProcessListChange ()
{
    char dummy = 'x';
    ssize_t nbwtp = write(_wakeupPipe[1], &dummy, 1);
    if (nbwtp != 1) {
	perror("FastOS_UNIX_IPCHelper: write to wakeupPipe failed");
    }
}

void FastOS_UNIX_IPCHelper::Exit ()
{
    _lock.Lock();
    _exitFlag = true;
    NotifyProcessListChange();
    _lock.Unlock();
}

void FastOS_UNIX_IPCHelper::AddProcess (FastOS_UNIX_Process *xproc)
{
    bool newStream = false;
    for(int type=0; type < int(FastOS_UNIX_Process::TYPE_READCOUNT); type++)
    {
        FastOS_UNIX_Process::DescriptorType type_ =
            FastOS_UNIX_Process::DescriptorType(type);
        FastOS_UNIX_Process::DescriptorHandle &desc =
            xproc->GetDescriptorHandle(type_);

        if (desc._fd != -1)
        {
            newStream = true;
            SetBlocking(desc._fd, false);
        }
    }
    if(newStream)
        NotifyProcessListChange();
}

void FastOS_UNIX_IPCHelper::RemoveProcess (FastOS_UNIX_Process *xproc)
{
    (void)xproc;

    FastOS_BoolCond closeWait;

    closeWait.SetBusy();
    xproc->_closing = &closeWait;

    NotifyProcessListChange();

    closeWait.WaitBusy();
}

void FastOS_UNIX_IPCHelper::DeliverMessages (FastOS_RingBuffer *buffer)
{
    if(buffer == NULL)
        return;

    buffer->Lock();

    unsigned int readSpace;
    while((readSpace = buffer->GetReadSpace()) > sizeof(int))
    {
        FastOS_RingBufferData *bufferData = buffer->GetData();

        if((readSpace - sizeof(int)) >= bufferData->_messageSize)
        {
            _app->OnReceivedIPCMessage(&bufferData->_buffer[sizeof(int)],
                                       bufferData->_messageSize);
            buffer->Consume(sizeof(int) + bufferData->_messageSize);
            buffer->RepositionDataAt0();
        }
        else
            break;
    }

    buffer->Unlock();
}

void FastOS_UNIX_IPCHelper::
PipeData (FastOS_UNIX_Process *process,
          FastOS_UNIX_Process::DescriptorType type)
{
    FastOS_UNIX_Process::DescriptorHandle &desc = process->GetDescriptorHandle(type);
    FastOS_RingBuffer *buffer = desc._readBuffer.get();
    if(buffer == NULL)
        return;

    FastOS_ProcessRedirectListener *listener = process->GetListener(type);
    if(listener == NULL)
        return;

    buffer->Lock();

    unsigned int readSpace;
    while((readSpace = buffer->GetReadSpace()) > 0) {
        listener->OnReceiveData(buffer->GetReadPtr(), size_t(readSpace));
        buffer->Consume(readSpace);
    }

    if(buffer->GetCloseFlag())
        process->CloseListener(type);

    buffer->Unlock();
}