public NetworkPacketSender(TcpChnl tcpChnl) { this.tcpChnl = tcpChnl; _tempSendBuffer = new Queue <byte[]>(); m_headerBytes = BitConverter.GetBytes(NetDefine.TCP_HEADER); if (BitConverter.IsLittleEndian) { Array.Reverse(m_headerBytes); } }
// 取包 推送给业务层处理 private bool HandleTcpPacket() { if (TcpChnl.State != EConnetState.EConneted) { return(false); } PacketTuple <IMessage> message = TcpChnl.TryGetRecvedMessage(); if (message == null) { return(false); } return(_broadcastHelp.HandleTcpPacket(message)); }
public void OnInit() { _broadcastHelp = new BroadcastHelp(); //可以写在对应频道 也可以 _netThread = new Thread(RunAsync); _netThread.Start(); nets = new Dictionary <ENetType, Ichnl> { { ENetType.UDP, UdpChnl = new UdpChnl() }, { ENetType.TCP, TcpChnl = new TcpChnl() } }; #if UNITY_EDITOR UnityEditor.EditorApplication.playModeStateChanged -= OnEditorPlayModeChanged; UnityEditor.EditorApplication.playModeStateChanged += OnEditorPlayModeChanged; #endif }
/// <summary> /// 发送消息 /// </summary> public void SendMessage(short id, IMessage message) { TcpChnl.SendMessage(id, message); }
/// <summary> /// 建立TCP连接 /// </summary> public void Connect(string ip, int port) { TcpChnl.Connect(ip, port); }
public void OnRelease() { _netThread?.Abort(); TcpChnl.Dispose(); }
/// <summary> /// 关闭一下 TCP连接 /// </summary> public void Close() { TcpChnl.DisConnect(); }