/// <summary> /// 连接客户端 /// </summary> /// <param name="ar"></param> private void HandleTcpClientAccepted(IAsyncResult ar) { try { if (IsRunning) { TcpListener tcpListener = (TcpListener)ar.AsyncState; TcpClient tcpClient = tcpListener.EndAcceptTcpClient(ar); byte[] buffer = new byte[4096]; RfClientTcp client = new RfClientTcp(tcpClient, buffer); DoAddClient(client); NetworkStream networkStream = client.NetworkStream; networkStream.BeginRead(client.Buffer, 0, client.Buffer.Length, HandleDatagramReceived, client); tcpListener.BeginAcceptTcpClient(new AsyncCallback(HandleTcpClientAccepted), ar.AsyncState); } } catch (Exception e) { _mLog.Error(true, e.Message, e); } }
private void DoAddClient(RfClientTcp client) { lock (clients) { clients.Add(client); SendMsg(RfConnectE.客户端连接, client.IP_PORT, client.MEID, null); _mLog.Status(true, "客户端连接:" + client.MEID); } }
/// <summary> /// 移除断开的客户端 /// </summary> /// <param name="client"></param> internal void DoRemoveClient(RfClientTcp client) { if (client == null) { return; } lock (clients) { client.Close(); clients.Remove(client); SendMsg(RfConnectE.客户端断开, client.IP_PORT, client.MEID, null); _mLog.Status(true, "客户端断开:" + client.MEID); } }
internal override void NoticeDataReceive(byte[] data, RfClientTcp client) { try { string dstr = Encoding.UTF8.GetString(data); RfPackage package = JsonTool.Deserialize <RfPackage>(dstr); if (!client.IsUpdateMEID) { client.SetMEID(package.Meid); } package.ClientId = client.MEID; SendMsg(RfConnectE.客户端接收信息, client.IP_PORT, client.MEID, package); _mLog.Status(true, string.Format("[Send]-Client:{0}\n" + "Msg:{1}", client.MEID, dstr)); }catch (Exception e) { _mLog.Error(true, e.StackTrace); } }
/// <summary> /// 发送报文至指定的客户端 /// </summary> /// <param name="tcpClient">客户端</param> /// <param name="datagram">报文</param> private void Send(RfClientTcp tcpClient, byte[] datagram) { try { if (!IsRunning) { throw new InvalidProgramException("This TCP server has not been started."); } if (tcpClient == null) { throw new ArgumentNullException("tcpClient"); } if (tcpClient.TcpClient == null) { throw new ArgumentNullException("tcpClient.TcpClient"); } if (datagram == null) { throw new ArgumentNullException("datagram"); } if (tcpClient.TcpClient.Connected) { tcpClient.TcpClient.GetStream().BeginWrite( datagram, 0, datagram.Length, HandleDatagramWritten, tcpClient); } else { //信息不准确,重新连接 DoRemoveClient(tcpClient); } } catch (Exception e) { _mLog.Error(true, e.Message, e); //信息不准确,重新连接 DoRemoveClient(tcpClient); } }
internal abstract void NoticeDataReceive(byte[] data, RfClientTcp client);
/// <summary> /// 处理接收数据 /// </summary> /// <param name="ar"></param> private void HandleDatagramReceived(IAsyncResult ar) { try { if (IsRunning) { RfClientTcp client = (RfClientTcp)ar.AsyncState; NetworkStream networkStream = client.NetworkStream; int numberOfReadBytes = 0; try { numberOfReadBytes = networkStream.EndRead(ar); } catch { numberOfReadBytes = 0; } if (numberOfReadBytes == 0) { // connection has been closed DoRemoveClient(client); return; } // received byte and trigger event notification byte[] readData = new byte[numberOfReadBytes]; Buffer.BlockCopy(client.Buffer, 0, readData, 0, numberOfReadBytes); uint headerKey = BitConverter.ToUInt16(ShiftBytes(readData, 0, 2), 0); if (headerKey != P_HEAD) //43707 { //信息不准确,重新连接 DoRemoveClient(client); return; } if (readData.Length > 8) { uint messageSize = BitConverter.ToUInt32(ShiftBytes(readData, 2, 4), 0); if (readData.Count() >= messageSize) { //2 + 4 + msgsize + 2 int tailKeyIndex = (int)messageSize - 2; uint tailKey = BitConverter.ToUInt16(ShiftBytes(readData, tailKeyIndex, 2), 0); if (tailKey == P_TAIL)//52445 { byte[] data = new byte[messageSize - 8]; Array.Copy(readData, 6, data, 0, messageSize - 8); NoticeDataReceive(data, client); int skiplen = tailKeyIndex + 4; // remove from data array readData = readData.Skip(skiplen).ToArray(); } else { //信息不准确,重新连接 DoRemoveClient(client); return; } } } try { if (readData.Any()) { client.AddLeft(readData); networkStream.BeginRead(client.Buffer, readData.Length, client.Buffer.Length - readData.Length, HandleDatagramReceived, client); } else { networkStream.BeginRead(client.Buffer, 0, client.Buffer.Length, HandleDatagramReceived, client); } } catch (Exception e) { _mLog.Error(true, e.Message, e); //信息不准确,重新连接 DoRemoveClient(client); return; } } } catch (Exception e) { _mLog.Error(true, e.Message, e); } }