Skip to content

Commit 9d198da

Browse files
authored
Merge pull request #16 from leapmotion/file-endpoint
File endpoint
2 parents 76fc29c + 5a4924f commit 9d198da

File tree

6 files changed

+128
-1
lines changed

6 files changed

+128
-1
lines changed

src/leapipc/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ set(IPC_SRCS
55
IPCClientConnector.cpp
66
IPCEndpoint.h
77
IPCEndpoint.cpp
8+
IPCFileEndpoint.h
9+
IPCFileEndpoint.cpp
810
IPCListener.h
911
IPCListener.cpp
1012
MessageBuffers.h

src/leapipc/IPCFileEndpoint.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
}

src/leapipc/IPCFileEndpoint.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (C) 2012-2016 Leap Motion, Inc. All rights reserved.
2+
#pragma once
3+
#include "IPCEndpoint.h"
4+
#include <fstream>
5+
6+
namespace leap {
7+
namespace ipc {
8+
9+
class IPCFileEndpoint:
10+
public IPCEndpoint
11+
{
12+
public:
13+
IPCFileEndpoint(const std::string & fileName, bool read, bool write);
14+
~IPCFileEndpoint(void);
15+
16+
private:
17+
std::fstream m_file;
18+
19+
/// <summary>
20+
/// Simulate EOF behavior when we are done.
21+
/// </summary>
22+
int Done(Reason reason);
23+
24+
public:
25+
// IPCEndpoint overrides:
26+
std::streamsize ReadRaw(void* buffer, std::streamsize size) override;
27+
bool WriteRaw(const void* pBuf, std::streamsize nBytes) override;
28+
bool Abort(Reason reason) override;
29+
};
30+
31+
}}

src/leapipc/testing/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ set(LeapIPCTest_SRCS
55
IPCListenerTest.cpp
66
IPCMessagingTest.cpp
77
IPCShutdownTest.cpp
8+
IPCFileEndpointTest.cpp
89
IPCTestUtils.h
910
IPCTestUtils.cpp
1011
)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (C) 2012-2016 Leap Motion, Inc. All rights reserved.
2+
#include "stdafx.h"
3+
#include "IPCTestUtils.h"
4+
#include <leapipc/IPCFileEndpoint.h>
5+
#include <autowiring/autowiring.h>
6+
#include <autowiring/CoreThread.h>
7+
#include <cstdio>
8+
#include <gtest/gtest.h>
9+
10+
using namespace leap::ipc;
11+
12+
class IPCFileEndpointTest :
13+
public testing::Test
14+
{};
15+
16+
TEST_F(IPCFileEndpointTest, RawWriteToReadFromFile)
17+
{
18+
std::string testMessage = "0123456789abcdef";
19+
const char* testFile = "IPCFileEndpoint_TestFile";
20+
21+
// Write test message to file
22+
{
23+
std::shared_ptr<IPCFileEndpoint> ep = std::make_shared<IPCFileEndpoint>(testFile, false, true);
24+
ep->WriteRaw((void*)testMessage.c_str(), testMessage.size());
25+
}
26+
27+
// Read test message from file
28+
{
29+
std::shared_ptr<IPCFileEndpoint> ep = std::make_shared<IPCFileEndpoint>(testFile, true, false);
30+
31+
size_t bufsize = testMessage.size()+1;
32+
char* buf = new char[bufsize];
33+
buf[bufsize - 1] = 0;
34+
35+
ep->ReadRaw((void*)buf, bufsize-1);
36+
37+
ASSERT_EQ(strcmp(buf, testMessage.c_str()), 0);
38+
39+
delete[] buf;
40+
}
41+
42+
remove(testFile);
43+
}

version.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
set(LeapIPC_VERSION 0.1.2)
1+
set(LeapIPC_VERSION 0.1.3)

0 commit comments

Comments
 (0)