This repository has been archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
transport.h
104 lines (78 loc) · 3.13 KB
/
transport.h
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
/*!
* janus-client SDK
*
* transport.h
* Janus tranports implementation
* This module defines twot different kinds of janus transport: HTTP and WebSocket
*
* Copyright 2019 Pasquale Boemio <[email protected]>
*/
#pragma once
#define HTTP_CLIENT_POOL_SIZE 2
#include <memory>
#include <queue>
#include <nlohmann/json.hpp>
#include "janus/http.h"
#include "janus/async.h"
#include "janus/bundle.hpp"
namespace Janus {
class HttpTransport;
using HttpTask = std::function<std::shared_ptr<HttpResponse>(const std::string&, const std::shared_ptr<Http>&, const std::shared_ptr<HttpTransport>&)>;
class TransportDelegate {
public:
virtual void onMessage(const nlohmann::json& message, const std::shared_ptr<Bundle>& context) = 0;
};
enum TransportType { HTTP, WS };
enum TransportStatus { ON, OFF };
class Transport {
public:
virtual void sessionId(const std::string& id) = 0;
virtual void close() = 0;
virtual TransportType type() = 0;
virtual void send(const nlohmann::json& message, const std::shared_ptr<Bundle>& context) = 0;
};
class TransportImpl : public Transport {
public:
TransportImpl(const std::shared_ptr<TransportDelegate>& delegate, const std::shared_ptr<Async>& async);
void sessionId(const std::string& id);
void close();
protected:
TransportStatus _status = TransportStatus::OFF;
std::shared_ptr<TransportDelegate> _delegate;
std::string _sessionId = "";
std::mutex _sessionIdMutex;
std::shared_ptr<Async> _async;
};
class HttpTransport : public TransportImpl, public std::enable_shared_from_this<HttpTransport> {
public:
HttpTransport(const std::string& url, const std::shared_ptr<TransportDelegate>& delegate, const std::shared_ptr<HttpFactory>& factory, const std::shared_ptr<Async>& async);
TransportType type() {
return TransportType::HTTP;
}
void send(const nlohmann::json& message, const std::shared_ptr<Bundle>& context);
void sessionId(const std::string& id);
private:
void _sendAsync(const HttpTask& kernel, const std::shared_ptr<Bundle>& context);
static std::shared_ptr<HttpResponse> _loop(const std::string& path, const std::shared_ptr<Http>& client, const std::shared_ptr<HttpTransport>& main);
std::queue<std::shared_ptr<Http>> _clients;
std::mutex _clientsMutex;
std::condition_variable _notEmpty;
};
class WebSocketTransport : public TransportImpl {
public:
WebSocketTransport(const std::string& url, const std::shared_ptr<TransportDelegate>& delegate) : TransportImpl(delegate, nullptr) {}
TransportType type() {
return TransportType::WS;
}
void send(const nlohmann::json& message, const std::shared_ptr<Bundle>& context);
void close();
};
class TransportFactory {
public:
virtual std::shared_ptr<Transport> create(const std::string& url, const std::shared_ptr<TransportDelegate>& delegate) = 0;
};
class TransportFactoryImpl : public TransportFactory {
public:
std::shared_ptr<Transport> create(const std::string& url, const std::shared_ptr<TransportDelegate>& delegate);
};
}