-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
63 lines (50 loc) · 2.01 KB
/
main.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
#include "stdio.h"
#include <thread>
#include <emscripten/emscripten.h>
#include "string_container.h"
worker_handle worker = emscripten_create_worker("test_worker.js");
// Second callback which receives wrong data.
void workerCallbackInner(char* data, int size, void* args)
{
StringContainer stringContainer;
stringContainer.deserialize(data, size);
delete [] data;
printf("String deserialized in second worker callback: %s \n", stringContainer.getString().c_str());
}
// First callback, which receives correct data.
void workerCallback(char* data, int size, void* args)
{
StringContainer stringContainer;
stringContainer.deserialize(data, size);
delete [] data;
printf("String deserialized in first worker callback: %s \n", stringContainer.getString().c_str());
StringContainer newStringContainer;
newStringContainer.loadData();
printf("String passed to second worker call: %s \n", newStringContainer.getString().c_str());
emscripten_call_worker(
worker,
"test",
newStringContainer.serialize(),
newStringContainer.getSerializedSize(),
workerCallbackInner,
(void *) 0
);
}
// This program creates simple string container, which can be serialized and deserialized.
// Then it sends serialized container to the webworker.
// The webworker deserializes it, performs some actions on it and then sends serialized container back.
// When performing these operations for the first time, everything works fine.
// But during further calls webworker sends back some wrong data, which can't be properly deserialized.
int main(int argc, char** argv) {
StringContainer stringContainer;
stringContainer.loadData();
printf("String passed to first worker call: %s \n", stringContainer.getString().c_str());
emscripten_call_worker(
worker,
"test",
stringContainer.serialize(),
stringContainer.getSerializedSize(),
workerCallback,
(void *) 0
);
}