1+ /*
2+ * BSD 3-Clause License
3+ *
4+ * Copyright (c) 2018, Dolby Laboratories
5+ * All rights reserved.
6+ *
7+ * Redistribution and use in source and binary forms, with or without
8+ * modification, are permitted provided that the following conditions are met:
9+ *
10+ * * Redistributions of source code must retain the above copyright notice, this
11+ * list of conditions and the following disclaimer.
12+ *
13+ * * Redistributions in binary form must reproduce the above copyright notice,
14+ * this list of conditions and the following disclaimer in the documentation
15+ * and/or other materials provided with the distribution.
16+ *
17+ * * Neither the name of the copyright holder nor the names of its
18+ * contributors may be used to endorse or promote products derived from
19+ * this software without specific prior written permission.
20+ *
21+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+ */
32+
133#include < thread>
234#include < chrono>
335#include < map>
1345#include < signal.h>
1446#include < sys/stat.h>
1547#include < fcntl.h>
16- #include < unistd.h>
48+ #include < unistd.h>
1749#include < string.h>
1850#include < errno.h>
1951#endif
@@ -86,15 +118,15 @@ class CircularFifo
86118 }
87119 int PopFront (char * buffer, size_t size)
88120 {
89- if (PeekFront (buffer, size) != 0 )
121+ if (PeekFront (buffer, size) != 0 )
90122 return -1 ;
91123 mStart = (mStart + size) % mSize ;
92124 mFree += size;
93125 return 0 ;
94126 }
95127 int PopFront (size_t size)
96128 {
97- if (size > Taken ())
129+ if (size > Taken ())
98130 return -1 ;
99131 mStart = (mStart + size) % mSize ;
100132 mFree += size;
@@ -156,7 +188,7 @@ int get_flag(pipe_type_t type)
156188 else if (type == OUTPUT_PIPE)
157189 {
158190 return O_RDONLY;
159- }
191+ }
160192 else
161193 {
162194 return O_RDWR;
@@ -186,9 +218,9 @@ int pipe_write_func(PipeData* data)
186218 int bytes_written = write (data->mHandle , data->mTempBuf .data (), data_size);
187219 if (bytes_written < 0 )
188220 {
189- if (errno != EAGAIN)
190- status = PIPE_MGR_WRITE_ERROR;
191- bytes_written = 0 ;
221+ if (errno != EAGAIN)
222+ status = PIPE_MGR_WRITE_ERROR;
223+ bytes_written = 0 ;
192224 }
193225#endif
194226
@@ -212,7 +244,7 @@ static
212244int pipe_read_func (PipeData* data)
213245{
214246 piping_status_t status = PIPE_MGR_OK;
215- size_t outBufSize = data->mOutBuf .Free ();
247+ size_t outBufSize = data->mOutBuf .Free ();
216248
217249 if (outBufSize == 0 ) return 0 ;
218250
@@ -232,9 +264,9 @@ int pipe_read_func(PipeData* data)
232264 int bytes_read = read (data->mHandle , data->mTempBuf .data (), outBufSize);
233265 if (bytes_read < 0 )
234266 {
235- if (errno != EAGAIN)
236- status = PIPE_MGR_READ_ERROR;
237- bytes_read = 0 ;
267+ if (errno != EAGAIN)
268+ status = PIPE_MGR_READ_ERROR;
269+ bytes_read = 0 ;
238270 }
239271#endif
240272
@@ -259,28 +291,28 @@ void pipe_thread_func(PipeData* data)
259291 data->mThreadRunning = true ;
260292
261293 // connect the pipe
262- #ifdef WIN32
294+ #ifdef WIN32
263295 if (ConnectNamedPipe (data->mHandle , NULL ) == NULL )
264296 {
265297 DWORD last_error = GetLastError ();
266298 data->mErrorString = std::to_string (last_error);
267299 data->mStatus = PIPE_MGR_CONNECT_ERROR;
268300 }
269301#else
270- signal (SIGPIPE, SIG_IGN); // ignore sigpipe, we handle errors in another way
271- while ((data->mHandle = open (data->mName .c_str (), get_flag (data->mType ) | O_NONBLOCK )) == -1 )
272- {
273- if (errno != ENXIO) // ENXIO means the other end of the pipe is not ready and we need to try again
274- {
275- data->mStatus = PIPE_MGR_CONNECT_ERROR;
276- break ;
277- }
278- if (data->mStop == true )
279- {
280- data->mStatus = PIPE_MGR_CONNECT_ERROR;
281- break ;
282- }
283- }
302+ signal (SIGPIPE, SIG_IGN); // ignore sigpipe, we handle errors in another way
303+ while ((data->mHandle = open (data->mName .c_str (), get_flag (data->mType ) | O_NONBLOCK )) == -1 )
304+ {
305+ if (errno != ENXIO) // ENXIO means the other end of the pipe is not ready and we need to try again
306+ {
307+ data->mStatus = PIPE_MGR_CONNECT_ERROR;
308+ break ;
309+ }
310+ if (data->mStop == true )
311+ {
312+ data->mStatus = PIPE_MGR_CONNECT_ERROR;
313+ break ;
314+ }
315+ }
284316#endif
285317
286318 if (data->mStatus != PIPE_MGR_OK)
@@ -294,37 +326,37 @@ void pipe_thread_func(PipeData* data)
294326 {
295327 if ((data->mType == INPUT_PIPE || data->mType == DUPLEX_PIPE) && data->mInBuf .Taken () > 0 )
296328 {
297- int written_bytes = pipe_write_func (data);
298- if (written_bytes < 0 )
329+ int written_bytes = pipe_write_func (data);
330+ if (written_bytes < 0 )
299331 {
300332 break ;
301333 }
302- else if (written_bytes > 0 )
334+ else if (written_bytes > 0 )
303335 data->mKillTimer = 0 ;
304336 }
305337 if ((data->mType == OUTPUT_PIPE || data->mType == DUPLEX_PIPE) && data->mOutBuf .Free () > 0 )
306338 {
307- int read_bytes = pipe_read_func (data);
339+ int read_bytes = pipe_read_func (data);
308340 if (read_bytes < 0 )
309341 {
310342 break ;
311343 }
312- else if (read_bytes > 0 )
344+ else if (read_bytes > 0 )
313345 data->mKillTimer = 0 ;
314346 }
315-
347+
316348 if (data->mCloseIfEmpty && data->mInBuf .Taken () == 0 && data->mOutBuf .Taken () == 0 )
317349 {
318- break ;
319- }
350+ break ;
351+ }
320352 }
321-
353+
322354#ifdef WIN32
323355 CloseHandle (data->mHandle );
324356#else
325357 close (data->mHandle );
326358#endif
327-
359+
328360 data->mStatus = PIPE_MGR_PIPE_CLOSED;
329361 data->mThreadRunning = false ;
330362}
@@ -452,11 +484,11 @@ void piping_manager_thread_func(PipingManagerData* data)
452484 for (it = data->mPipes .begin (); it != data->mPipes .end (); ++it)
453485 {
454486 PipeData* pipe = it->second ;
455- if (pipe->mStatus != PIPE_MGR_OK)
456- {
457- continue ;
458- }
459- pipe->mKillTimer = pipe->mKillTimer + timeDiff.count ();
487+ if (pipe->mStatus != PIPE_MGR_OK)
488+ {
489+ continue ;
490+ }
491+ pipe->mKillTimer = pipe->mKillTimer + timeDiff.count ();
460492 if (pipe->mKillTimer >= data->mPipeTimeout && data->mGlobalTimeout == false && pipe->mStatus != PIPE_MGR_PIPE_CLOSED)
461493 {
462494 pipe->closeThread ();
@@ -529,7 +561,7 @@ piping_status_t PipingManager::destroyNamedPipe(int pipe_id)
529561
530562piping_status_t PipingManager::closePipe (int pipe_id)
531563{
532- std::map<int , PipeData*>::iterator it = mData ->mPipes .find (pipe_id);
564+ std::map<int , PipeData*>::iterator it = mData ->mPipes .find (pipe_id);
533565 if (it == mData ->mPipes .end ())
534566 {
535567 return PIPE_MGR_PIPE_NOT_FOUND;
@@ -567,22 +599,22 @@ piping_status_t PipingManager::writeToPipe(int pipe_id, void* buffer, size_t dat
567599 {
568600 PipeData* pipe = it->second ;
569601 std::lock_guard<std::mutex> lock (pipe->mMutex );
570-
602+
571603 size_t data_to_write = data_size;
572604 if (data_to_write > pipe->mInBuf .Free ())
573605 {
574606 data_to_write = pipe->mInBuf .Free ();
575- }
576-
577- if (pipe->mStatus != PIPE_MGR_OK)
578- {
579- status = pipe->mStatus ;
580- }
581- else if (data_to_write > 0 )
582- {
583- pipe->mInBuf .Append ((char *)buffer, data_to_write);
584- bytes_written = data_to_write;
585- }
607+ }
608+
609+ if (pipe->mStatus != PIPE_MGR_OK)
610+ {
611+ status = pipe->mStatus ;
612+ }
613+ else if (data_to_write > 0 )
614+ {
615+ pipe->mInBuf .Append ((char *)buffer, data_to_write);
616+ bytes_written = data_to_write;
617+ }
586618 }
587619
588620 return status;
0 commit comments