This repository has been archived by the owner on Jul 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 286
/
Copy pathAnchorImporter.cpp
179 lines (154 loc) · 6.31 KB
/
AnchorImporter.cpp
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
#include "pch.h"
#include "AnchorImporter.h"
AnchorImporter::AnchorImporter()
{
}
AnchorImporter::~AnchorImporter()
{
}
bool AnchorImporter::ConnectToServer(Platform::String^ serverIP, int port)
{
if (serverIP == nullptr)
{
return false;
}
anchorOwnerIP = serverIP;
anchorPort = port;
anchorOwnerHost = ref new HostName(anchorOwnerIP);
anchorConnection = ref new StreamSocket();
create_task(
anchorConnection->ConnectAsync(anchorOwnerHost, anchorPort.ToString())
).then([this](task<void> previousTask)
{
try
{
// Try getting all exceptions from the continuation chain above this point.
previousTask.get();
DataReader^ networkDataReader = ref new DataReader(anchorConnection->InputStream);
DataReaderLoadOperation^ drlo = networkDataReader->LoadAsync(4);
while (drlo->Status == Windows::Foundation::AsyncStatus::Started)
{
// just waiting.
}
int dataSize = networkDataReader->ReadInt32();
OutputDebugString(L"Importing anchor data.\n");
OutputDebugString(L"Client connected, start reading anchor with ");
OutputDebugString(std::to_wstring(dataSize).c_str());
OutputDebugString(L" bytes.\n");
anchorData = new byte[dataSize];
create_task(
networkDataReader->LoadAsync((unsigned int)dataSize)
).then([=](unsigned int size)
{
if (size == 0)
{
OutputDebugString(L"ERROR: No anchor bytes were receibed.\n");
return false;
}
OutputDebugString(L"Anchor data imported.\n");
networkDataReader->ReadBytes(Platform::ArrayReference<byte>(anchorData, size));
ImportAnchorDataAsync(anchorData, size);
//TODO: start another connection.
return true;
});
return true;
}
catch (Platform::Exception^ exception)
{
if ((exception->HResult == WSAETIMEDOUT) || //0x8007274c) || // connection timed out
(exception->HResult == WSAEADDRINUSE)) //0x80072740)) // connection maxed at server end
{
// If the connection timed out, try again.
ConnectToServer(anchorOwnerIP, anchorPort);
}
else if (exception->HResult == WSAEADDRNOTAVAIL) //0x80072741)
{
// No connection is possible, wait for server to start.
return false;
}
//HandleException(exception);
return true;
}
});
return true;
}
task<bool> AnchorImporter::ImportAnchorDataAsync(byte* anchorData, int anchorSize)
{
InMemoryRandomAccessStream^ stream = ref new InMemoryRandomAccessStream();
IOutputStream^ outputStream = stream->GetOutputStreamAt(0);
DataWriter^ writer = ref new DataWriter(outputStream);
// Request access to transfer spatial anchors.
return create_task(SpatialAnchorTransferManager::RequestAccessAsync()).then(
[anchorData, anchorSize, writer](SpatialPerceptionAccessStatus status)
{
if (status == SpatialPerceptionAccessStatus::Allowed)
{
// Access is allowed.
// Write the bytes to the stream.
OutputDebugString(L"Writing bytes.\n");
writer->WriteBytes(Platform::ArrayReference<byte>(anchorData, anchorSize));
// Store the stream.
return create_task(writer->StoreAsync());
}
else
{
// Access is denied.
OutputDebugString(L"ERROR: Spatial Anchor Transfer Manager access denied.\n");
return task_from_result<size_t>(0);
}
}).then([writer, stream](unsigned int bytesWritten)
{
if (bytesWritten > 0)
{
OutputDebugString(L"Flushing data.\n");
// Try to import anchors from the byte stream.
return create_task(writer->FlushAsync())
.then([stream](bool dataWasFlushed)
{
if (dataWasFlushed)
{
// Get the input stream for the anchor data.
IInputStream^ inputStream = stream->GetInputStreamAt(0);
return create_task(SpatialAnchorTransferManager::TryImportAnchorsAsync(inputStream));
}
else
{
OutputDebugString(L"ERROR: Data was not flushed.\n");
return task_from_result<Windows::Foundation::Collections::IMapView<Platform::String^, SpatialAnchor^>^>(nullptr);
}
});
}
else
{
OutputDebugString(L"ERROR: Zero bytes written.\n");
return task_from_result<Windows::Foundation::Collections::IMapView<Platform::String^, SpatialAnchor^>^>(nullptr);
}
}).then([this](task<Windows::Foundation::Collections::IMapView<Platform::String^, SpatialAnchor^>^> previousTask)
{
try
{
OutputDebugString(L"Importing data.\n");
Windows::Foundation::Collections::IMapView<Platform::String^, SpatialAnchor^>^ importedAnchorsMap = previousTask.get();
if (importedAnchorsMap == nullptr || importedAnchorsMap->Size == 0)
{
OutputDebugString(L"ERROR: no imported anchors.\n");
return false;
}
std::for_each(Windows::Foundation::Collections::begin(importedAnchorsMap),
Windows::Foundation::Collections::end(importedAnchorsMap), [this](Windows::Foundation::Collections::IKeyValuePair<Platform::String^, SpatialAnchor^>^ entry)
{
// Get the first imported anchor.
OutputDebugString(L"Caching imported anchor.\n");
sharedAnchor = entry->Value;
return true;
});
}
catch (Platform::Exception^ exception)
{
OutputDebugString(L"Error: Unable to import the anchor data buffer bytes into the in-memory anchor collection.\n");
}
return false;
});
}