MM2 socket networking

Discuss, ask for help, share ideas, give suggestions, read tutorials, and tell us about bugs you have found with MicroMacro in here.

Do not post RoM-Bot stuff here. There is a subforum for that.
Forum rules
This is a sub-forum for things specific to MicroMacro.

This is not the place to ask questions about the RoM bot, which uses MicroMacro. There is a difference.
Post Reply
Message
Author
User avatar
Administrator
Site Admin
Posts: 5307
Joined: Sat Jan 05, 2008 4:21 pm

MM2 socket networking

#1 Post by Administrator » Tue Dec 02, 2014 12:51 pm

I've finally worked out (most of) the kinks and have an alpha version available for you. The attached zip file contains just the MicroMacro 1.9.22 debug executable which contains the new networking functions. It is still considered experimental and subject to change.


Functions:
network.socket(["tcp" | "udp"]) - Creates a new socket in either TCP or UDP mode; defaults to TCP. Returns the socket as an object. NOTE: UDP unfinished.
socket:connect("host/IP", port) - Connects a socket to a server, returns true/false and error message. Retruns true/false and error message (on fail)
socket:listen("host/IP", port) - Sets the socket to listen (as a server) for incoming connections, host/IP and port are the ones to listen to; use "" for the host/IP to listen on any available connections. Returns true/false and error message (on fail)
socket:send("message goes here") - Send a message to the remote destination on this socket. Returns true/false and error message (on fail)
socket:close() - Shuts down the connection; this also happens automatically if/when the socket object is destroyed
socket:socket() - Returns the socket ID (an integer value)


Events:
socketconnected - A server application pushes this event when a new client has connected. The new socket object for this client will be pushed as the second (and only) additional parameter.
socketreceived - Whenever a socket receives any message, this event is pushed. Additional parameters are the socket ID (not object!) and the actual message.
socketdisconnected - When a connection is closed for any reason, this event is raised.The socket ID is also pushed as the second and only parameter.
socketerror - whenever there's any unhandled error on a socket. Socket ID and error number are also pushed.


Notes:
All sockets are run in separate threads and will not block or limit your application.
All received data is, therefor, handled internally and only accessible through the event system.
Make sure you notice that socketconnected pushes a socket object while other socket events only receive the socket ID; a server application should store new clients in a list for use, otherwise the connection could be closed when the variable goes out of scope.


Examples:

Here's a basic client/server setup. It initiates the connections, handled multiple clients, and sends some messages.

server.lua

Code: Select all

local serverSocket;
local clients;

function macro.init()
	clients = {};
	serverSocket = network.socket();
	local success,msg = serverSocket:listen("", 9999);
	if( not success ) then
		error("Failed to listen: " .. msg, 0);
	end

	print("Waiting for clients");
end

function macro.main(dt)

end

function macro.event(e, ...)
	if( e == "socketconnected" ) then
		sock = ...;
		print("New client found:", sock);
		clients[sock:socket()] = sock;
		sock:send("Greetings from the server");
	end
	if( e == "socketreceived" ) then
		sock, msg = ...;
		print("Data received from client:", sock, msg);
	end
	if( e == "socketdisconnected" ) then
		sock = ...;
		print("Client disconnected", sock);
		clients[sock] = nil;
	end
end
client.lua

Code: Select all

function macro.init()
	socket = network.socket();
	local success,msg = socket:connect("127.0.0.1", 9999);
	if( not success ) then
		error("Failed to listen: " .. msg, 0);
	end

	print("Connected to host");
	socket:send("Hello!");
end

function macro.main(dt)

end

function macro.event(e, ...)
	if( e == "socketreceived" ) then
		sock, msg = ...;
		print("Data received from server", msg);
	end
	if( e == "socketdisconnected" ) then
		sock = ...;
		print("Socket disconnected", sock);
	end
end

Accessing HTML content from webservers is not much different. Just open a socket in TCP mode, connect on port 80, and send the standard HTTP request header.
web.lua:

Code: Select all

function macro.init()
	local socket = network.socket("tcp");
	print(socket);

	local connected,err = socket:connect("www.google.com", 80);
	print("Connected:", connected, err);

	print("Send:", socket:send("GET / HTTP/1.1\r\n\r\n"));
end

function macro.main()
	return true;
end

function macro.event(e, ...)
	if( e == "socketconnected" ) then
		print(e, ...);
	end
	if( e == "socketreceived" ) then
		print("Socket received:", ...);
	end
	if( e == "socketdisconnected" ) then
		print("Socket closed.", ...);
	end
	if( e == "socketerror" ) then
		print("Socket error", ...);
	end
end
Attachments
micromacro.1.9.22.debug.zip
(841.45 KiB) Downloaded 170 times

User avatar
BlubBlab
Posts: 948
Joined: Fri Nov 30, 2012 11:33 pm
Location: My little Pony cafe

Re: MM2 socket networking

#2 Post by BlubBlab » Sun Jan 04, 2015 5:33 pm

Since you released this version I thought for what we could use it:
+ Write an App with which you can control your bot/MM2 remotely
+ Maybe for faking the work of HS.
When I extend that direction I end up making a man-in-the-middle attack e.g. against the AA client spoofing the sent information from the client to the server. Which properly this MM won't support , yeah I know way too hackish the problem with that is there all if we talk about HS.( I dived also into the making of kernel driver you can make a test driver in 5 minutes with VS but how the hell should I deploy this?)

In theory you could also use MM with sockets and a mysql add-on to implement a server but I think this is a terrible idea, unfortunately I saw in a video how RW implemented the AI for the monsters in DP in LUA server-side :oops: (To be clear this would slow down everything enormously a lot of unnecessary functions calls and the compiler would be unable to optimize the code and lags through the garbage collector)
Jack-of-all-trades, but master-of-only of a few :D

My Reps:
https://github.com/BlubBlab/Micromacro-with-OpenCV (My version of MM2 with OpenCV and for MS Visual Studio)
https://github.com/BlubBlab/rom-bot (rombot with no stop WP and advanced human emulation mode and some other extensions)
https://github.com/BlubBlab/Micromacro-2-Bot-Framework ( A work in progress )
My Tools : viewtopic.php?f=10&t=6226

User avatar
Administrator
Site Admin
Posts: 5307
Joined: Sat Jan 05, 2008 4:21 pm

Re: MM2 socket networking

#3 Post by Administrator » Mon Jan 05, 2015 12:51 pm

There's all kinds of things that have become possible. I've toyed with the idea of having a mini web-server built into the bot so that you'll be able to control it from your browser locally, in the network, or across the internet. We can also do clientless bots now (and the MM2 architecture makes this much easier). We can have updated automatically downloaded and installed. We could have a central web-accessible database (for what? I don't know. It's just a rough idea) that any clients could query from. All kinds of fun stuff, really.

User avatar
BlubBlab
Posts: 948
Joined: Fri Nov 30, 2012 11:33 pm
Location: My little Pony cafe

Re: MM2 socket networking

#4 Post by BlubBlab » Mon Jan 05, 2015 2:27 pm

A packet bot was a little too obviously so I overlooked it to mentioned.
With app I also meant to use either a smart-phone client or a HP with an RPC/XML script to control MM remotely. What also came to my mind we could combine things and send screenshots over a socket so you can built a self made teamviewer.

Basically it is the same as I'm with my open cv support for MM I have everything I want now in it and someone needs to say there is a bug or I need something else additionally.
Jack-of-all-trades, but master-of-only of a few :D

My Reps:
https://github.com/BlubBlab/Micromacro-with-OpenCV (My version of MM2 with OpenCV and for MS Visual Studio)
https://github.com/BlubBlab/rom-bot (rombot with no stop WP and advanced human emulation mode and some other extensions)
https://github.com/BlubBlab/Micromacro-2-Bot-Framework ( A work in progress )
My Tools : viewtopic.php?f=10&t=6226

Post Reply

Who is online

Users browsing this forum: No registered users and 11 guests