public void ClientConnected(TcpClientInfo clientInfo, WaitingForVerificationClients waitingForVerificationClients) { log($"{clientInfo.OriginalRemotePoint} (System) Connected", Log.LogLevel.Success); ClientInfo = clientInfo; waitingForVerificationClients.ClientConnected(clientInfo); }
public void HeartBeat(TcpClientInfo clientInfo) { if (ClientInfo == clientInfo) { ClientInfo.HeartAlive = 0; } }
public async Task Initialize() { YummyOnlineManager manager = new YummyOnlineManager(); waitingForVerificationClients = new WaitingForVerificationClients(log, send); systemClient = new SystemClient(log, send, GetTcpServerStatus); newDineInformClients = new NewDineInformClients(log, send, await manager.GetGuids()); printerClients = new PrinterClients(log, send, await manager.GetHotels()); log($"Binding {ip}:{port}", Log.LogLevel.Info); tcp.StartListening(IPAddress.Parse(ip), port, client => { TcpClientInfo clientInfo = new TcpClientInfo(client); waitingForVerificationClients.Add(clientInfo); log($"{clientInfo.OriginalRemotePoint} Connected, Waiting for verification", Log.LogLevel.Info); }); System.Timers.Timer timer = new System.Timers.Timer(10 * 1000); timer.Elapsed += (e, o) => { // 30秒之内已连接但是未发送身份信息的socket断开 waitingForVerificationClients.HandleTimeOut(); //60秒之内没有接收到心跳包的socket断开, 或发送心跳包失败的socket断开 systemClient.HandleTimeOut(); newDineInformClients.HandleTimeOut(); printerClients.HandleTimeOut(); }; timer.Start(); }
/// <summary> /// 请求打印交接班 /// </summary> public void RequestPrintShifts(TcpClientInfo clientInfo, RequestPrintShiftsProtocol protocol, NewDineInformClientGuid sender) { lock (this) { if (sender == null) { log($"{clientInfo.OriginalRemotePoint} Received RequestPrintShifts From Invalid NewDineInformClient", Log.LogLevel.Error); clientInfo.Close(); return; } protocol.Ids = protocol.Ids ?? new List <int>(); StringBuilder idStr = new StringBuilder(); foreach (int id in protocol.Ids) { idStr.Append($"{id} "); } log($"{clientInfo.OriginalRemotePoint} (RequestPrintShifts): From: {sender.Description}, HotelId: {protocol.HotelId}, Ids: {idStr}, DateTime: {protocol.DateTime.ToString("yyyy-MM-dd")}", Log.LogLevel.Success); PrintShiftsProtocol p = new PrintShiftsProtocol(protocol.Ids, protocol.DateTime); if (Clients[protocol.HotelId] == null) { WaitedQueue[protocol.HotelId].Enqueue(p); log($"Printer of Hotel {protocol.HotelId} is not connected", Log.LogLevel.Error); return; } sendPrintShiftsProtocol(protocol.HotelId, p); } }
public void NewDineInform(TcpClientInfo clientInfo, NewDineInformProtocol protocol) { lock (this) { NewDineInformClientGuid sender = GetSender(clientInfo); if (sender == null) { log($"{clientInfo.OriginalRemotePoint} Received NewDineInform From Invalid NewDineInformClient", Log.LogLevel.Error); clientInfo.Close(); return; } log($"{clientInfo.OriginalRemotePoint} (NewDineInform): From: {sender.Description}, HotelId: {protocol.HotelId}, DineId: {protocol.DineId}, IsPaid: {protocol.IsPaid}", Log.LogLevel.Success); foreach (var p in Clients) { // 不向未连接的客户端与发送方客户端 发送新订单通知信息 if (p.Value == null || p.Value.Client == clientInfo.Client) { continue; } send(p.Value.Client, protocol); } } }
public void ClientConnected(TcpClientInfo clientInfo) { lock (this) { TcpClientInfo waitedClientInfo = Clients.FirstOrDefault(p => p == clientInfo); clientInfo.ConnectedTime = waitedClientInfo.ConnectedTime; Clients.Remove(waitedClientInfo); } }
public override void HandleError(TcpClient client, Exception e) { if (ClientInfo?.Client == client) { ClientInfo = null; log($"System {ClientInfo.OriginalRemotePoint} Disconnected", Log.LogLevel.Error); return; } }
public NewDineInformClientGuid GetSender(TcpClientInfo clientInfo) { lock (this) { return(Clients .Where(p => p.Value?.Client == clientInfo.Client) .Select(p => p.Key) .FirstOrDefault()); } }
private void Tcp_MessageReceivedEvent(TcpClient client, string content) { try { BaseTcpProtocol baseProtocol = JsonConvert.DeserializeObject <BaseTcpProtocol>(content); TcpClientInfo clientInfo = new TcpClientInfo(client); switch (baseProtocol.Type) { case TcpProtocolType.HeartBeat: systemClient.HeartBeat(clientInfo); newDineInformClients.HeartBeat(clientInfo); printerClients.HeartBeat(clientInfo); break; case TcpProtocolType.SystemConnect: systemClient.ClientConnected(clientInfo, waitingForVerificationClients); break; case TcpProtocolType.SystemCommand: systemClient.SystemCommand(clientInfo, JsonConvert.DeserializeObject <SystemCommandProtocol>(content), newDineInformClients); break; case TcpProtocolType.NewDineInformClientConnect: newDineInformClients.ClientConnected(clientInfo, JsonConvert.DeserializeObject <NewDineInformClientConnectProtocol>(content), waitingForVerificationClients); break; case TcpProtocolType.NewDineInform: newDineInformClients.NewDineInform(clientInfo, JsonConvert.DeserializeObject <NewDineInformProtocol>(content)); break; case TcpProtocolType.PrintDineClientConnect: printerClients.ClientConnected(clientInfo, JsonConvert.DeserializeObject <PrintDineClientConnectProtocol>(content), waitingForVerificationClients); break; case TcpProtocolType.RequestPrintDine: printerClients.RequestPrintDine(clientInfo, JsonConvert.DeserializeObject <RequestPrintDineProtocol>(content), newDineInformClients.GetSender(clientInfo)); break; case TcpProtocolType.RequestPrintShifts: printerClients.RequestPrintShifts(clientInfo, JsonConvert.DeserializeObject <RequestPrintShiftsProtocol>(content), newDineInformClients.GetSender(clientInfo)); break; } } catch (Exception e) { log($"{client.Client.RemoteEndPoint} Receive Error: {e.Message}, Data: {content}", Log.LogLevel.Error); client.Close(); } }
public override void HandleError(TcpClient client, Exception e) { lock (this) { TcpClientInfo clientInfo = Clients.FirstOrDefault(p => p.Client == client); if (clientInfo != null) { Clients.Remove(clientInfo); log($"WaitingForVerificationClient {clientInfo.OriginalRemotePoint} Disconnected", Log.LogLevel.Error); return; } } }
public void HeartBeat(TcpClientInfo clientInfo) { lock (this) { foreach (var pair in Clients) { if (pair.Value == clientInfo) { pair.Value.HeartAlive = 0; return; } } } }
public void ClientConnected(TcpClientInfo clientInfo, PrintDineClientConnectProtocol protocol, WaitingForVerificationClients waitingForVerificationClients) { lock (this) { log($"{clientInfo.OriginalRemotePoint} (Printer): HotelId: {protocol.HotelId} Request Connection", Log.LogLevel.Info); if (!Clients.ContainsKey(protocol.HotelId)) { log($"{clientInfo.OriginalRemotePoint} Printer HotelId {protocol.HotelId} Not Matched", Log.LogLevel.Warning); clientInfo.Close(); return; } KeyValuePair <int, TcpClientInfo> pair = Clients.FirstOrDefault(p => p.Key == protocol.HotelId); if (pair.Value != null) { log($"Printer HotelId {pair.Key} Repeated", Log.LogLevel.Warning); pair.Value.ReadyToReplaceClient = clientInfo; pair.Value.Close(); } else { Clients[pair.Key] = clientInfo; } log($"{clientInfo.OriginalRemotePoint} (Printer of Hotel {protocol.HotelId}) Connected", Log.LogLevel.Success); // 打印存储在打印等待队列中的所有请求 while (WaitedQueue[pair.Key].Count > 0) { BaseTcpProtocol printProtocol = WaitedQueue[pair.Key].Dequeue(); if (printProtocol.Type == TcpProtocolType.PrintDine) { sendPrintDineProtocol(pair.Key, (PrintDineProtocol)printProtocol); } else if (printProtocol.Type == TcpProtocolType.PrintShifts) { sendPrintShiftsProtocol(pair.Key, (PrintShiftsProtocol)printProtocol); } log($"Send Waited Dine of Hotel {pair.Key}", Log.LogLevel.Success); } waitingForVerificationClients.ClientConnected(clientInfo); } }
private ClientStatus getClientStatus(TcpClientInfo info) { ClientStatus status = new ClientStatus(); if (info != null) { status.IsConnected = true; status.ConnectedTime = info.ConnectedTime; status.IpAddress = info.OriginalRemotePoint.Address.ToString(); status.Port = info.OriginalRemotePoint.Port; status.HeartAlive = info.HeartAlive; } else { status.IsConnected = false; } return(status); }
/// <summary> /// 请求打印订单 /// </summary> public void RequestPrintDine(TcpClientInfo clientInfo, RequestPrintDineProtocol protocol, NewDineInformClientGuid sender) { lock (this) { if (sender == null) { log($"{clientInfo.OriginalRemotePoint} Received RequestPrintDine From Invalid NewDineInformClient", Log.LogLevel.Error); clientInfo.Close(); return; } protocol.DineMenuIds = protocol.DineMenuIds ?? new List <int>(); protocol.PrintTypes = protocol.PrintTypes ?? new List <PrintType>(); StringBuilder dineMenuStr = new StringBuilder(); for (int i = 0; i < protocol.DineMenuIds.Count; i++) { dineMenuStr.Append(protocol.DineMenuIds[i]); if (i != protocol.DineMenuIds.Count - 1) { dineMenuStr.Append(' '); } } StringBuilder typeStr = new StringBuilder(); foreach (var type in protocol.PrintTypes) { typeStr.Append($"{type.ToString()} "); } log($"{clientInfo.OriginalRemotePoint} (RequestPrintDine): From: {sender.Description}, HotelId: {protocol.HotelId}, DineId: {protocol.DineId}, DineMenuIds: {dineMenuStr}, PrintTypes: {typeStr}", Log.LogLevel.Success); PrintDineProtocol p = new PrintDineProtocol(protocol.DineId, protocol.DineMenuIds, protocol.PrintTypes); if (Clients[protocol.HotelId] == null) { WaitedQueue[protocol.HotelId].Enqueue(p); log($"Printer of Hotel {protocol.HotelId} is not connected", Log.LogLevel.Error); return; } sendPrintDineProtocol(protocol.HotelId, p); } }
public void SystemCommand(TcpClientInfo clientInfo, SystemCommandProtocol protocol, NewDineInformClients newDineInformClients) { if (ClientInfo != clientInfo) { log($"{clientInfo.OriginalRemotePoint} Received SystemCommand From Invalid SystemClient", Log.LogLevel.Error); clientInfo.Close(); return; } switch (protocol.CommandType) { case SystemCommandType.RefreshNewDineClients: log($"{clientInfo.OriginalRemotePoint} Refresh NewDineInformClients", Log.LogLevel.Success); var _ = refreshNewDineInformClients(newDineInformClients); break; case SystemCommandType.RequestTcpServerStatus: log($"{clientInfo.OriginalRemotePoint} Request TcpServerStatus", Log.LogLevel.Success); requestTcpServerStatus(); break; } }
public void ClientConnected(TcpClientInfo clientInfo, NewDineInformClientConnectProtocol protocol, WaitingForVerificationClients waitingForVerificationClients) { lock (this) { log($"{clientInfo.OriginalRemotePoint} (NewDineInformClient): Guid: {protocol.Guid} Request Connection", Log.LogLevel.Info); if (protocol.Guid == new Guid()) { log($"{clientInfo.OriginalRemotePoint} NewDineInformClient Lack Guid", Log.LogLevel.Warning); clientInfo.Close(); return; } KeyValuePair <NewDineInformClientGuid, TcpClientInfo> pair = Clients.FirstOrDefault(p => p.Key.Guid == protocol.Guid); if (pair.Key == null) { log($"{clientInfo.OriginalRemotePoint} NewDineInformClient Guid {protocol.Guid} Not Matched", Log.LogLevel.Warning); clientInfo.Close(); return; } else { if (pair.Value != null) { log($"NewDineInformClient Guid {pair.Key.Guid} Repeated", Log.LogLevel.Warning); pair.Value.ReadyToReplaceClient = clientInfo; pair.Value.Close(); } else { Clients[pair.Key] = clientInfo; } log($"{clientInfo.OriginalRemotePoint} ({pair.Key.Description}) Connected", Log.LogLevel.Success); } waitingForVerificationClients.ClientConnected(clientInfo); } }
public void Add(TcpClientInfo clientInfo) { lock (this) { Clients.Add(clientInfo); } }