/// <summary> /// 接收到消息 /// </summary> /// <param name="ms"></param> void OnReceivedMessage(byte[] message) { ByteBuffer buffer = new ByteBuffer(message); //放在lua中解析pbid int pbId = buffer.ReadShort(); if (pbId == 10000) { if (ConnectObserver.inReconnect) { ConnectObserver.DispatchEvent(Protocal.Reconnected, new ByteBuffer()); if (reconnectAction != null) { reconnectAction(); } } heartBeatService.resetTimeout(); heartBeatService.pingUpdate(); } else { buffer.SeekToBegin(); ConnectObserver.DispatchEvent(Protocal.Message, buffer); heartBeatService.resetTimeout(); } }
internal void pingUpdate() { int protocal = Protocal.PingUpdate; ByteBuffer buffer = new ByteBuffer(); buffer.WriteShort((ushort)protocal); ConnectObserver.DispatchEvent(protocal, buffer); }
public void UpdateHeartBeat2Server(float deltaTime) { if (heartBeatService != null && client != null && client.Connected) { if (heartBeatService.updateHeartBeat2Server(deltaTime)) { ConnectObserver.DispatchEvent(Protocal.HeartBeat2Server, new ByteBuffer()); } } }
/// <summary> /// 主动从服务器断开连接 /// </summary> public void DisconnectFromServer(int protocal) { StopHeartBeat(); Close(); if (protocal == 0) { protocal = Protocal.KickOut; } ByteBuffer buffer = new ByteBuffer(); buffer.WriteShort((ushort)protocal); ConnectObserver.DispatchEvent(protocal, buffer); }
/// <summary> /// 连接上服务器 /// </summary> void OnConnect(IAsyncResult asr) { try { outStream = client.GetStream(); client.GetStream().BeginRead(byteBuffer, 0, MAX_READ, new AsyncCallback(OnRead), null); if (!ConnectObserver.inReconnect) { ConnectObserver.DispatchEvent(Protocal.Connect, new ByteBuffer()); } } catch (Exception e) { Console.WriteLine(e.Message); OnDisconnected(DisType.Exception, e.Message); } }
/// <summary> /// 丢失链接 /// </summary> void OnDisconnected(DisType dis, string msg) { if (client != null) { Close(); //关掉客户端链接 int protocal = dis == DisType.Exception ? Protocal.Exception : Protocal.Disconnect; ByteBuffer buffer = new ByteBuffer(); buffer.WriteShort((ushort)protocal); ConnectObserver.DispatchEvent(protocal, buffer); if (ConnectObserver.inReconnect) { ConnectObserver.inReconnect = false; } ConsoleView.LogWarning(string.Format("Connection was closed:>{0} Distype:>{1}", msg, dis)); } }