Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix game freeze and modify TCP connection #91

Merged
merged 1 commit into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using UnityEngine;
using Google.FlatBuffers;
using static UnityEngine.Application;
using UnityEditor.SceneManagement;

namespace BocchiTracker
{
Expand All @@ -16,7 +17,7 @@ public class BocchiTrackerSystem : MonoBehaviour
{
public Queue<BocchiTrackerLocation> JumpRequest { get; set; } = new Queue<BocchiTrackerLocation>();

private static BocchiTrackerTcpSocket tcpSocket;
private BocchiTrackerTcpSocket tcpSocket;
private BocchiTrackerSetting setting;
private BocchiTrackerLogHook logHook;
private bool isSentAppBasicInfo;
Expand All @@ -26,7 +27,6 @@ public BocchiTrackerSystem()
{
if (tcpSocket == null)
tcpSocket = new BocchiTrackerTcpSocket();

}

private void Awake()
Expand All @@ -35,14 +35,13 @@ private void Awake()
logHook = new BocchiTrackerLogHook();
}

private async void Start()
private void Start()
{
setting = GetComponent<BocchiTrackerSetting>();
if(tcpSocket == null)
tcpSocket = gameObject.AddComponent<BocchiTrackerTcpSocket>();
if (!IsConnect())
{
isSentAppBasicInfo = false;
await tcpSocket.Connect(setting.ServerAddress, setting.ServerPort);
}
tcpSocket.ReciveCallback = this.OnReceiveData;
}

Expand All @@ -51,16 +50,14 @@ private void OnDestroy()
tcpSocket.DisConnect();
}

private async void Update()
private void Update()
{
if (IsConnect())
{
if (!isSentAppBasicInfo)
ProcessSendAppBasicInfo();

ProcessSendLogMessage();

await tcpSocket.Update();
}
}

Expand Down Expand Up @@ -100,7 +97,7 @@ private void LateUpdate()

public bool IsConnect()
{
return tcpSocket.IsConnect();
return tcpSocket != null && tcpSocket.IsConnect();
}

private void OnReceiveData(List<byte> inData)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,47 @@

using System;
using System.Threading.Tasks;
using System.Collections;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Net;
using UnityEngine;
using PlasticPipe.PlasticProtocol.Messages;

namespace BocchiTracker
{
/// <summary>
/// Manages TCP socket communication for the BocchiTracker system.
/// </summary>
public class BocchiTrackerTcpSocket
public class BocchiTrackerTcpSocket : MonoBehaviour
{
public Action<List<byte>> ReciveCallback { private get; set; } // Callback to handle received data

private Socket socket; // The TCP socket
private Queue<List<byte>> sendDataQueue = new Queue<List<byte>>(); // Queue for outgoing data

public BocchiTrackerTcpSocket()
public void Start()
{
socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
socket.Blocking = false; // Set socket to non-blocking mode

var setting = GetComponent<BocchiTrackerSetting>();
StartCoroutine(Connect(setting.ServerAddress, setting.ServerPort));
}

/// <summary>
/// Initiates a connection to the specified IP address and port.
/// </summary>
/// <param name="inIPAddress">The IP address to connect to.</param>
/// <param name="inPort">The port number to connect to.</param>
public async Task Connect(string inIPAddress, int inPort)
private IEnumerator Connect(string inIPAddress, int inPort)
{
if (IPAddress.TryParse(inIPAddress, out IPAddress ipAddress))
{
while (!IsConnect())
{
try
{
await socket.ConnectAsync(ipAddress, inPort);
Debug.Log("Connected to server.");
}
catch { }
}
Task connectTask = socket.ConnectAsync(ipAddress, inPort);
yield return new WaitUntil(() => connectTask.IsCompleted);
}
yield break;
}

/// <summary>
Expand All @@ -59,13 +58,13 @@ public void DisConnect()
/// <summary>
/// Updates the socket, processing send and receive operations.
/// </summary>
public async Task Update()
public void Update()
{
if (sendDataQueue.TryDequeue(out List<byte> data))
{
await ProcessSendData(data);
StartCoroutine(ProcessSendData(data));
}
await ProcessReceiveData();
StartCoroutine(ProcessReceiveData());
}

/// <summary>
Expand All @@ -77,30 +76,26 @@ public void AddSendData(List<byte> inData)
sendDataQueue.Enqueue(inData);
}

private async Task ProcessSendData(List<byte> inData)
private IEnumerator ProcessSendData(List<byte> inData)
{
if (!IsConnect())
return;
yield break;

int bytesSent = await socket.SendAsync(inData.ToArray(), SocketFlags.None);
if (bytesSent > 0)
{
Console.WriteLine("Data sent successfully: " + bytesSent + " bytes");
}
else
{
Console.WriteLine("Failed to send data.");
}
var task = socket.SendAsync(inData.ToArray(), SocketFlags.None);
yield return new WaitUntil(() => task.IsCompleted);
}

private async Task ProcessReceiveData()
private IEnumerator ProcessReceiveData()
{
if (!IsConnect())
return;
yield break;

byte[] receivedData = new byte[1024];
int bytesRead = await socket.ReceiveAsync(receivedData, SocketFlags.None);
if (bytesRead > 0)
var task = socket.ReceiveAsync(receivedData, SocketFlags.None);
yield return new WaitUntil(() => task.IsCompleted);

int bytesRead = task.Result;
if (task.Result > 0)
{
Console.WriteLine("ProcessReceiveData::Success, size=" + bytesRead);
List<byte> receivedDataList = new List<byte>(receivedData);
Expand Down
Loading