1. 程式人生 > >脫坑之路-車站專案問題總彙

脫坑之路-車站專案問題總彙

using System.Collections;
using System.Collections.Generic;
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
using UnityEngine;

public class UdpState
{
	public UdpClient udpClient = null;
	public IPEndPoint ipEndPoint = null;
	public const int BufferSize = 1024;
	public byte[] buffer = new byte[BufferSize];
	public int counter = 0;
}

public class UdpManager
{
	public static bool messageSent = false;

	public static int listenPort = 13050;
	public static int remotePort = 15080;

	private IPEndPoint localEP = null;
	private IPEndPoint remoteEP = null;

	private UdpClient udpReceive = null;
	private UdpClient udpSend = null;
	private UdpState udpSendState = null;
	private UdpState udpReceiveState = null;
	Thread receiveThread = null;
	private int counter = 0;
	private static UdpManager udpManager;
	public byte[] msgBuffer =null;
	private bool sendDone = true;
	private bool receiveDone = true;
	private Device[] devices;

	public static UdpManager Instance
	{
		get{
			if(udpManager == null){	
				Debug.Log("udpManager == null");
				udpManager = new UdpManager();
			}
//			Debug.Log("udpManager == null----if wai");
			return udpManager;
		}
	}
	
	private UdpManager(){
		//
		DataManager.msgbyteQueue.Clear ();
//		Create ("127.0.0.1", remotePort);

	}
	public void Create(string ip, int port)
	{
		Close ();

		localEP = new IPEndPoint(IPAddress.Any, listenPort);

		remoteEP = new IPEndPoint(IPAddress.Parse(ip), port);

		udpReceive = new UdpClient(localEP);
		udpSend = new UdpClient();
		
		udpSendState = new UdpState();
		udpSendState.ipEndPoint = remoteEP;
		udpSendState.udpClient = udpSend;
		
		udpReceiveState = new UdpState();
		udpReceiveState.ipEndPoint = remoteEP;
		udpReceiveState.udpClient = udpReceive;
		receiveThread = new Thread(ReceiveMessages);
		receiveThread.Start();

	}
	public void Close(){
		receiveDone = true;//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
		if (udpReceive != null) {
			udpReceive.Close ();
		}
		if (udpSend != null) {
			udpSend.Close ();
		}
		if (receiveThread != null) {
			receiveThread.Abort ();
		}
	}

	public void SendMsg(byte[] bytes)
	{
		udpSend.Connect(remoteEP);



		udpSend.BeginSend(bytes, bytes.Length, new AsyncCallback(SendCallback), udpSendState);

	}
	

	public void SendCallback(IAsyncResult iar)
	{
		Debug.Log("SendCallback ----  ");
		UdpState udpState = iar.AsyncState as UdpState;
		if (iar.IsCompleted)
		{
		
			Console.WriteLine("number of bytes sent: {0}", udpState.udpClient.EndSend(iar));

		}
	}

	public void ReceiveMessages()
	{
		while (true)
		{
			if (receiveDone)
			{
			receiveDone = false;
			udpReceive.BeginReceive(new AsyncCallback(ReceiveCallback), udpReceiveState);
			//receiveDone.WaitOne();
			}
			Thread.Sleep(10);
		}
	}
	

	public void ReceiveCallback(IAsyncResult iar)
	{
		UdpState udpState = iar.AsyncState as UdpState;
		if (iar.IsCompleted)
		{
			Debug.LogError("收到訊息-1");
			Byte[] receiveBytes = udpState.udpClient.EndReceive(iar, ref udpReceiveState.ipEndPoint);
			Debug.LogError("收到訊息-2");			//string receiveString = Encoding.Unicode.GetString(receiveBytes);
			DataManager.msgbyteQueue.Enqueue(receiveBytes);

			Debug.LogError("收到訊息-3");
			receiveDone = true;
		}
	}


}

<span style="white-space:pre">		</span>UdpManager.Instance.Create (strServerIP, UdpManager.remotePort);
<span style="white-space:pre">		</span>UdpManager.Instance.SendMsg (TestDataManager.Make101Package ("127.0.0.1#"+UdpManager.listenPort));
<span style="white-space:pre">		</span>UdpManager.Instance.Create (strServerIP, UdpManager.remotePort);
<span style="white-space:pre">		</span>UdpManager.Instance.SendMsg (TestDataManager.Make101Package ("127.0.0.1#"+UdpManager.listenPort));
當第二次建立udp併發送時,返回資料收不到,卡在了<span style="font-family: Arial, Helvetica, sans-serif;">ReceiveCallback函式中的endreceive中,原來是少寫了感嘆號那條語句,導致執行緒並沒有立即終止。</span>