1+ // Copyright (C) 2012-2016 Leap Motion, Inc. All rights reserved.
2+ #include " stdafx.h"
3+ #include " IPCFileEndpoint.h"
4+ #include < system_error>
5+
6+ using namespace leap ::ipc;
7+
8+ IPCFileEndpoint::IPCFileEndpoint (const std::string & fileName, bool read, bool write)
9+ {
10+ std::ios_base::openmode mode = std::ios::binary;
11+ if (read) mode |= std::ios::in;
12+ if (write) mode |= std::ios::out;
13+ m_file.open (fileName, mode);
14+ if (!m_file.is_open ())
15+ throw std::runtime_error (" Failed to open file" );
16+ }
17+
18+ IPCFileEndpoint::~IPCFileEndpoint (void )
19+ {
20+ if (m_file.is_open ())
21+ m_file.close ();
22+ }
23+
24+ std::streamsize IPCFileEndpoint::ReadRaw (void * buffer, std::streamsize size) {
25+ if (m_file.eof ())
26+ return Done (Reason::UserClosed);
27+
28+ m_file.read ((char *)buffer, size);
29+ return m_file.gcount ();
30+ }
31+
32+ bool IPCFileEndpoint::WriteRaw (const void * pBuf, std::streamsize nBytes) {
33+ m_file.write ((char *)pBuf, nBytes);
34+ if (m_file.bad ())
35+ return false ;
36+ return true ;
37+ }
38+
39+ bool IPCFileEndpoint::Abort (Reason reason) {
40+ if (m_file.is_open ()) {
41+ m_file.close ();
42+ return true ;
43+ }
44+ return false ;
45+ }
46+
47+ // On the first call, return 0 (EOF). Subsequent calls, return -1 (error).
48+ int IPCFileEndpoint::Done (Reason reason) {
49+ return Abort (reason) ? 0 : -1 ;
50+ }
0 commit comments