2

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);
        }
    }
}
derHugo
  • 83,094
  • 9
  • 75
  • 115
narner
  • 2,908
  • 3
  • 26
  • 63

1 Answers1

3

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.

Everts
  • 10,408
  • 2
  • 34
  • 45
  • Yup; you're correct about that -- I did make some of those changes already actually; edited my question for better clarity. – narner Mar 06 '19 at 06:46
  • you never call private void SetupServer() this must be done in the Constructor or from outside (than it must be public) – Vampirasu Mar 06 '19 at 09:51
  • Yes, I'm calling private void SetupServer() from public void StartConnection(), which is called from a script that's interfacing with this plugin. – narner Mar 06 '19 at 16:27
  • Are you getting anything on console? Warnings, errors or statements? – Saad Anees Mar 14 '19 at 09:56