void OnPacketReceived(Packet packet) { LastUpdateTime = Environment.TickCount; var message = Message.FromPacket(packet); //Env.Logger.Log($"Message received from device{this} {message}"); if (message is ConversationMessage) { //connectedTimer?.ReStart(); //这部分代码适合放在AppModel里面。Device和Conversation并没有直接联系。Conversation可以不关心Device的状态。 var cm = message as ConversationMessage; SuperDriveCore.ProcessConversationMessage(this, cm); } else { if (message is DisconnectMessage) { var dm = message as DisconnectMessage; ExtraData = dm; State = DeviceState.Disconnected; } else if (message is RejectMessage) { if (IsRequester) { ExtraData = message; State = DeviceState.BeingRejected; } } else if (message is AcceptMessage) { if (IsRequester) { var am = message as AcceptMessage; SessionCode = am.SessionCode; if (Id == null) { //如果我是通过IP地址去主动连接别人的,那直到别人回复我这个消息,我没有对方的完整信息。这个分支在Release版里面应该不存在。 am.ExtractToDevice(this); SuperDriveCore.AddOrUpdateDevice(this); } State = DeviceState.Connected; } } else if (message is ChannelReadyMessage) { if (State == DeviceState.WaitSecureInitiatorConfirm) { State = DeviceState.Connected; } } } }
private void OnPacketReceived(Packet packet, IPEndPoint remote) { //排队处理收到的消息。如果不排队,一个消息会发回来很多次,多线程调试很麻烦。 var message = Message.FromPacket(packet); var msg = message as AbstractDeviceMessage; var peer = msg?.Device; if (peer == null || !peer.IsValid || peer.Id == SuperDriveCore.LocalDevice.Id) { return; } Env.Logger.Log(message.ToString(), nameof(LanDiscoverer)); //AppModel就是神... 没必要再把这些东西中转一下在AppModel里面处理。 //如果有精力,可以把发现和Channel的部分再整理一下。 if (msg is OfflineMessage) { var existingDevice = SuperDriveCore.Devices.FirstOrDefault(d => d.Id == peer.Id); existingDevice?.Offline(); return; } peer.DefaultIp = Util.AddressToString(remote.Address); peer.DiscoveryType = DiscoveryType.LAN; peer = SuperDriveCore.AddOrUpdateDevice(peer); if (peer == null) { return; } if (!peer.IsFirstSeen) { peer.Refresh(); } if (msg is OnlineMessage && SuperDriveCore.LocalDevice.State != DeviceState.OffLine) { peer.Acknowledge(); } }