public void Handle(NetClient netclient, GatewayCommand cmd) { var keys = cmd.Content.FromJson <string[]>(); var service = _gateway.GetServiceById(cmd.Header["ServiceId"]); if (service != null && service.Host == ((IPEndPoint)netclient.Socket.RemoteEndPoint).Address.ToString()) { List <string> failed = new List <string>(); foreach (var key in keys) { if (_lockKeyManager.TryLock(key, service, false) == false) { failed.Add(key); } } SystemEventCenter.OnMicroServiceUploadLockedKeyCompleted(service); netclient.WriteServiceData(new InvokeResult { Success = true, Data = failed.ToArray() }); } else { netclient.WriteServiceData(new InvokeResult { Success = false }); } }
public void Handle(NetClient netclient, GatewayCommand cmd) { netclient.WriteServiceData(new InvokeResult { Success = _gatewayRefereeClient.IsMaster, }); }
public void Handle(NetClient netclient, GatewayCommand cmd) { var filepath = cmd.Content; var root = _configuration.GetValue <string>("ShareFolder"); filepath = $"{root}/{filepath}"; _logger?.LogDebug("getting file:{0}", filepath); if (File.Exists(filepath)) { byte[] data = File.ReadAllBytes(filepath); netclient.WriteServiceData(new InvokeResult { Success = true, Data = data.Length }); netclient.Write(data); } else { netclient.WriteServiceData(new InvokeResult { Success = false, Error = "file not found" }); } }
public void HealthyCheck(NetClient netclient, GatewayCommand registerCmd) { this.NetClient = netclient; ServiceInfo = registerCmd.Content.FromJson <RegisterServiceInfo>(); ServiceInfo.Host = ((IPEndPoint)NetClient.Socket.RemoteEndPoint).Address.ToString(); if (string.IsNullOrEmpty(ServiceInfo.ServiceAddress)) { ServiceInfo.ServiceAddress = ServiceInfo.Host; } _gatewayReferee.AddMicroService(ServiceInfo); NetClient.WriteServiceData(new InvokeResult { Success = true }); lock (_Gateway.OnlineMicroServices) { _Gateway.OnlineMicroServices.Add(this); } SystemEventCenter.OnMicroServiceOnline(this.ServiceInfo); Task.Run(() => { _ServiceProviderAllocator.ServiceInfoChanged(_Gateway.GetAllServiceProviders()); }); _Logger?.LogInformation($"微服务{this.ServiceInfo.ServiceNames.ToJsonString()} {this.ServiceInfo.Host}:{this.ServiceInfo.Port}注册"); checkState(); }
public async void SendCommandAsync( GatewayCommand cmd, byte channel, byte level) { const string URL_FORMAT = "api.htm?cache={0}&ch={1}&cmd={2}&br={3}"; var url = string.Format(URL_FORMAT, DateTime.Now.Ticks, channel, (byte)cmd, level); await SendRequest(url); }
public void Handle(NetClient netclient, GatewayCommand cmd) { netclient.ReadTimeout = 30000; while (true) { netclient.WriteServiceData(new InvokeResult { Success = true }); netclient.ReadServiceObject <GatewayCommand>(); } }
public void Handle(NetClient netclient, GatewayCommand cmd) { if (cmd.Header != null) { if (cmd.Header.ContainsKey("TranId") == false || string.IsNullOrEmpty(cmd.Header["TranId"])) { cmd.Header["TranId"] = _TransactionIdBuilder.Build(); } } var requestBody = cmd.Content.FromJson <GetServiceProviderRequest>(); requestBody.Header = cmd.Header; requestBody.ClientAddress = ((IPEndPoint)netclient.Socket.RemoteEndPoint).Address.ToString(); try { if (_gatewayRefereeClient.IsMaster == false) { outputResult(netclient, cmd, new RegisterServiceLocation { Host = "not master", Port = 0 }); } else { var location = _serviceProvider.GetService <IServiceProviderAllocator>().Alloc(requestBody); if (location == null) { outputResult(netclient, cmd, new RegisterServiceLocation { Host = "", Port = 0, TransactionId = cmd.Header != null ? cmd.Header["TranId"] : null }); return; } if (cmd.Header != null) { location.TransactionId = cmd.Header["TranId"]; } outputResult(netclient, cmd, location); } } catch { outputResult(netclient, cmd, new RegisterServiceLocation { Host = "", Port = 0, TransactionId = cmd.Header != null ? cmd.Header["TranId"] : null }); } }
public void Handle(NetClient netclient, GatewayCommand cmd) { var locations = this.List(cmd.Content); if (cmd.IsHttp) { var contentBytes = Encoding.UTF8.GetBytes(locations.ToJsonString()); netclient.OutputHttpContent(contentBytes); } else { netclient.WriteServiceData(locations); } }
public void Handle(NetClient netclient, GatewayCommand cmd) { var info = cmd.Content.FromJson <RegisterServiceInfo>(); var service = _gateway.GetServiceById(info.ServiceId); if (service != null) { service.ServiceNames = info.ServiceNames; } netclient.WriteServiceData(new InvokeResult { Success = true }); }
protected virtual void OnSendCommand(GatewayCommand command, byte channel, byte brightness = 0) { var handler = SendCommand; if (handler != null) { var args = new SendCommandEventArgs { command = command, channel = channel, brightness = brightness }; handler(this, args); } }
public void Handle(NetClient netclient, GatewayCommand cmd) { string[] allowips = _configuration.GetSection("AllowIps").Get <string[]>(); if (allowips != null && allowips.Length > 0 && allowips.Contains(((IPEndPoint)netclient.Socket.RemoteEndPoint).Address.ToString()) == false) { //不允许的ip netclient.WriteServiceData(new InvokeResult { Success = false }); return; } var serviceClient = _serviceProvider.GetService <IMicroServiceReception>(); serviceClient.HealthyCheck(netclient, cmd); }
void outputResult(NetClient netclient, GatewayCommand cmd, RegisterServiceLocation location) { if (cmd.IsHttp) { if (location.Host.Length == 0) { var contentBytes = Encoding.UTF8.GetBytes("{}"); netclient.OutputHttpContent(contentBytes); } else { var contentBytes = Encoding.UTF8.GetBytes(location.ToJsonString()); netclient.OutputHttpContent(contentBytes); } } else { netclient.WriteServiceData(location); } }
public void Handle(NetClient netclient, GatewayCommand cmd) { var info = cmd.Content.FromJson <RegisterServiceInfo>(); var service = _gateway.GetServiceById(info.ServiceId); if (service != null) { service.ServiceNames = info.ServiceNames; service.Description = info.Description; service.ClientCheckCode = info.ClientCheckCode; Task.Run(() => { _ServiceProviderAllocator.ServiceInfoChanged(_gateway.GetAllServiceProviders()); }); } netclient.WriteServiceData(new InvokeResult { Success = true }); }
public void Handle(NetClient netclient, GatewayCommand cmd) { var location = cmd.Content.FromJson <RegisterServiceLocation>(); if (((IPEndPoint)netclient.Socket.RemoteEndPoint).Address.ToString() != _referee.MasterIp.Address) { netclient.WriteServiceData(new InvokeResult { Success = false }); return; } _logger?.LogInformation($"{location.Host}:{location.Port} 服务断开"); _referee.MasterGatewayServices.TryRemove($"{location.Host}:{location.Port}", out RegisterServiceLocation original); netclient.WriteServiceData(new InvokeResult { Success = true }); }
/// <summary> /// 发送命令 /// </summary> /// <returns></returns> private void SendMessage(GatewayCommand cmd) { using (MemoryStream ms = new MemoryStream()) { using (BinaryStreamWriter sw = new BinaryStreamWriter(ms)) { sw.Write(GatewayProtocolHeader.Gateway_STX); sw.Write((short)0); sw.Write((ushort)cmd); sw.Write((byte)ClientPacketType.CLIENTPACKET_ALL);//客户端类型 // // byte[] message = ms.ToArray(); // message[1] = (byte)message.Length; message[2] = (byte)(message.Length << 8); // listener.SendToServer(message); } } }
public void Handle(NetClient netclient, GatewayCommand cmd) { bool success; var ip = ((IPEndPoint)netclient.Socket.RemoteEndPoint).Address.ToString(); var port = Convert.ToInt32(cmd.Content); lock (_referee) { if (_referee.MasterIp == null || _referee.MasterIp.Equals(ip, port)) { _referee.MasterIp = new NetAddress { Address = ip, Port = port }; success = true; } else { success = false; } } var data = success ? _referee.MasterGatewayServices.ToJsonString() : _referee.MasterIp.ToJsonString(); if (success) { _referee.MasterGatewayServices.Clear(); } netclient.WriteServiceData(new InvokeResult { Success = success, Data = data }); if (success) { _serviceProvider.GetService <MasterGatewayConnector>().Start(netclient); } }
public void Handle(NetClient netclient, GatewayCommand cmd) { var arr = cmd.Content.FromJson <RegisterServiceLocation[]>(); if (((IPEndPoint)netclient.Socket.RemoteEndPoint).Address.ToString() != _referee.MasterIp.Address) { netclient.WriteServiceData(new InvokeResult { Success = false }); return; } foreach (var location in arr) { _referee.MasterGatewayServices[$"{location.Host}:{location.Port}"] = location; } netclient.WriteServiceData(new InvokeResult { Success = true }); }
public void Handle(NetClient netclient, GatewayCommand cmd) { var serviceid = cmd.Content; for (int i = 0; i < _gateway.OnlineMicroServices.Count; i++) { try { if (_gateway.OnlineMicroServices[i].ServiceInfo.ServiceId == serviceid) { _gateway.OnlineMicroServices[i].Close(); break; } } catch { } } netclient.WriteServiceData(new InvokeResult { Success = true }); }
/// <summary> /// 创建服务器网关消息 /// </summary> /// <param name="links">服务器的连接编号</param> /// <param name="message">转发的数据</param> /// <returns></returns> public static byte[] CreateServerMessage(byte clienttype, long linkno, string ipaddress, GatewayCommand command, byte[] message) { using (MemoryStream ms = new MemoryStream(8096)) { using (BinaryStreamWriter sw = new BinaryStreamWriter(ms)) { sw.Write(GatewayProtocolHeader.Gateway_STX); sw.Write((short)0); sw.Write((ushort)command); sw.Write(clienttype); sw.Write(linkno); //发送客户端IP sw.Write(ipaddress); sw.Write(message); // byte[] buffer = ms.ToArray(); buffer[1] = (byte)buffer.Length; buffer[2] = (byte)(buffer.Length >> 8); // return(buffer); } } }
public void Handle(NetClient netclient, GatewayCommand cmd) { var info = cmd.Content.FromJson <LockKeyInfo>(); if (info.IsUnlock && info.MicroServiceId == "$$$") { //强制释放 _lockKeyManager.UnLock(info.Key, null); netclient.WriteServiceData(new InvokeResult { Success = true }); return; } var service = _gateway.GetServiceById(info.MicroServiceId); if (service == null) { netclient.WriteServiceData(new InvokeResult { Success = false, Data = $"找不到注册的服务{info.MicroServiceId}" }); return; } try { if (info.IsUnlock) { netclient.WriteServiceData(new InvokeResult { Success = _lockKeyManager.UnLock(info.Key, service) }); return; } if (_lockKeyManager.TryLock(info.Key, service)) { netclient.WriteServiceData(new InvokeResult { Success = true, Data = _lockKeyManager.KeyTimeout }); } else { netclient.WriteServiceData(new InvokeResult { Success = false }); } } catch (Exception ex) { netclient.WriteServiceData(new InvokeResult { Success = false, Data = ex.Message }); } }
/// <summary> /// 根据请求分配处理者 /// </summary> /// <param name="cmd"></param> /// <returns></returns> public ICommandHandler AllocHandler(GatewayCommand cmd) { return(_cache[cmd.Type]); }
public void Handle(NetClient netclient, GatewayCommand cmd) { var locations = this.List(cmd.Content); netclient.WriteServiceData(locations); }
public void Handle(NetClient netclient, GatewayCommand cmd) { _serviceProvider.GetService <ListenFileChangeReception>().Handle(netclient, cmd); }
public void Handle(NetClient netclient, GatewayCommand cmd) { netclient.WriteServiceData(new InvokeResult { Success = true }); }
public void Handle(NetClient client, GatewayCommand cmd) { try { _listeningFiles = cmd.Content.FromJson <string[]>(); while (true) { if (_changedFiles.Count == 0) { client.WriteServiceData(new InvokeResult { Success = true }); client.ReadServiceObject <InvokeResult>(); } else { string[] sendFiles = null; lock (_changedFiles) { sendFiles = _changedFiles.ToArray(); _changedFiles.Clear(); } foreach (var file in sendFiles) { string fullpath = $"{_root}/{file}"; if (File.Exists(fullpath)) { byte[] data = null; try { data = File.ReadAllBytes(fullpath); } catch (Exception ex) { _logger?.LogError(ex, ex.Message); continue; } client.WriteServiceData(new InvokeResult { Success = true, Data = file }); client.Write(data.Length); client.Write(data); client.ReadServiceObject <InvokeResult>(); } } } _waitObj.WaitOne(38000); } } catch (Exception ex) { throw; } finally { SystemEventCenter.ShareFileChanged -= SystemEventCenter_ShareFileChanged; } }