diff options
Diffstat (limited to '')
| -rw-r--r-- | src/fifo.cpp | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/src/fifo.cpp b/src/fifo.cpp new file mode 100644 index 0000000..2321cb6 --- /dev/null +++ b/src/fifo.cpp | |||
| @@ -0,0 +1,146 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (C) 2007-2008 Xagasoft, All rights reserved. | ||
| 3 | * | ||
| 4 | * This file is part of the libbu++ library and is released under the | ||
| 5 | * terms of the license contained in the file LICENSE. | ||
| 6 | */ | ||
| 7 | |||
| 8 | #include "fifo.h" | ||
| 9 | #include "exceptions.h" | ||
| 10 | #include <errno.h> | ||
| 11 | #include <sys/types.h> | ||
| 12 | #include <sys/stat.h> | ||
| 13 | #include <fcntl.h> | ||
| 14 | |||
| 15 | Bu::Fifo::Fifo( const Bu::FString &sName, int iFlags, mode_t mAcc ) : | ||
| 16 | iFlags( iFlags ), | ||
| 17 | iIn( -1 ), | ||
| 18 | iOut( -1 ) | ||
| 19 | { | ||
| 20 | if( iFlags&Create ) | ||
| 21 | { | ||
| 22 | if( mkfifo( sName.getStr(), mAcc ) ) | ||
| 23 | { | ||
| 24 | throw FifoException("Error creating fifo: %s\n", strerror( errno ) ); | ||
| 25 | } | ||
| 26 | } | ||
| 27 | if( iFlags&Read ) | ||
| 28 | { | ||
| 29 | iIn = ::open( | ||
| 30 | sName.getStr(), | ||
| 31 | O_RDONLY|((iFlags&NonBlock)?O_NONBLOCK:0) | ||
| 32 | ); | ||
| 33 | } | ||
| 34 | if( iFlags&Write ) | ||
| 35 | { | ||
| 36 | iOut = ::open( | ||
| 37 | sName.getStr(), | ||
| 38 | O_WRONLY | ||
| 39 | ); | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | Bu::Fifo::~Fifo() | ||
| 44 | { | ||
| 45 | close(); | ||
| 46 | } | ||
| 47 | |||
| 48 | void Bu::Fifo::close() | ||
| 49 | { | ||
| 50 | if( iIn > -1 ) | ||
| 51 | { | ||
| 52 | ::close( iIn ); | ||
| 53 | iIn = -1; | ||
| 54 | } | ||
| 55 | if( iOut > -1 ) | ||
| 56 | { | ||
| 57 | ::close( iOut ); | ||
| 58 | iOut = -1; | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | size_t Bu::Fifo::read( void *pBuf, size_t nBytes ) | ||
| 63 | { | ||
| 64 | if( iIn < 0 ) | ||
| 65 | throw FifoException("Fifo not open for reading."); | ||
| 66 | |||
| 67 | return TEMP_FAILURE_RETRY( ::read( iIn, pBuf, nBytes ) ); | ||
| 68 | } | ||
| 69 | |||
| 70 | size_t Bu::Fifo::write( const void *pBuf, size_t nBytes ) | ||
| 71 | { | ||
| 72 | if( iOut < 0 ) | ||
| 73 | throw FifoException("Fifo not open for writing."); | ||
| 74 | |||
| 75 | return TEMP_FAILURE_RETRY( ::write( iOut, pBuf, nBytes ) ); | ||
| 76 | } | ||
| 77 | |||
| 78 | long Bu::Fifo::tell() | ||
| 79 | { | ||
| 80 | return -1; | ||
| 81 | } | ||
| 82 | |||
| 83 | void Bu::Fifo::seek( long offset ) | ||
| 84 | { | ||
| 85 | } | ||
| 86 | |||
| 87 | void Bu::Fifo::setPos( long pos ) | ||
| 88 | { | ||
| 89 | } | ||
| 90 | |||
| 91 | void Bu::Fifo::setPosEnd( long pos ) | ||
| 92 | { | ||
| 93 | } | ||
| 94 | |||
| 95 | bool Bu::Fifo::isEOS() | ||
| 96 | { | ||
| 97 | return false; | ||
| 98 | } | ||
| 99 | |||
| 100 | bool Bu::Fifo::canRead() | ||
| 101 | { | ||
| 102 | return (iIn>-1); | ||
| 103 | } | ||
| 104 | |||
| 105 | bool Bu::Fifo::canWrite() | ||
| 106 | { | ||
| 107 | return (iOut>-1); | ||
| 108 | } | ||
| 109 | |||
| 110 | bool Bu::Fifo::isReadable() | ||
| 111 | { | ||
| 112 | return (iIn>-1); | ||
| 113 | } | ||
| 114 | |||
| 115 | bool Bu::Fifo::isWritable() | ||
| 116 | { | ||
| 117 | return (iOut>-1); | ||
| 118 | } | ||
| 119 | |||
| 120 | bool Bu::Fifo::isSeekable() | ||
| 121 | { | ||
| 122 | return false; | ||
| 123 | } | ||
| 124 | |||
| 125 | bool Bu::Fifo::isBlocking() | ||
| 126 | { | ||
| 127 | return ((fcntl( iIn, F_GETFL, 0 )&O_NONBLOCK) == O_NONBLOCK); | ||
| 128 | } | ||
| 129 | |||
| 130 | void Bu::Fifo::setBlocking( bool bBlocking ) | ||
| 131 | { | ||
| 132 | if( bBlocking ) | ||
| 133 | fcntl( iIn, F_SETFL, fcntl( iIn, F_GETFL, 0 )&(~O_NONBLOCK) ); | ||
| 134 | else | ||
| 135 | fcntl( iIn, F_SETFL, fcntl( iIn, F_GETFL, 0 )|O_NONBLOCK ); | ||
| 136 | } | ||
| 137 | |||
| 138 | void Bu::Fifo::flush() | ||
| 139 | { | ||
| 140 | } | ||
| 141 | |||
| 142 | bool Bu::Fifo::isOpen() | ||
| 143 | { | ||
| 144 | return (iIn > -1) || (iOut > -1); | ||
| 145 | } | ||
| 146 | |||
