/// <summary> /// 线程循环 /// </summary> private void UF_ThreadUpdateLoop() { while (m_RunFlag) { try{ while (m_RunFlag) { Thread.Sleep(ThreadSleepRate); m_TickCount++; NetConnection connection = null; if (m_ListConnections.Count != 0) { //Select Mode lock (m_ListConnections){ for (int k = 0; k < m_ListConnections.Count; k++) { connection = m_ListConnections[k]; //轮询更新 connection.UF_Polling(); if (connection.connectState == NETCONNECT_STATE.CLOSED || connection.connectState == NETCONNECT_STATE.TIMEOUT) { connection.Dispose(); m_ListConnections.RemoveAt(k); k--; } else if (connection.connectState == NETCONNECT_STATE.OPENED) { //读取NetPacket bool _ReadOver = false; while (!_ReadOver && connection.UF_CheckCanReceive(ProtocalData.HEAD_SIZE)) { ProtocalData packet = ProtocalData.UF_Acquire(); if (connection.UF_Receive(packet)) { lock (m_ListProtocalDatas) { m_ListProtocalDatas.Add(packet); Debugger.UF_TrackNetProtol(packet.id, packet.size, packet.corCode, 1); } } else { packet.UF_Release(); _ReadOver = true; } } } } } } UF_UpdateHttpLoop(); } } catch (System.Exception e) { Debugger.UF_Exception(e); } } }
//发送协议数据包 public void UF_Send(ProtocalData packet) { if (packet != null) { //把数据包中的数据序列号到写入缓冲中 packet.UF_Write(this.m_WriteBuffer); } }
public static ProtocalData UF_Acquire() { ProtocalData ret = null; lock (s_DataPool) { ret = s_DataPool.Pop(); if (ret == null) { ret = new ProtocalData(); } } ret.isReleased = false; return(ret); }
//接收协议数据包 public bool UF_Receive(ProtocalData outData) { if (outData != null) { if (outData.UF_Read(this.m_ReadBuffer)) { ////过滤心跳包 //if (outData.id == HeatProtocalID) // return false; //else return(true); } } return(false); }
public NetConnection(string host, int port, ConnectType cType) { m_ReadBuffer = new CBytesBuffer(256); m_WriteBuffer = new CBytesBuffer(256); m_State = NETCONNECT_STATE.NONE; m_Host = host; m_Port = port; AddressFamily addressFamily = AddressFamily.InterNetwork; //转化IPV6地址 UF_ParseIPType(host, port, out host, out addressFamily); m_HostPort = string.Format("{0}:{1}", host, port); m_Socket = cType == ConnectType.TCP ? new TcpSocket(addressFamily) as ISocket : new UdpSocket(addressFamily) as ISocket; //心跳包协议号 m_ProtocalDataHeard = new ProtocalData(); m_ProtocalDataHeard.id = HeatProtocalID; }