示例#1
0
        public void RunReceiver(object buffer)
        {
            MessageBuffers workingbuffer = (MessageBuffers)buffer;
            string         remoteaddress = workingbuffer.Connection.RemoteEndPoint.Address.ToString();

            while (workingbuffer.Running)
            {
                string msg = workingbuffer.ReadNextMessageIN();
                if (!string.IsNullOrEmpty(msg))
                {
                    //Response
                    EmiProtocol eo;
                    try
                    {
                        eo = new EmiProtocol(msg, ReferredServer);
                        string         operation = eo.OT;
                        string         response  = string.Empty;
                        string         SR        = string.Empty;
                        EmiMessageType MTType    = EmiMessageType.SYS;
                        EmiMessage     m2        = new EmiMessage();

                        if (operation != null)
                        {
                            if (eo.OT == "51")
                            {
                                MTType = EmiMessageType.MT;

                                if (!workingbuffer.AcceptsMT)
                                {
                                    response = eo.CreateACK51(false, "07", "Authentication failure");
                                }


                                //CHECK IF THE MT IS PING OR ACK?
                                string MSISDN = eo.AdC;
                                if (string.IsNullOrEmpty(MSISDN))
                                {
                                    MTType   = EmiMessageType.PING;
                                    m2.Type  = EmiMessageType.PING_ACK;
                                    response = eo.CreateACK51(false, "06", "AdC invalid");
                                }
                                else
                                {
                                    //HERE Check The return type
                                    switch (ReferredServer.mtBehavior)
                                    {
                                    case MTBehavior.ACK:
                                        response = eo.CreateACK51(true, string.Empty, string.Empty);
                                        m2.Type  = EmiMessageType.MT_ACK;

                                        //CHECK if SR is demanded
                                        if (ReferredServer.srActive)
                                        {
                                            SR = eo.CreateSRForMT(ReferredServer.srDst, ReferredServer.srRsn);
                                        }
                                        else
                                        {
                                            SR = string.Empty;
                                        }

                                        break;

                                    case MTBehavior.NACK:
                                        response = eo.CreateACK51(false, ReferredServer.nackCode.ToString(), string.Empty);
                                        m2.Type  = EmiMessageType.MT_NACK;
                                        break;

                                    case MTBehavior.Nothing:
                                        response = string.Empty;
                                        m2.Type  = EmiMessageType.SYS;
                                        break;
                                    }
                                }
                            }
                            else if (eo.OT == "52")
                            {
                                MTType   = EmiMessageType.MO_ACK;
                                response = string.Empty;
                            }
                            else if (eo.OT == "53")
                            {
                                MTType   = EmiMessageType.SR_ACK;
                                response = string.Empty;
                            }
                            else if (eo.OT == "60")
                            {
                                MTType   = EmiMessageType.SESSION;
                                response = eo.CreateACK60(true, string.Empty);
                                m2.Type  = EmiMessageType.SESSION_ACK;
                                workingbuffer.AcceptsMT = true;
                            }

                            //Add MT To Log

                            EmiMessage m = new EmiMessage
                            {
                                Type              = MTType,
                                Direction         = MessageDirection.Incoming,
                                CreateDate        = new DateTimeOffset(DateTime.Now),
                                RAWMessage        = msg,
                                EMIProtocolObject = eo
                            };

                            AbstractMetadata metadata = null;
                            if (m.Type == EmiMessageType.MT)
                            {
                                metadata = ReferredServer.MTMetadata;
                            }
                            else if (m.Type == EmiMessageType.SESSION)
                            {
                                metadata = ReferredServer.SSMetadata;
                            }


                            //Do not log if hidePingACK is true and MessageType is Ping or ACK.
                            if (!(ReferredServer.hidePingACK && (m.Type == EmiMessageType.PING || m.Type == EmiMessageType.MO_ACK || m.Type == EmiMessageType.SR_ACK)))
                            {
                                MessageQueue.Add(
                                    new Message()
                                {
                                    Metadata      = ReferredServer.MTMetadata,
                                    Title         = m.FriendlyMessage,
                                    Direction     = MessageDirection.Incoming,
                                    RequestObject = m.EMIProtocolObject,
                                    RequestText   = m.RAWMessage,
                                    Status        = MessageStatus.Received,
                                    Tag           = m.RAWMessage
                                });
                            }

                            if (response.StartsWith("\x02"))
                            {
                                //Add ACK/NACK to buffer:
                                m2.Direction         = 0;
                                m2.CreateDate        = new DateTimeOffset(DateTime.Now);
                                m2.RAWMessage        = response;
                                m2.EMIProtocolObject = new EmiProtocol(response);
                                lock (workingbuffer.MessageToBeSent)
                                {
                                    workingbuffer.MessageToBeSent.Enqueue(m2);
                                    Monitor.Pulse(workingbuffer.MessageToBeSent);
                                }



                                if (m2.Type == EmiMessageType.MT_ACK && !string.IsNullOrEmpty(SR))
                                {
                                    EmiMessage sr = new EmiMessage
                                    {
                                        Type              = EmiMessageType.SR,
                                        Direction         = 0,
                                        CreateDate        = new DateTimeOffset(DateTime.Now).AddSeconds(ReferredServer.srDelay),
                                        RAWMessage        = SR,
                                        ExpectedSendDate  = new DateTimeOffset(DateTime.Now).AddSeconds(ReferredServer.srDelay),
                                        EMIProtocolObject = new EmiProtocol(SR)
                                    };

                                    //Before we sent SR directly, like this ->
                                    //SendMOSRACK(sr)
                                    //Now we stock the SR to a queue, and there will be a thread who will look this queue.
                                    //With this method, the SR can be sent 5 or 10 seconds later.
                                    workingbuffer.SRToBeSent.Enqueue(sr);
                                }
                            }
                            else
                            {
                                //Log that nothing will be replied

                                /*
                                 * m2.Direction = 0
                                 * m2.CreateDate = new DateTimeOffset(DateTime.Now)
                                 * m2.RAWMessage = "Nothing has been sent for this MT"
                                 * lock(MainPage.Messages){
                                 *  MainPage.Messages.Add(m2)
                                 * }
                                 */
                            }
                            Thread.Sleep(10);
                        }
                    }
                    catch
                    {
                        //EMIProtocol is not valid
                    }
                }
                else
                {
                    Thread.Sleep(100); //If there is no MT to receive, sleep 10 milliseconds
                }
            }

            MessageQueue.Add(new Message()
            {
                Direction = MessageDirection.None,
                Status    = MessageStatus.Received,
                Title     = string.Format("SMSC: The client from {0} disconnected from this SMS Center", remoteaddress),
            });
            connections.Remove(workingbuffer.Connection);
            Buffers.Remove(workingbuffer);
        }
        private void ParseObject(object o)
        {
            EmiProtocol eo = (EmiProtocol)o;

            trame            = eo.OriginalTrame;
            txtTrameEMI.Text = trame;
            try
            {
                string operation = eo.OT;

                if (operation != null)
                {
                    if (eo.OT == "51")
                    {
                        if (eo.OR == "O")
                        {
                            messageType = EmiMessageType.MT;
                        }
                        else
                        {
                            messageType = EmiMessageType.MT_ACK;
                        }

                        //CHECK IF THE MT IS PING OR ACK?
                        string MSISDN = eo.AdC;
                        if (string.IsNullOrEmpty(MSISDN))
                        {
                            if (eo.OR == "O")
                            {
                                messageType = EmiMessageType.PING;
                            }
                            else
                            {
                                messageType = EmiMessageType.PING_ACK;
                            }
                        }
                    }
                    else if (eo.OT == "52")
                    {
                        if (eo.OR == "O")
                        {
                            messageType = EmiMessageType.MO;
                        }
                        else
                        {
                            messageType = EmiMessageType.MO_ACK;
                        }
                    }
                    else if (eo.OT == "53")
                    {
                        if (eo.OR == "O")
                        {
                            messageType = EmiMessageType.SR;
                        }
                        else
                        {
                            messageType = EmiMessageType.SR_ACK;
                        }
                    }
                    else if (eo.OT == "60")
                    {
                        if (eo.OR == "O")
                        {
                            messageType = EmiMessageType.SESSION;
                        }
                        else
                        {
                            messageType = EmiMessageType.SESSION_ACK;
                        }
                    }

                    Analyse();
                }
            }
            catch
            {
                viewAnalyse.Items.Add(new Variable("Error", "Your EMI frame is not valid"));
            }
        }