I wanna make socket networking program with Unity so I made test project and added NetworkManager
class and PacketManager
class in that project for test.
When the program starts, I call connect()
method in NetworkManager
, then network connection is succeed. And I have to make packet to bytestream for sending to server. So I made PacketManager
class(static) using marshaling and a call for getBytes()
method. But It doesn't work (unity program is terminated).
Here are my scripts
Move.cs
using UnityEngine;
using System.Collections;
using System.Net.Sockets;
using System.Net;
using System;
using System.Runtime.InteropServices;
public class move : MonoBehaviour {
private NetworkManager nManager;
// Use this for initialization
void Start () {
Debug.Log ("start");
nManager = new NetworkManager ();
nManager.connect ();
packet p = new packet();
p.type = 100;
p.msg = "abcdabcd";
byte[] array = PacketManager.getBytes (p);
}
// Update is called once per frame
void Update () {
}
}
NetworkManager.cs
using UnityEngine;
using System.Collections;
using System.Net.Sockets;
using System.Net;
using System;
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct packet
{
public int type;
public int number;
public int room;
public float x, y, z;
public float rotate;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 100)]
public string msg;
}
public class NetworkManager : MonoBehaviour
{
private const string serverIP = "127.0.0.1";
private const int portNum = 9090;
private const int buffersize = 128;
private static TcpClient client = null;
private static NetworkStream netStream = null;
public NetworkManager() {}
public Boolean connect()
{
Boolean success = false;
try
{
Debug.Log("try to connenct..");
client = new TcpClient(serverIP, portNum);
netStream = client.GetStream();
Debug.Log("connected success!");
}
catch (Exception e)
{
success = false;
Debug.Log(e.Message);
}
return success;
}
public void sendPacket(packet p)
{
byte[] array = PacketManager.getBytes(p);
netStream.Write(array, 0, buffersize);
}
public packet recvPacket()
{
packet p = new packet();
byte[] bytebuffer = new Byte[buffersize];
int totalByteRcvd = 0;
int bytesRcvd = 0;
while (totalByteRcvd < buffersize)
{
if ((bytesRcvd = netStream.Read(bytebuffer, totalByteRcvd, buffersize - totalByteRcvd)) == 0)
{
p = PacketManager.BytesToSturct<packet>(bytebuffer);
break;
}
totalByteRcvd += bytesRcvd;
Debug.Log("byte : " + totalByteRcvd);
}
return p;
}
}
PacketManager.cs
using UnityEngine;
using System.Collections;
using System.Net.Sockets;
using System.Net;
using System;
using System.Runtime.InteropServices;
public class PacketManager : MonoBehaviour
{
public static byte[] getBytes(object str)
{
int size = Marshal.SizeOf(str);
byte[] arr = new byte[size];
IntPtr ptr = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(str, ptr, true);
Marshal.Copy(ptr, arr, 0, size);
Marshal.FreeHGlobal(ptr);
return arr;
}
public static T BytesToSturct<T>(byte[] buffer) where T : struct
{
int size = Marshal.SizeOf(typeof(T));
IntPtr ptr = Marshal.AllocHGlobal(size);
Marshal.Copy(buffer, 0, ptr, size);
T obj = (T)Marshal.PtrToStructure(ptr, typeof(T));
Marshal.FreeHGlobal(ptr);
return obj;
}
}
If I remove:
byte[] array = PacketManager.getBytes (p);
in move.cs
, Logs are printed well, but if i call any method (not only that method), program terminated.
What is the problem?