Skip to content

Instantly share code, notes, and snippets.

@Mystfit
Created October 17, 2015 14:58
Show Gist options
  • Select an option

  • Save Mystfit/b51b27d7fc091874313d to your computer and use it in GitHub Desktop.

Select an option

Save Mystfit/b51b27d7fc091874313d to your computer and use it in GitHub Desktop.
Lightweight Server/Client for testing Netmq Unity issues

A small client/server application using C# and Unity to track down issues with AsyncIO not triggering OnCompleted. This test was performed on Windows 10 using Unity 5.2.1f and Visual Studio Community 2015.

  1. Create a new Unity project. Add ReqRepUnity.cs as a component to a new GameObject. Enter the ip and port of the server in the "address" and "port" fields of the component.
  2. Compile NetMQ3.5 using AsyncIO.Unity instead of the default binary. Copy all the files from \src\Net35\bin<Release> to \Assets\plugins
  3. Add \Source\AsyncIO\AsyncIO.Unity.csproj to the Unity project's C# solution.
  4. From the AsyncIO.Unity project, open DotNet\NativeSocket.cs and place a breakpoint on line 58. This should be the line "operationType = OperationType.Send;"
  5. Create a new C# project on another computer using Server.cs and run the program.
  6. Attach the debugger to Unity and hit play.

The result of this should be the debugger stopping 4 times on the NativeSocket.cs breakpoint. The first 3 times are during the socket.connect()stage, the last is when the message is actually sent.

If the breakpoint is removed, the message does not seem to be sent to the destination socket. PS: When adding hit conditions to the breakpoint, occasionally the breakpoint will still fail to fire. The line hit count will usually range from 1-2 hits rather than the expected 4.

using UnityEngine;
using NetMQ;
using System.Collections;
public class ReqRepTest : MonoBehaviour
{
NetMQSocket client;
NetMQContext ctx;
public string greeting = "Hello";
public string address = "192.168.1.127";
public int port = 6001;
void Start()
{
ctx = NetMQContext.Create();
client = ctx.CreateRequestSocket();
client.Options.Linger = System.TimeSpan.FromSeconds(0);
client.Connect("tcp://" + address + ":" + port.ToString());
Debug.Log(string.Format("Sending message " + greeting));
NetMQMessage msg = new NetMQMessage();
msg.Append(greeting);
client.SendMessage(msg);
NetMQMessage responseMsg = new NetMQMessage();
client.TryReceiveMultipartMessage(System.TimeSpan.FromSeconds(3), ref responseMsg);
if (responseMsg.IsEmpty)
throw new System.Exception("No frames in message. Unity will now hang when stopped. :(");
else
Debug.Log("Response: " + responseMsg[0].ConvertToString());
}
public void OnApplicationQuit()
{
client.Dispose();
ctx.Dispose();
}
}
using System;
using NetMQ;
namespace UnityServer
{
class MainClass
{
public static void Main (string[] args)
{
int port = 6001;
NetMQContext ctx = NetMQContext.Create ();
NetMQSocket server = ctx.CreateResponseSocket ();
server.Options.Linger = System.TimeSpan.FromSeconds (0);
server.Bind ("tcp://*:" + port.ToString());
Console.WriteLine ("Listening on port " + port.ToString ());
while (true) {
NetMQMessage msg = server.ReceiveMultipartMessage ();
Console.WriteLine("Received " + msg.ToString());
NetMQMessage resp = new NetMQMessage ();
resp.Append ("Sup");
server.SendMessage(resp);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment