summaryrefslogtreecommitdiffstatshomepage
path: root/src/osd/modules/output/network.cpp
blob: 6c0af498204c3df30f57cb620a7e3a325fc0d480 (plain) (blame)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// license:BSD-3-Clause
// copyright-holders:Miodrag Milanovic
/***************************************************************************

    network.cpp

    Network output interface.

***************************************************************************/

#include "output_module.h"
#include "modules/osdmodule.h"
#include "modules/lib/osdobj_common.h"

#include "emu.h"

#include <thread>
#include <set>
#include "asio.h"

class output_client
{
public:
  virtual ~output_client() {}
  virtual void deliver(std::string &msg) = 0;
};

using output_client_ptr = std::shared_ptr<output_client>;
using client_set = std::set<output_client_ptr>;

class output_session : public output_client,
		public std::enable_shared_from_this<output_session>

{
public:
  output_session(asio::ip::tcp::socket socket, client_set *clients) :
	m_socket(std::move(socket)),
	m_clients(clients)
  {
  }

  void start(running_machine &machine)
  {
	m_machine = &machine;
	m_clients->insert(shared_from_this());
	// now send "mame_start = rom" to the newly connected client
	std::snprintf(m_data, max_length, "mame_start = %s\r", machine.system().name);
	do_write(std::strlen(m_data));
	do_read();
  }

private:
  void deliver(std::string &msg)
  {
	std::strncpy(m_data, msg.c_str(), max_length);
	do_write(msg.size());
  }

  void handle_message(char *msg)
  {
	const char *equals_delimiter = " = ";
	char *msg_name = strtok(msg, equals_delimiter);
	char *msg_value = strtok(NULL, equals_delimiter);

	//printf("handle_message: msg_name [%s] msg_value [%s]\n", msg_name, msg_value);
	
	if (std::strcmp(msg_name, "mame_message") == 0)
	{
		const char *comma_delimiter = ",";
		msg_name = strtok(msg_value, comma_delimiter);
		msg_value = strtok(NULL, comma_delimiter);
		int id = atoi(msg_name);
		int value = atoi(msg_value);
		
		switch(id)
		{
		case IM_MAME_PAUSE:
			if (value == 1 && !machine().paused())
			{
				machine().pause();
			}
			else if (value == 0 && machine().paused())
			{
				machine().resume();
			}
			break;
		case IM_MAME_SAVESTATE:
			if (value == 0)
			{
				machine().schedule_load("auto");
			}
			else if (value == 1)
			{
				machine().schedule_save("auto");
			}
			break;
		}
  }
}

  void do_read()
  {
	auto self(shared_from_this());
	m_socket.async_read_some(asio::buffer(m_input_m_data, max_length),
		[this, self](std::error_code ec, std::size_t length)
		{
		  if (!ec)
		  {
				if (length > 0)
				{
					m_input_m_data[length] = '\0';
					handle_message(m_input_m_data);
				}
				do_read();
		  }
		  else
		  {
				m_clients->erase(shared_from_this());
		  }
		});
  }

  void do_write(std::size_t length)
  {
	auto self(shared_from_this());
		asio::async_write(m_socket, asio::buffer(m_data, length),
		[this, self](std::error_code ec, std::size_t /*length*/)
		{
		  if (ec)
		  {
			m_clients->erase(shared_from_this());
		  }
		});
  }

  running_machine &machine() const { return *m_machine; }

  asio::ip::tcp::socket m_socket;
  enum { max_length = 1024 };
  char m_data[max_length];
  char m_input_m_data[max_length];
  client_set *m_clients;
  running_machine *m_machine;
};

class output_network_server
{
public:
  output_network_server(asio::io_context& io_context, short port, running_machine &machine) :
	m_acceptor(io_context, asio::ip::tcp::endpoint(asio::ip::tcp::v4(), port))
  {
	m_machine = &machine;
	do_accept();
  }

  void deliver_to_all(std::string msg)
  {
	for (auto client: m_clients)
	  client->deliver(msg);
  }

private:
  void do_accept()
  {
	m_acceptor.async_accept(
		[this](std::error_code ec, asio::ip::tcp::socket socket)
		{
		  if (!ec)
		  {
			std::make_shared<output_session>(std::move(socket),&m_clients)->start(machine());
		  }

		  do_accept();
		});
  }

  running_machine &machine() const { return *m_machine; }

  asio::ip::tcp::acceptor m_acceptor;
  client_set m_clients;
  running_machine *m_machine;
};


class output_network : public osd_module, public output_module
{
public:
	output_network()
	: osd_module(OSD_OUTPUT_PROVIDER, "network"),
	  output_module(),
	  m_io_context(nullptr), m_server(nullptr)
	{
	}

	virtual ~output_network()
	{
	}

	virtual int init(const osd_options &options) override
	{
		m_working_thread = std::thread([](output_network* self) { self->process_output(); }, this);
		return 0;
	}

	virtual void exit() override
	{
		// tell clients MAME is shutting down
		notify("mame_stop", 1);
		m_io_context->stop();
		m_working_thread.join();
		delete m_server;
		delete m_io_context;
	}

	// output_module

	virtual void notify(const char *outname, int32_t value) override
	{
		static char buf[256];
		sprintf(buf, "%s = %d\r", ((outname==nullptr) ? "none" : outname), value);
		m_server->deliver_to_all(buf);
	}

	// implementation
	void process_output()
	{
		m_io_context = new asio::io_context();
		m_server = new output_network_server(*m_io_context, 8000, machine());
		m_io_context->run();
	}

private:
	std::thread m_working_thread;
	asio::io_context *m_io_context;
	output_network_server *m_server;
};

MODULE_DEFINITION(OUTPUT_NETWORK, output_network)