/// <summary> /// 连接 /// </summary> /// <param name="ip"></param> /// <param name="port"></param> public void Connect(string ip, int port) { //// 暂时先这样,以后修改为在外部处理 2015年12月23日 16:25:48 //Close(); mSession = mSocket.Connect(ip, port); mSession.SetHandleReceive(Receive); mSocket.ReceiveCompleted = OnReceive; mSession.mNetType = Net.ENet.UdpClient; if (Connected != null) Connected(Net.ENet.UdpClient, mSession); }
/// <summary> /// 初始化 /// </summary> /// <param name="AdapterID">唯一值</param> /// <param name="net">网络模型</param> /// <param name="param">参数值</param> public override void Init(long lAdapterID, ENet net, object param) { base.Init(lAdapterID, net, param); mSession = param as UDPSession; }
/// <summary> /// 接收到数据 /// </summary> /// <param name="udpSession"></param> /// <param name="byData"></param> protected void OnReceive(IPEndPoint ipEndPoint, byte[] byData) { UDPSession udpSession; if (HasSession(ipEndPoint)) { long lSessionID = GetSessionID(ipEndPoint); udpSession = GetSession(lSessionID); } else { udpSession = new UDPSession(mSocket, ipEndPoint); udpSession.SetHandleReceive(Receive); InsertSession(udpSession); } // 接收处理 udpSession.Receive(byData); }
protected void RemoveSession(UDPSession session) { lock (mSessionLock) { mSessionSet.Remove(session.GetIPEndPoint()); if (session != null) mSessionDict.Remove(session.SessionID); } }
/// <summary> /// 插入Session /// </summary> /// <param name="session"></param> protected void InsertSession(UDPSession session) { lock (mSessionLock) { if (mSessionSet.ContainsKey(session.GetIPEndPoint())) return; session.SessionID = NetSessionID(); mSessionSet.Add(session.GetIPEndPoint(), session.SessionID); mSessionDict.Add(session.SessionID, session); session.mNetType = Net.ENet.UdpServer; if (Connected != null) Connected(Net.ENet.UdpServer, session); } }