From 7fe48e2c84bc944050da1392d689b271453befb8 Mon Sep 17 00:00:00 2001 From: MWLee Date: Thu, 12 Mar 2020 19:55:30 +0900 Subject: [PATCH] AsyncServer Setting #2 We did it fucking shit god damnit MARSHAL --- Assets/Resources/Displayer.json | Bin 152 -> 512 bytes Assets/Scenes/SampleScene.unity | 2 +- Assets/Scripts/AsyncServer/AsyncClient.cs | 60 +++- Assets/Scripts/BaseScript.cs | 39 +++ Assets/Scripts/TransportTCP.cs | 336 +++++++++++----------- ProjectSettings/ProjectVersion.txt | 4 +- 6 files changed, 256 insertions(+), 185 deletions(-) diff --git a/Assets/Resources/Displayer.json b/Assets/Resources/Displayer.json index 0ed30f8038737030c09b9bd21d7b27f5753e106d..2ab2b4a7b25df16c89f707c9345a050254c110c9 100644 GIT binary patch literal 512 zcmb`0BC{^Z?S%llv+FFKDK#vdr08Nu70) + if(bytesRead > 0) { - state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead)); + state.sb.Append(Encoding.UTF8.GetString(state.buffer, 0, bytesRead)); + + PACKET_HEADER tempPH = new PACKET_HEADER(); + PKT_NOTICE_CHAT tempPNC = new PKT_NOTICE_CHAT(); + byte[] tempByte = new byte[1024]; + + Array.Copy(state.buffer, 0, tempByte, 0, Marshal.SizeOf()); + tempPH = ByteToStruct(tempByte, Marshal.SizeOf()); + Array.Clear(tempByte, 0, tempByte.Length); + + if (tempPH.nID == 7) + { + Array.Copy(state.buffer, Marshal.SizeOf(), tempByte, 0, Marshal.SizeOf()); + tempPNC = ByteToStruct(tempByte, Marshal.SizeOf()); + Array.Clear(tempByte, 0, tempByte.Length); + } + + //Array.Copy(state.buffer, Marshal.SizeOf(), tempByte, 0, (state.buffer.Length - Marshal.SizeOf())); + ////string msg = Encoding.Default.GetString(state.buffer); + //string msg = Encoding.Default.GetString(state.buffer); + //string[] DummyMsg = msg.Split('\0'); + + jsonmgr.CreateJsonFile(datapath, "Displayer", new string(tempPNC.szMessage)); client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state); @@ -189,13 +213,21 @@ private byte[] StructToByte(object _obj) return arr; } - private T ByteToStruct(byte[] _inputHeader) where T : struct + private T ByteToStruct(byte[] _inputHeader, int size) where T : new() { - int size = 4; IntPtr ptr = Marshal.AllocHGlobal(size); + T oResult = new T(); Marshal.Copy(_inputHeader, 0, ptr, size); - T oResult = (T)Marshal.PtrToStructure(ptr, typeof(T)); + try + { + oResult = (T)Marshal.PtrToStructure(ptr, typeof(T)); + } + catch(Exception e) + { + Debug.Log(e.ToString()); + } + Marshal.FreeHGlobal(ptr); return oResult; diff --git a/Assets/Scripts/BaseScript.cs b/Assets/Scripts/BaseScript.cs index 6286ea9..fdc98c8 100644 --- a/Assets/Scripts/BaseScript.cs +++ b/Assets/Scripts/BaseScript.cs @@ -9,6 +9,7 @@ //////////////////////////////////////////////// using System.Collections; using System.Collections.Generic; +using System.Runtime.InteropServices; using UnityEngine; public static class Constant @@ -20,16 +21,54 @@ public static class Constant public const short REQ_CHAT = 6; public const short NOTICE_CHAT = 7; + + // + // + // + public const int MAX_NAME_LEN = 17; + + public const int MAX_MESSAGE_LEN = 512; + + public const int MAX_RECEIVE_BUFFER_LEN = 512; } +[StructLayout(LayoutKind.Sequential, Pack = 1)] public struct PACKET_HEADER { // c++ 에서 short = 2byte c# 에서는 4byte 이므로 int 형으로 변경 public int nID; public int nSize; + + //public PACKET_HEADER() + //{ + // nID = 0; + // nSize = 0; + //} + + public PACKET_HEADER(int id, int size) + { + nID = id; + nSize = size; + } } +[StructLayout(LayoutKind.Sequential)] public struct PKT_NOTICE_CHAT { + [MarshalAs(UnmanagedType.ByValArray,SizeConst = Constant.MAX_NAME_LEN)] + public char[] szName; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = Constant.MAX_MESSAGE_LEN)] + public char[] szMessage; + + //string szName; + //string szMessage; + + //public PKT_NOTICE_CHAT() + //{ + // //szName = new char[Constant.MAX_NAME_LEN]; + // //szMessage = new char[Constant.MAX_MESSAGE_LEN]; + // //szName = string.Empty; + // //szMessage = string.Empty; + //} } diff --git a/Assets/Scripts/TransportTCP.cs b/Assets/Scripts/TransportTCP.cs index 105d955..20b206e 100644 --- a/Assets/Scripts/TransportTCP.cs +++ b/Assets/Scripts/TransportTCP.cs @@ -1,185 +1,185 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; +//using System.Collections; +//using System.Collections.Generic; +//using UnityEngine; -using System.Net; -using System.Net.Sockets; -using System; -using System.Text; -using System.Runtime.InteropServices; +//using System.Net; +//using System.Net.Sockets; +//using System; +//using System.Text; +//using System.Runtime.InteropServices; -public class TransportTCP : MonoBehaviour -{ - private Socket m_socket; +//public class TransportTCP : MonoBehaviour +//{ +// private Socket m_socket; - string ipAdress = "127.0.0.1"; +// string ipAdress = "127.0.0.1"; - private int port = 31400; +// private int port = 31400; - byte[] sendBytes; +// byte[] sendBytes; - JsonMgr jsonmgr = new JsonMgr(); +// JsonMgr jsonmgr = new JsonMgr(); - string datapath = null; +// string datapath = null; - // Use this for initialization - private void Start() - { - // create socket - m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); +// // Use this for initialization +// private void Start() +// { +// // create socket +// m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - // connect - try - { - IPAddress ipAddr = IPAddress.Parse(ipAdress); +// // connect +// try +// { +// IPAddress ipAddr = IPAddress.Parse(ipAdress); - IPEndPoint ipendPoint = new IPEndPoint(ipAddr, port); +// IPEndPoint ipendPoint = new IPEndPoint(ipAddr, port); - m_socket.Connect(ipendPoint); - } - catch(SocketException se) - { - Debug.Log("Socket connect error ! : " + se.ToString()); +// m_socket.Connect(ipendPoint); +// } +// catch(SocketException se) +// { +// Debug.Log("Socket connect error ! : " + se.ToString()); - return; - } +// return; +// } - datapath = Application.dataPath + "/Resources"; - } +// datapath = Application.dataPath + "/Resources"; +// } - private void Update() - { - if(Input.GetKeyDown(KeyCode.A)) - { - try - { - //StringBuilder sb = new StringBuilder(); - - //sb.Append("Test 1 - send data!!"); - - //int i = Encoding.Default.GetByteCount(sb.ToString()); - - //byte[] d = Encoding.Default.GetBytes(sb.ToString()); - - //m_socket.Send(d, i, 0); - - handle_send(); - } - catch(Exception e) - { - Debug.Log("Socket send or receive error ! : " + e.ToString()); - } - } - else if(Input.GetKeyUp(KeyCode.D)) - { - m_socket.Disconnect(true); - - m_socket.Close(); - } - else if(Input.GetKeyUp(KeyCode.S)) - { - handle_receive(); - } - } - - // json 보내기 테스트 성공 - private void handle_send() - { - JsonClass json = new JsonClass(this.gameObject.transform, State.Idle, Vector3.zero); - string jsonData = jsonmgr.ObjectToJson(json); - - // 해더를 추가한 만큼 데이터를 보내야 하기때문에 아래쪽에서 연산한 temp.nSize 만큼 크기를 보내주기로 변경. - //int i = Encoding.Default.GetByteCount(jsonData); - - byte[] data = Encoding.UTF8.GetBytes(jsonData); - - byte[] Buffer = new byte[8 + data.Length]; - - PACKET_HEADER temp; - temp.nID = 6; - temp.nSize = (short)Buffer.Length; - - byte[] Header = StructToByte(temp); +// private void Update() +// { +// if(Input.GetKeyDown(KeyCode.A)) +// { +// try +// { +// //StringBuilder sb = new StringBuilder(); + +// //sb.Append("Test 1 - send data!!"); + +// //int i = Encoding.Default.GetByteCount(sb.ToString()); + +// //byte[] d = Encoding.Default.GetBytes(sb.ToString()); + +// //m_socket.Send(d, i, 0); + +// handle_send(); +// } +// catch(Exception e) +// { +// Debug.Log("Socket send or receive error ! : " + e.ToString()); +// } +// } +// else if(Input.GetKeyUp(KeyCode.D)) +// { +// m_socket.Disconnect(true); + +// m_socket.Close(); +// } +// else if(Input.GetKeyUp(KeyCode.S)) +// { +// handle_receive(); +// } +// } + +// // json 보내기 테스트 성공 +// private void handle_send() +// { +// JsonClass json = new JsonClass(this.gameObject.transform, State.Idle, Vector3.zero); +// string jsonData = jsonmgr.ObjectToJson(json); + +// // 해더를 추가한 만큼 데이터를 보내야 하기때문에 아래쪽에서 연산한 temp.nSize 만큼 크기를 보내주기로 변경. +// //int i = Encoding.Default.GetByteCount(jsonData); + +// byte[] data = Encoding.UTF8.GetBytes(jsonData); + +// byte[] Buffer = new byte[8 + data.Length]; + +// PACKET_HEADER temp; +// temp.nID = 6; +// temp.nSize = (short)Buffer.Length; + +// byte[] Header = StructToByte(temp); - Array.Copy(Header, 0, Buffer, 0, Header.Length); - Array.Copy(data, 0, Buffer, Header.Length, data.Length); - - m_socket.Send(Buffer, temp.nSize, 0); - } +// Array.Copy(Header, 0, Buffer, 0, Header.Length); +// Array.Copy(data, 0, Buffer, Header.Length, data.Length); + +// m_socket.Send(Buffer, temp.nSize, 0); +// } - private byte[] StructToByte(object _obj) - { - int size = Marshal.SizeOf(_obj); - byte[] arr = new byte[size]; - IntPtr ptr = Marshal.AllocHGlobal(size); +// private byte[] StructToByte(object _obj) +// { +// int size = Marshal.SizeOf(_obj); +// byte[] arr = new byte[size]; +// IntPtr ptr = Marshal.AllocHGlobal(size); - Marshal.StructureToPtr(_obj, ptr, true); - Marshal.Copy(ptr, arr, 0, size); - Marshal.FreeHGlobal(ptr); - return arr; - } - - // json 받기 테스트 - private void handle_receive() - { - try - { - byte[] bytes = new byte[512]; - - int bytesRec = m_socket.Receive(bytes); - - //Debug.Log(bytesRec); - - if(bytesRec <= 0 || bytesRec > bytes.Length) - { - return; - } - else - { - PACKET_HEADER temp = new PACKET_HEADER(); - byte[] tempByte = new byte[512]; - - Array.Copy(bytes, 0, tempByte, 0, 4); - temp = ByteToStruct(tempByte); - Array.Clear(tempByte, 0, tempByte.Length); - - Array.Copy(bytes, 20, tempByte, 0, (bytes.Length - 20)); - string msg = Encoding.Default.GetString(tempByte); - string[] DummyMsg = msg.Split('\0'); - - jsonmgr.CreateJsonFile(datapath, "Displayer", DummyMsg[0]); - } - } - catch(System.Exception e) - { - Debug.Log(e.ToString()); - } - } - - private T ByteToStruct(byte[] _inputHeader) where T : struct - { - int size = 4; - IntPtr ptr = Marshal.AllocHGlobal(size); - - Marshal.Copy(_inputHeader, 0, ptr, size); - T oResult = (T)Marshal.PtrToStructure(ptr, typeof(T)); - Marshal.FreeHGlobal(ptr); - - return oResult; - } - - private void ProcessPacket(int nSessionID, string[] pData) - { - - switch(nSessionID) - { - case Constant.RES_IN: - - break; - - case Constant.NOTICE_CHAT: - - break; - } - } -} +// Marshal.StructureToPtr(_obj, ptr, true); +// Marshal.Copy(ptr, arr, 0, size); +// Marshal.FreeHGlobal(ptr); +// return arr; +// } + +// // json 받기 테스트 +// private void handle_receive() +// { +// try +// { +// byte[] bytes = new byte[512]; + +// int bytesRec = m_socket.Receive(bytes); + +// //Debug.Log(bytesRec); + +// if(bytesRec <= 0 || bytesRec > bytes.Length) +// { +// return; +// } +// else +// { +// PACKET_HEADER temp = new PACKET_HEADER(); +// byte[] tempByte = new byte[512]; + +// Array.Copy(bytes, 0, tempByte, 0, 4); +// temp = ByteToStruct(tempByte); +// Array.Clear(tempByte, 0, tempByte.Length); + +// Array.Copy(bytes, 20, tempByte, 0, (bytes.Length - 20)); +// string msg = Encoding.Default.GetString(tempByte); +// string[] DummyMsg = msg.Split('\0'); + +// jsonmgr.CreateJsonFile(datapath, "Displayer", DummyMsg[0]); +// } +// } +// catch(System.Exception e) +// { +// Debug.Log(e.ToString()); +// } +// } + +// private T ByteToStruct(byte[] _inputHeader) where T : struct +// { +// int size = 4; +// IntPtr ptr = Marshal.AllocHGlobal(size); + +// Marshal.Copy(_inputHeader, 0, ptr, size); +// T oResult = (T)Marshal.PtrToStructure(ptr, typeof(T)); +// Marshal.FreeHGlobal(ptr); + +// return oResult; +// } + +// private void ProcessPacket(int nSessionID, string[] pData) +// { + +// switch(nSessionID) +// { +// case Constant.RES_IN: + +// break; + +// case Constant.NOTICE_CHAT: + +// break; +// } +// } +//} diff --git a/ProjectSettings/ProjectVersion.txt b/ProjectSettings/ProjectVersion.txt index 798259b..77fa359 100644 --- a/ProjectSettings/ProjectVersion.txt +++ b/ProjectSettings/ProjectVersion.txt @@ -1,2 +1,2 @@ -m_EditorVersion: 2019.3.0f6 -m_EditorVersionWithRevision: 2019.3.0f6 (27ab2135bccf) +m_EditorVersion: 2019.3.4f1 +m_EditorVersionWithRevision: 2019.3.4f1 (4f139db2fdbd)