File: main.cc

package info (click to toggle)
civetweb 1.16%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,184 kB
  • sloc: ansic: 32,473; cpp: 1,374; sh: 480; javascript: 204; makefile: 120; php: 11; perl: 6; python: 3
file content (150 lines) | stat: -rw-r--r-- 3,338 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
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
#include <iostream>
#include <memory>
#include <mutex>
#include <sstream>
#include <unistd.h>
#include <unordered_map>

#include "CivetServer.h"

class WebSocketBase : public CivetWebSocketHandler
{

  public:
	explicit WebSocketBase(std::string name) : name_(std::move(name))
	{
	}

	bool
	handleConnection(CivetServer *server, const mg_connection *conn) override
	{
		return true;
	}

	void
	handleReadyState(CivetServer *server, mg_connection *conn) override
	{
		std::unique_lock<std::mutex> lock(mutex_);
		pool_.emplace(conn, std::make_shared<std::mutex>());
	}

	bool
	handleData(CivetServer *server,
	           mg_connection *conn,
	           int bits,
	           char *data,
	           size_t data_len) override
	{

		if ((bits & 0x0F) == MG_WEBSOCKET_OPCODE_CONNECTION_CLOSE) {
			return false;
		}
		data_.write(data, data_len);
		if (current_opcode_ == 0x00) {
			current_opcode_ = bits & 0x7f;
		}
		bool is_final_fragment = bits & 0x80;
		if (is_final_fragment) {
			switch (current_opcode_) {
			case MG_WEBSOCKET_OPCODE_TEXT:
				std::cout << data_.str() << std::endl; // print request data
				break;
			default:
				break;
			}
			current_opcode_ = 0x00;
			this->sendData(conn, data_.str()); // response
			data_.clear();
			data_.str(std::string());
		}
		return true;
	}

	void
	handleClose(CivetServer *server, const mg_connection *conn) override
	{
		auto *connection = const_cast<mg_connection *>(conn);
		std::shared_ptr<std::mutex> user_lock;
		{
			std::unique_lock<std::mutex> lock(mutex_);
			user_lock = pool_[connection];
		}
		{
			std::unique_lock<std::mutex> lock_connection(*user_lock);
			std::unique_lock<std::mutex> lock(mutex_);
			pool_.erase(connection);
		}
	}

	bool
	sendData(mg_connection *conn,
	         const std::string &data,
	         bool skippable = false,
	         int op_code = MG_WEBSOCKET_OPCODE_TEXT)
	{
		std::shared_ptr<std::mutex> connection_lock;
		{
			std::unique_lock<std::mutex> lock(mutex_);
			connection_lock = pool_[conn];
		}

		if (!connection_lock->try_lock()) {
			if (skippable) {
				return false;
			}
			connection_lock->lock();
			std::unique_lock<std::mutex> lock(mutex_);
		}

		int ret = mg_websocket_write(conn, op_code, data.c_str(), data.size());
		connection_lock->unlock();

		if (ret != static_cast<int>(data.size())) {
			if (data.empty() && ret == 2) {
				return true;
			}
			return false;
		}

		return true;
	}

	thread_local static std::stringstream data_;
	thread_local static unsigned char current_opcode_;

  private:
	const std::string name_;
	mutable std::mutex mutex_;
	std::unordered_map<mg_connection *, std::shared_ptr<std::mutex>> pool_;
};

thread_local unsigned char WebSocketBase::current_opcode_ = 0x00;
thread_local std::stringstream WebSocketBase::data_;

int
main()
{
	std::cout << "Hello, Civetweb Websocket port:8080!" << std::endl;

	mg_init_library(0);
	std::vector<std::string> options = {
	    "document_root",
	    ".",
	    "listening_ports",
	    "8080",
	    "access_control_allow_headers",
	    "*",
	    "access_control_allow_methods",
	    "*",
	    "access_control_allow_origin",
	    "*",
	};
	auto server = std::make_shared<CivetServer>(options);
	auto ws = std::make_shared<WebSocketBase>("simple");
	server->addWebSocketHandler("/ws", *ws);
	while (true) {
		sleep(1);
	}
	server->close();
	return 0;
}