Exemple #1
0
        public static byte[] ProcessPandARequest(string clientID, byte[] bytes)
        {
            //Process a PandA request from a remote socket client
            PandaPacket request  = null;
            PandaPacket response = null;
            DateTime    started  = DateTime.Now;

            try {
                //Process the message
                request = PandaPacket.Decode(clientID, bytes);
                AppTrace.Instance().TheTrace.WriteLine(new TraceMessage("REQUEST:" + request.Message, AppLib.EVENTLOGNAME, LogLevel.Information, "PacketMgr"));
                if (request.Valid)
                {
                    //Check if this was a prior request
                    PandaPacket priorResponse = isPriorRequest(clientID, request);
                    if (priorResponse != null)
                    {
                        //This was a prior message; return prior response
                        response = priorResponse;
                        AppTrace.Instance().TheTrace.WriteLine(new TraceMessage("PRIOR RESPONSE: " + priorResponse.Message, AppLib.EVENTLOGNAME, LogLevel.Information, "PacketMgr"));
                    }
                    else
                    {
                        //New, valid message; persist request and route to message handler
                        switch (request.MessageCode)
                        {
                        case REQUEST_LABELDATA:
                            response    = getLabelDataResponse(clientID, request);
                            LastRequest = request;
                            try { request.ProcessingTime = ((TimeSpan)DateTime.Now.Subtract(started)).TotalSeconds; } catch {}
                            if (LabelDataRequestComplete != null)
                            {
                                LabelDataRequestComplete(null, new PandaPacketEventArgs(request));
                            }
                            break;

                        case REQUEST_VERIFYLABEL:
                            response    = getVerifyLabelResponse(clientID, request);
                            LastRequest = request;
                            try { request.ProcessingTime = ((TimeSpan)DateTime.Now.Subtract(started)).TotalSeconds; } catch {}
                            if (VerifyLabelRequestComplete != null)
                            {
                                VerifyLabelRequestComplete(null, new PandaPacketEventArgs(request));
                            }
                            break;

                        case REQUEST_HEARTBEAT:
                            response = getHeartbeatResponse(clientID, request);
                            if (HeartbeatRequestComplete != null)
                            {
                                HeartbeatRequestComplete(null, EventArgs.Empty);
                            }
                            break;

                        case REQUEST_RESEND:
                            response = LastRequest.Response;
                            AppTrace.Instance().TheTrace.WriteLine(new TraceMessage("Resend requested.", AppLib.EVENTLOGNAME, LogLevel.Warning, "PacketMgr"));
                            if (ResendRequestComplete != null)
                            {
                                ResendRequestComplete(null, EventArgs.Empty);
                            }
                            break;

                        default:
                            AppTrace.Instance().TheTrace.WriteLine(new TraceMessage("Unknown message code.", AppLib.EVENTLOGNAME, LogLevel.Warning, "PacketMgr"));
                            break;
                        }
                    }
                }
                else
                {
                    //Invalid message packet
                    response = PandaPacket.Encode(clientID, RESPONSE_MSGERROR, request, 0, 0, "");
                    AppTrace.Instance().TheTrace.WriteLine(new TraceMessage("Message failed (decode) validation...", AppLib.EVENTLOGNAME, LogLevel.Warning, "PacketMgr"));
                }
            }
            catch (Exception ex) {
                response = PandaPacket.Encode(clientID, RESPONSE_MSGERROR, request, 0, 0, "");
                AppTrace.Instance().TheTrace.WriteLine(new TraceMessage("Error occurred while processing message: " + ex.ToString(), AppLib.EVENTLOGNAME, LogLevel.Error, "PacketMgr"));
            }
            return(response != null?response.Bytes:null);
        }