MCPcopy Create free account
hub / github.com/PhoneVR-Developers/PhoneVR / TCPTalker

Method TCPTalker

code/common/src/PVRSocketUtils.cpp:13–107  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

11// prefix -> 6 bytes
12
13TCPTalker::TCPTalker(uint16_t port,
14 function<void(PVR_MSG, vector<uint8_t>)> recCb,
15 function<void(std::error_code)> errCb,
16 bool isServer,
17 string ip) {
18 bool initted = false;
19 PVR_DB_I("[TCPTalker::TCPTalker] Init'ed with p:" + to_string(port) +
20 " isServer:" + to_string(isServer) + " ip:" + to_string(ip))
21
22 thr = new std::thread([=, &initted] {
23 try {
24 io_service svc;
25 tcp::socket skt(svc);
26 _skt = &skt;
27 tcp::acceptor acc(svc, {tcp::v4(), port});
28
29 asio::error_code ec = asio::error::fault;
30 auto errHdl = [&](const asio::error_code &err) { ec = err; };
31 if (isServer) { // talker is a server; Mobile device will be Announcing UDP + Will be
32 // Server
33 if (ip.empty()) {
34 acc.async_accept(skt, errHdl); // todo: simplify
35 PVR_DB_I("[TCPTalker::TCPTalker] Talker is Server and ip is Empty. Accepting "
36 "All connection...");
37 } else {
38 acc.async_accept(skt, errHdl); // todo: simplify
39 // skt.async_connect({ address::from_string(ip), port }, errHdl);
40 PVR_DB_I("[TCPTalker::TCPTalker] Talker is Server and Accepting connections "
41 "from ip:" +
42 ip + " & port:" + to_string(port) + "...");
43 }
44 initted = true; // early unblock server
45 } else // talker is a client; Desktop will be Client will receive the Announcement +
46 // Will try to connect.
47 {
48 skt.async_connect({address::from_string(ip), port}, errHdl);
49 PVR_DB_I("[TCPTalker::TCPTalker] Talker is Client and Connecting to ip:" + ip +
50 " & port:" + to_string(port) + "...");
51 }
52 svc.run();
53 svc.reset();
54 if (!isServer)
55 initted = true; // on server mode this goes out of scope
56
57 if (ec.value() == 0) {
58 IP = skt.remote_endpoint().address().to_string();
59
60 const size_t bufsz = 256;
61 string prefix = "pvr";
62 uint8_t buf[bufsz];
63 vector<uint8_t> stream;
64 size_t msgLen = 0;
65
66 function<void(const asio::error_code &, size_t)> handle = [&](const asio::error_code
67 & /*err*/,
68 size_t len) {
69 // PVR_DB_I("[TCPTalker::TCPTalker] Revd some data... Interpreting...");
70 stream.insert(stream.end(), buf, buf + len);

Callers

nothing calls this directly

Calls 6

to_stringFunction · 0.85
PVR_MSGEnum · 0.85
emptyMethod · 0.80
runMethod · 0.80
resetMethod · 0.80
sizeMethod · 0.45

Tested by

no test coverage detected