Callbacks not firing in C# script inside a Unity plugin

553 Views Asked by At

I have a C# script that I'm attempting to put into a Unity plugin.

The script essentially is just creating a socket connection, ingesting data, and parsing it. It works perfectly when just used as a "standalone" script in Unity.

This is the code for the script:

using UnityEngine;
using System.Collections;
using System;
using System.Net;
using System.Net.Sockets;
using System.Linq;

public class ClientSocket
{
    private Socket _clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    private byte[] _recieveBuffer = new byte[8142];

    void Start()
    {
        SetupServer();
    }

    void Update()
    {

    }

    void OnApplicationQuit()
    {
        _clientSocket.Close();
    }

    private void SetupServer()
    {
        try
        {
            _clientSocket.Connect("127.0.0.1", 12345);

        }
        catch (SocketException ex)
        {
            Debug.Log(ex.Message);
        }

        _clientSocket.BeginReceive(_recieveBuffer, 0, _recieveBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);

    }

    private void ReceiveCallback(IAsyncResult AR)
    {
        //Check how much bytes are recieved and call EndRecieve to finalize handshake
        int recieved = _clientSocket.EndReceive(AR);

        if (recieved <= 0)
            return;

        //Copy the recieved data into new buffer, to avoid null bytes
        byte[] recData = new byte[recieved];
        Buffer.BlockCopy(_recieveBuffer, 0, recData, 0, recieved);

        string incomingData = System.Text.Encoding.Default.GetString(_recieveBuffer);
        Debug.Log(incomingData);            
    }
}

As I mentioned, this works great when just as a script --- a connection gets created, and my ReceiveCallback fires, and receives data from the socket connection.

Now, when I try to use essentially the same code in a Unity Plugin, the connection gets created; but the callback never fires...kind of at a loss as to why this may be different. Any ideas or pointers appreciated!

This is the slightly modified version for use in the plugin:

using UnityEngine;
using System.Collections;
using System;
using System.Net;
using System.Net.Sockets;
using System.Linq;

namespace InputsPlugin
{
    public class ClientSocket
    {
        private Socket _clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        private byte[] _recieveBuffer = new byte[8142];

        public void StartConnection()
        {
            SetupServer();
        }

        private void SetupServer()
        {
            try
            {
                _clientSocket.Connect("127.0.0.1", 12345);
                Debug.Log("Connected!!!!");
            }
            catch (SocketException ex)
            {
                Debug.Log(ex.Message);
            }

            Debug.Log("Callback?");
            _clientSocket.BeginReceive(_recieveBuffer, 0, _recieveBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
            Debug.Log("Callback?");
        }

        private void ReceiveCallback(IAsyncResult AR)
        {
            Debug.Log("Callback!!!!");

            //Check how much bytes are recieved and call EndRecieve to finalize handshake
            int recieved = _clientSocket.EndReceive(AR);
            Debug.Log(recieved);

            if (recieved <= 0)
                return;

            //Copy the recieved data into new buffer, to avoid null bytes
            byte[] recData = new byte[recieved];
            Buffer.BlockCopy(_recieveBuffer, 0, recData, 0, recieved);

            string incomingData = System.Text.Encoding.Default.GetString(_recieveBuffer);
            Debug.Log(incomingData);

            //Start receiving again
            _clientSocket.BeginReceive(_recieveBuffer, 0, _recieveBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
        }

        private void SendData(byte[] data)
        {
            SocketAsyncEventArgs socketAsyncData = new SocketAsyncEventArgs();
            socketAsyncData.SetBuffer(data, 0, data.Length);
            _clientSocket.SendAsync(socketAsyncData);
        }
    }
}
1

There are 1 best solutions below

4
Everts On

Your Start method should most likely be converted to a constructor since it is no longer called by the engine. Being a private method, it cannot be called from outside.

As a result your SetupServer is probably never called.

You can also remove the Update and OnApplicationQuit should be the destructor or some cleaning method.