Skip to content

Commit

Permalink
AsyncServer Setting #2
Browse files Browse the repository at this point in the history
We did it fucking shit god damnit MARSHAL
  • Loading branch information
MW-Lee committed Mar 12, 2020
1 parent 3f173d5 commit 7fe48e2
Show file tree
Hide file tree
Showing 6 changed files with 256 additions and 185 deletions.
Binary file modified Assets/Resources/Displayer.json
Binary file not shown.
2 changes: 1 addition & 1 deletion Assets/Scenes/SampleScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ MonoBehaviour:
m_GameObject: {fileID: 727657722}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: d0b1eb40e20952d4d860e49dc7783376, type: 3}
m_Script: {fileID: 11500000, guid: 377910a02793ec944bacaaa6964372f0, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!4 &727657724
Expand Down
60 changes: 46 additions & 14 deletions Assets/Scripts/AsyncServer/AsyncClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class StateObject
{
public Socket workSocket = null;

public const int BufferSize = 512;
public const int BufferSize = 1024;

public byte[] buffer = new byte[BufferSize];

Expand All @@ -23,7 +23,7 @@ public class AsyncClient : MonoBehaviour
{
public Socket socket;

string ipAdress = "127.0.0.1";
string ipAdress = "192.168.43.35";

private int port = 31400;

Expand All @@ -36,6 +36,7 @@ public class AsyncClient : MonoBehaviour
string response;

private JsonMgr jsonmgr = new JsonMgr();
string datapath;

byte[] sendBuffer = new byte[512];

Expand All @@ -48,6 +49,8 @@ private void Start()
ipep = new IPEndPoint(ipAddr, port);

Connect(ipep, socket);

datapath = Application.dataPath + "/Resources";
}

private void Update()
Expand Down Expand Up @@ -96,9 +99,7 @@ private void Send(Socket client, byte[] datas)

byte[] Buffer = new byte[8 + data.Length];

PACKET_HEADER temp;
temp.nID = 6;
temp.nSize = (short)Buffer.Length;
PACKET_HEADER temp = new PACKET_HEADER(6, Buffer.Length);

byte[] Header = StructToByte(temp);

Expand Down Expand Up @@ -131,12 +132,13 @@ private void Receive(Socket client)
{
try
{
StateObject state = new StateObject();

state.workSocket = client;
StateObject state = new StateObject
{
workSocket = client
};

client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);
new AsyncCallback(ReceiveCallback), state);
}
catch (Exception e)
{
Expand All @@ -154,9 +156,31 @@ private void ReceiveCallback(IAsyncResult ar)

int bytesRead = client.EndReceive(ar);

if(bytesRead >0)
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<PACKET_HEADER>());
tempPH = ByteToStruct<PACKET_HEADER>(tempByte, Marshal.SizeOf<PACKET_HEADER>());
Array.Clear(tempByte, 0, tempByte.Length);

if (tempPH.nID == 7)
{
Array.Copy(state.buffer, Marshal.SizeOf<PACKET_HEADER>(), tempByte, 0, Marshal.SizeOf<PKT_NOTICE_CHAT>());
tempPNC = ByteToStruct<PKT_NOTICE_CHAT>(tempByte, Marshal.SizeOf<PKT_NOTICE_CHAT>());
Array.Clear(tempByte, 0, tempByte.Length);
}

//Array.Copy(state.buffer, Marshal.SizeOf<PKT_NOTICE_CHAT>(), tempByte, 0, (state.buffer.Length - Marshal.SizeOf<PKT_NOTICE_CHAT>()));
////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);
Expand Down Expand Up @@ -189,13 +213,21 @@ private byte[] StructToByte(object _obj)
return arr;
}

private T ByteToStruct<T>(byte[] _inputHeader) where T : struct
private T ByteToStruct<T>(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;
Expand Down
39 changes: 39 additions & 0 deletions Assets/Scripts/BaseScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
////////////////////////////////////////////////
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;

public static class Constant
Expand All @@ -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;
//}
}
Loading

0 comments on commit 7fe48e2

Please sign in to comment.