//Mutex appModelMutex; private SuperDriveCore() { //container.Logger.Error("Test Log"); var cfg = Env.Config; _roDevices = new ReadOnlyObservableCollection <Device>(DevicesInternal); _conversations = new Dictionary <string, Conversation>(); _localDevice = cfg.LocalDevice; _localDevice.ConnectCode = null; _localDevice.DeviceType = Env.DeviceType; _localDevice.IpAddress = GetIpAddressListString(); _channelManager = new ChannelManager(Consts.ChannelPort); _instance = this; _channelManager.ChannelCreated += channel => { Action <Packet> packetReceiver = null; packetReceiver = packet => { Message message = Message.FromPacket(packet); ConnectMessage cm = message as ConnectMessage; //一个新连接的socket,在发送ConnectMessage之前发送其它Message,是不会理会的。 if (cm != null) { channel.PacketReceived -= packetReceiver; var device = AddOrUpdateDevice(cm.Device); if (device != null) { if (PacketHeader.DefaultVersion == packet.Header.version) { device.OnConnectMessageReceived(channel, cm); } else { device.CallPacketProtocolVersionError(PacketHeader.DefaultVersion > packet.Header.version); } } } }; channel.PacketReceived += packetReceiver; }; _discoverer = new LanDiscoverer(LocalDevice, Consts.DiscoverPort); _httpServer = new HttpListener(IPAddress.Any, Consts.HttpPort); _httpServer.Start(); //SuperDriveCore是集大成者,作用就是承上启下。它无所不知。所以,既能访问上层,又能访问下层。 _httpServer.Request += HttpRequestDispatcher.Dispatch; Env.Network.NetworkChanged += ips => { if (!string.IsNullOrEmpty(ips)) { LocalDevice.IpAddress = GetIpAddressListString(); _discoverer.StartBroadcast(); } }; }
public void StartImpl() { Util.Check(LocalDevice != null, "Local Device Must be set before start"); if (_isRunning) { return; } _isRunning = true; _discoverer.StartListen(); _discoverer.StartBroadcast(); _channelManager.Start(); Env.Logger.Log("SuperDriveCore Started", nameof(SuperDriveCore)); }