Esempio n. 1
0
        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();
            }
        }
Esempio n. 2
0
        /// <summary>
        /// TcpClient构造函数
        /// </summary>
        /// <param name="ip">ip地址</param>
        /// <param name="port">端口</param>
        /// <param name="connectSender">连接完成发送的身份信息</param>
        public TcpClient(IPAddress ip, int port, BaseTcpProtocol connectSender)
        {
            this.ip            = ip;
            this.port          = port;
            this.connectSender = connectSender;

            tcp = new TcpManager();
            tcp.MessageReceivedEvent += (client, content) => {
                try {
                    BaseTcpProtocol p = JsonConvert.DeserializeObject <BaseTcpProtocol>(content);

                    object obj = null;
                    switch (p.Type)
                    {
                    case TcpProtocolType.HeartBeat:
                        // 如果接收到心跳包, 则发送心跳包
                        heartAlive = 0;
                        var _ = tcp.Send(client, JsonConvert.SerializeObject(new HeartBeatProtocol()), null);
                        return;

                    case TcpProtocolType.NewDineInform:
                        obj = JsonConvert.DeserializeObject <NewDineInformProtocol>(content);
                        break;

                    case TcpProtocolType.PrintDine:
                        obj = JsonConvert.DeserializeObject <PrintDineProtocol>(content);
                        break;

                    case TcpProtocolType.PrintShifts:
                        obj = JsonConvert.DeserializeObject <PrintShiftsProtocol>(content);
                        break;

                    case TcpProtocolType.TcpServerStatusInform:
                        obj = JsonConvert.DeserializeObject <TcpServerStatusInformProtocol>(content);
                        break;
                    }

                    CallBackWhenMessageReceived?.Invoke(p.Type, obj);
                }
                catch (Exception e) {
                    exceptionOccured(e);
                }
            };
            tcp.ErrorEvent += async(s, e) => {
                client = null;
                exceptionOccured(e);
                // 重新连接
                await Task.Delay(ReconnectInterval * 1000);

                Start();
            };

            Timer timer = new Timer(10 * 1000);

            timer.Elapsed += Timer_Elapsed;
            timer.Start();
        }
Esempio n. 3
0
 /// <summary>
 /// 发送tcp协议
 /// </summary>
 /// <param name="p">协议</param>
 public void Send(BaseTcpProtocol p, Action callBack = null)
 {
     if (client == null)
     {
         waitedQueue.Enqueue(p);
         return;
     }
     var _ = tcp.Send(client, JsonConvert.SerializeObject(p), () => {
         callBack?.Invoke();
     });
 }
Esempio n. 4
0
        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);
            }
        }