File: link_tester.cpp

package info (click to toggle)
mumble 1.5.735-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 90,008 kB
  • sloc: cpp: 556,921; ansic: 81,662; python: 3,606; sh: 659; makefile: 506; asm: 371; cs: 306; sql: 228; javascript: 143; perl: 80; xml: 13
file content (115 lines) | stat: -rw-r--r-- 2,984 bytes parent folder | download | duplicates (2)
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
// Copyright 2021-2023 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.

#include "LinkedMem.h"
#include "SharedMemory.h"

#ifndef _WIN32
#	include <unistd.h>
#endif

#include <chrono>
#include <csignal>
#include <cstring>
#include <cwchar>
#include <iostream>
#include <random>
#include <thread>

SharedMemory sharedMem;
LinkedMem lm;
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_real_distribution< float > generator(0, 100);

void initMumble() {
	sharedMem.mapMemory(getLinkedMemoryName());

	lm = LinkedMem();
}

void updateMumble() {
	if (!sharedMem.isMemoryMapped())
		return;

	if (lm.uiVersion != 2) {
		wcsncpy(lm.name, L"TestLink", 256);
		wcsncpy(lm.description, L"TestLink is a test of the Link plugin.", 2048);
		lm.uiVersion = 2;
	}
	lm.uiTick++;

	// Left handed coordinate system.
	// X positive towards "right".
	// Y positive towards "up".
	// Z positive towards "front".
	//
	// 1 unit = 1 meter

	// Unit vector pointing out of the avatar's eyes aka "At"-vector.
	lm.fAvatarFront[0] = 0.0f;
	lm.fAvatarFront[1] = 0.0f;
	lm.fAvatarFront[2] = 1.0f;

	// Unit vector pointing out of the top of the avatar's head aka "Up"-vector (here Top points straight up).
	lm.fAvatarTop[0] = 0.0f;
	lm.fAvatarTop[1] = 1.0f;
	lm.fAvatarTop[2] = 0.0f;

	// Position of the avatar (here standing slightly off the origin)
	lm.fAvatarPosition[0] = generator(rng);
	lm.fAvatarPosition[1] = generator(rng);
	lm.fAvatarPosition[2] = generator(rng);

	// Same as avatar but for the camera.
	lm.fCameraPosition[0] = 0.0f;
	lm.fCameraPosition[1] = 0.0f;
	lm.fCameraPosition[2] = 0.0f;

	lm.fCameraFront[0] = 0.0f;
	lm.fCameraFront[1] = 0.0f;
	lm.fCameraFront[2] = 1.0f;

	lm.fCameraTop[0] = 0.0f;
	lm.fCameraTop[1] = 1.0f;
	lm.fCameraTop[2] = 0.0f;

	// Identifier which uniquely identifies a certain player in a context (e.g. the ingame name).
	wcsncpy(lm.identity, L"Unique ID", 256);
	// Context should be equal for players which should be able to hear each other positional and
	// differ for those who shouldn't (e.g. it could contain the server+port and team)
	memcpy(lm.context, "ContextBlob\x00\x01\x02\x03\x04", 16);
	lm.context_len = 16;

	sharedMem.write(lm);
}

void signalHandler(int signum) {
	std::cout << "Interrupt signal (" << signum << ") received - shutting down..." << std::endl;

	sharedMem.close();

	std::exit(signum);
}

int main() {
	signal(SIGINT, signalHandler);

	initMumble();

	if (!sharedMem.isMemoryMapped()) {
		std::cerr << "Failed to create shared memory region (" << sharedMem.lastError() << ")" << std::endl;
		return 1;
	}

	std::cout << "Shared memory created successfully - Now starting update loop" << std::endl;

	while (true) {
		std::cout << "Tick" << std::endl;
		updateMumble();

		std::this_thread::sleep_for(std::chrono::milliseconds(500));
	}
}