标签:
using UnityEngine; using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.Net; using System.Threading; using System.Net.Sockets; using System.IO; public class NetWorkManager : ManagerBase<NetWorkManager>, ManagerInterface { /// <summary>与服务器的唯一Socket</summary> public Socket client; /// <summary>接收线程</summary> private Thread receiveThread; /// <summary>接收数据列表</summary> private List<ReceiveParse> receiveList = new List<ReceiveParse>(); /// <summary>接受Buffer大小</summary> private const int BUFFER_SIZE = 16384; /// <summary>网络连接成功</summary> public const int EVENT_NETCONNECTED = 1; /// <summary>网络发送</summary> public const int EVENT_SEND = 2; /// <summary>网络接收</summary> public const int EVENT_RECEIVE = 3; public void Init() { DisConnect(); } /// <summary>连接Socket</summary> public void ConnectSocket(string host, int port) { if (client != null) { DisConnect(); } IPAddress ip = IPAddress.Parse(host); IPEndPoint ipe = new IPEndPoint(ip, port); client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { Security.PrefetchSocketPolicy(host, 843); client.Connect(ipe); } catch (Exception e) { Debug.Log(e); } if (client.Connected) { DoEvent(EVENT_NETCONNECTED); NetData.Connected = true; receiveThread = new Thread(new ThreadStart(ReceiveSocket)); receiveThread.IsBackground = true; receiveThread.Start(); } } /// <summary>断开Socket</summary> public void DisConnect() { if (receiveThread != null) { Debug.Log("receiveThread Abort!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); receiveThread.Abort(); receiveThread = null; } if (client != null) { client.Close(); client = null; } NetData.Connected = false; receiveList.Clear(); } void OnApplicationQuit() { DisConnect(); } /// <summary>Socket接收</summary> private void ReceiveSocket() { byte[] buf = new byte[BUFFER_SIZE * 2]; byte[] tmpBuf = new byte[BUFFER_SIZE]; long protocolIdx = -1; long protoclWaittingSize = 0; long bufStart = 0; long bufEnd = 0; while (true) { if (bufStart == bufEnd || ((protocolIdx == -1) && (bufEnd - bufStart) < 8)) { int b = client.Receive(tmpBuf, tmpBuf.Length, 0); if (b <= 0) { DisConnect(); continue; } if (protocolIdx == -1 && bufStart == bufEnd) { bufStart = 0; bufEnd = 0; } Buffer.BlockCopy(tmpBuf, 0, buf, (int)bufEnd, b); bufEnd += b; } if (protocolIdx == -1 && (bufEnd - bufStart) >= 8) { // short protocol = (short)((buf[bufStart + 2] << 8) | (buf[bufStart + 3])); protoclWaittingSize = (short)((buf[bufStart + 0] << 8) | (buf[bufStart + 1])); protocolIdx = bufStart; bufStart += 8; } if (protocolIdx >= 0) { bufStart += protoclWaittingSize; if (bufStart > bufEnd) { protoclWaittingSize = bufStart - bufEnd; bufStart = bufEnd; } else { Byte[] fullyProtocol = new byte[bufStart - protocolIdx + 32]; Buffer.BlockCopy(buf, (int)protocolIdx, fullyProtocol, 0, (int)(bufStart - protocolIdx)); try { Analyse(fullyProtocol); } catch (Exception e) { Debug.Log(e); } protocolIdx = -1; protoclWaittingSize = 0; } } } } /// <summary>发送Socket</summary> public void SendSocket(byte[] sp, bool sm) { if (sm) { DoEvent(EVENT_SEND); } client.Send(sp); } /// <summary>发送Http协议</summary> public void SendHttp(Dictionary<string, string> parameters) { WWWForm wf = new WWWForm(); if (!(parameters == null || parameters.Count == 0)) { foreach (string key in parameters.Keys) { wf.AddField(key, parameters[key]); } } WWW www = new WWW(NetData.loginHost, wf); StartCoroutine(ReceiveHttp(www)); } /// <summary>Socket接收</summary> private IEnumerator ReceiveHttp(WWW www) { yield return www; if (www.error == null) { } else { Debug.Log(www.error); } } /// <summary>包解析</summary> private void Analyse(byte[] recvBytes) { ReceiveParse rd = new ReceiveParse(recvBytes); receiveList.Add(rd); } void Update() { if (receiveList.Count != 0) { DoEvent(EVENT_RECEIVE); DoEvent(receiveList[0].protocolID); receiveList.RemoveAt(0); } } /// <summary>获取接受协议</summary> public ReceiveParse GetReceive() { return receiveList[0]; } /// <summary>获取接受协议号</summary> public short GetReceiveID() { return receiveList[0].protocolID; } }
using UnityEngine; using System.Collections; using System.Collections.Generic; using GameY; public class NetData { /// <summary>登陆服务器地址</summary> public static string loginHost = "http://192.168.1.102:8080/loginServer/l.d"; /// <summary>玩家数据</summary> public static PlayerData playerData; /// <summary>是否在线</summary> public static bool isOnline; /// <summary>连接上</summary> public static bool Connected; /// <summary>战斗结算资源列表</summary> public static Dictionary<ResType, int> getResList = new Dictionary<ResType, int>(); /// <summary>战斗结算道具列表</summary> public static List<ItemBase> getItemList = new List<ItemBase>(); /// <summary>游戏账号</summary> public static string username; /// <summary>清空</summary> public static void Clear() { playerData = null; getResList.Clear(); getItemList.Clear(); username = ""; } }
标签:
原文地址:http://www.cnblogs.com/jiangjieqim/p/4586583.html