Author Topic: Call a function from another program [Solved]  (Read 2746 times)

Simplified version of the C# code with IP removed because I'm paranoid:
Code: [Select]
using System;
using System.Net;
using System.Net.Sockets;

.
.
.

        static int linecount = 5;
        static string[] lines = {"a", "b", "c", "defghijk", "this is a long string aaaaaaaa"};

.
.
.

        static void blCommunicate() {
            Console.Clear();
            try {
                Console.WriteLine("Creating the TCP server...");
                IPAddress me = IPAddress.Parse("-IP-");
                TcpListener serv = new TcpListener(me, -port-);

                Console.WriteLine("Initializing server at " + me + ":-port-.");
                serv.Start();
                Console.WriteLine("Received endpoint " + serv.LocalEndpoint);

                Socket s = null;
                bool foundCli = false;
                while (foundCli == false) {
                    Console.WriteLine("Awaiting connections.");
                    s = serv.AcceptSocket();
                    Console.WriteLine("Found connection: " + s.RemoteEndPoint);
                    Console.WriteLine("Begin transmit " + serv.LocalEndpoint + " -> " + s.RemoteEndPoint + "? Y/N");
                    if (Console.ReadKey().Key.ToString().ToUpper() == "Y") foundCli = true;
                }

                Console.WriteLine("Sending " + linecount + " strings...");
                for (int x = 0; x < linecount; x ++) {
                    s.Send(new System.Text.ASCIIEncoding().GetBytes(lines[x]));

                    byte[] bread = new byte[1025];
                    int k = s.Receive(bread, 1024, SocketFlags.None);
                    for (int i = 0; i < k; i++)
                        Console.Write(Convert.ToChar(bread[i]));
                }

                Console.WriteLine("Ending server...");
                s.Close();
                serv.Stop();

            } catch (Exception ex) {
                Console.WriteLine("Exception encountered in net communication!");
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
            Console.Beep();
            Console.WriteLine("Press ENTER to quit.");
            Console.ReadLine();
        }

I need to make a receiver for this in Blockland. The problem is I need the ASCII thing there to send anything, which needs a decoder Blockland-side. Also, the lines are going to be around 30 or 35 characters long - is this too much?
« Last Edit: January 19, 2013, 11:44:47 AM by ThinkInvisible »

the lines are going to be around 30 or 35 characters long - is this too much?
Nope

Also, there are getUTF8String(str) and getASCIIString(str) functions, they may be related to that decoding issue.

Never mind, apparently Blockland decodes it or something.

But if I try to make Blockland the server, it keeps saying 'Got bad connected receive event.' instead of echoing the string:
Code: [Select]
function beginListening() {
if(isObject(TCPTest)) {TCPTest.Delete();}
new TCPObject(TCPTest) {};
TCPTest.listen(-port-);
}

function TCPTest::onLine(%this, %line) {
    echo(%line);
%this.send("confirmed");
}

Also for some reason if Blockland is the client it only runs onLine once the server closes, so I have to call it manually for every line and then it gives it all in one echo at the end.
« Last Edit: January 19, 2013, 12:19:46 PM by ThinkInvisible »

I found this in the system_rtbhosting mod. May be useful for your problem.
Code: [Select]
//***************************************************
//* Communication Listener
//***************************************************
//- RTB_Hosting_CommServer::onConnectRequest (creates a new connection for incoming requests)
function RTB_Hosting_CommServer::onConnectRequest(%this, %address, %socket)
{
    %connection = new TCPObject(RTB_Hosting_CommClient, %socket);
    if(strstr(%address, "IP:127.0.0.1") !$= 0)
    {
        %connection.disconnect();
        %connection.delete();
    }
}

//- RTB_Hosting_CommClient::onLine (callback from a comms client)
function RTB_Hosting_CommClient::onLine(%this, %line)
{
    //onLine code here
}

Also for some reason if Blockland is the client it only runs onLine once the server closes, so I have to call it manually for every line and then it gives it all in one echo at the end.
Put \r\n at the end of the line so the server knows that you are done with sending a line

Put \r\n at the end of the line so the server knows that you are done with sending a line
that fixed it, it works now!

the test anyways!

yay!

Now, what are you doing with this?
Making a remote console of some sort?
Using an external program for insane calculations because blockland is too slow?

Simplified version of the C# code with IP removed because I'm paranoid:
For several reasons I would use 127.0.0.1 instead of your external IP


No but can someone tell me what's up with that address? I've seen it so many time
It's a loopback address.
It's goes back to your own computer.
http://en.wikipedia.org/wiki/Localhost

This is of course assuming the blockland server and the C# application are both running on the same computer
« Last Edit: January 19, 2013, 12:50:36 PM by Headcrab Zombie »

So the 127.0.0.1 in the rtb and kalp remote consoles are because the connection is coming from the same computer?

Torque combines listeners and clients into one object. A listener is a TCP server, and a client connects to a TCP server. Blockland automatically detects which to spawn when you call either ::listen or ::connect.

Now, what are you doing with this?
Making a remote console of some sort?
Using an external program for insane calculations because blockland is too slow?
No and yes for the last one. it's for a terrain generator. I'm sending Blockland the brick coordinates directly to avoid having to make a save file and everything.

BUT BRICK PLACEMENT STILL WON'T WORK RIGHT AARGH.

One last problem. For some reason if I don't allow more lines to be sent until AFTER the brick is done planting, lots of bricks glitch out. Is there a better way to plant bricks that I'm missing out on?

Current brick plant code:
Code: [Select]
function addListenerBrick(%datablock, %pos, %a, %color, %id, %rend, %rayc, %coll) {
switch(%a) {
case 0:
%rot = "1 0 0 0";
case 1:
%rot = "0 0 1 90";
case 2:
%rot = "0 0 1 180";
case 3:
%rot = "0 0 1 -90";
}
%brick = new fxDTSBrick() {
datablock = %datablock;
position = %pos;
rotation = %rot;
angleID = %a;
scale = "1 1 1";
colorID = %color;
colorFxID = 0;
shapeFxID = 0;
isplanted = "1";
stackbl_id = %id;
};
//rendering = %ren;
//raycasting = %ray;
//collision = %col;
%brick.client = findclientbybl_id(%id);
%error = %brick.plant();
if(%error > 2) {
%brick.delete();
echo("Error placing brick: " @ %error);
return(%error);
}
brickgroup_14511.add(%brick);
%brick.setTrusted(1);
return("planted!");
}

Post the code calling that.