Example #1
0
        //******************************************************************************
        //******************************************************************************
        //******************************************************************************
        // Copy a message from a byte buffer
        //
        // 1) Extract the header parameters.
        // 2) Create a new message object of the type specifed by the identifiers
        //    that were extracted from the header
        // 3) Copy the data from the byte buffer into the new message object
        // and returns a pointer to the base class.

        public ByteContent makeMsgFromBuffer(ByteBuffer aBuffer)
        {
            // Set buffer direction for get.
            aBuffer.setCopyFrom();

            // Extract the header parameters.
            aBuffer.rewind();
            extractMessageHeaderParms(aBuffer);
            aBuffer.rewind();

            // Guard.
            if (!mHeaderValidFlag)
            {
                return(null);
            }

            // Call inheritor's creator to create a new message based on the
            // message type that was extracted from the header.
            ByteContent aMsg = mMsgCreator.createMsg(mMessageType);

            // Guard
            if (aMsg == null)
            {
                return(null);
            }

            // Call inheritor's copier to copy from the buffer to the message.
            aMsg.copyToFrom(aBuffer);

            // Done.
            return(aMsg);
        }
Example #2
0
        //**************************************************************************
        // Create a new message based on a message type

        public override Ris.ByteContent createMsg(int aMessageType)
        {
            Ris.ByteContent tMsg = null;

            switch (aMessageType)
            {
            case MsgIdT.cTestMsg:
                tMsg = new TestMsg();
                break;

            case MsgIdT.cFirstMessageMsg:
                tMsg = new FirstMessageMsg();
                break;

            case MsgIdT.cStatusRequestMsg:
                tMsg = new StatusRequestMsg();
                break;

            case MsgIdT.cStatusResponseMsg:
                tMsg = new StatusResponseMsg();
                break;

            case MsgIdT.cDataMsg:
                tMsg = new DataMsg();
                break;

            default:
                break;
            }
            return(tMsg);
        }
        public void getFromBuffer(ByteContent aContent)
        {
            // Set copy direction
            setCopyFrom();

            // Call ByteContent supplied member function to do the get
            aContent.copyToFrom(this);
        }
Example #4
0
        //***************************************************************************
        //***************************************************************************
        //***************************************************************************
        // Copy a message to a byte buffer.

        public void putMsgToBuffer(ByteBuffer aBuffer, ByteContent aMsg)
        {
            // Call inheritor's override to preprocess the message before it is sent.
            processBeforeSend(aMsg);

            // Set buffer direction for put.
            aBuffer.setCopyTo();

            // Call inheritor's copier to copy from the message to the buffer.
            aMsg.copyToFrom(aBuffer);
        }
Example #5
0
        //**********************************************************************
        //**********************************************************************
        //**********************************************************************
        // Thread run function, receives messages

        public void threadRun()
        {
            while (true)
            {
                //--------------------------------------------------------------
                // Receive from the message socket

                ByteContent tMsg = mRxSocket.receiveMsg();

                //--------------------------------------------------------------
                // Call inheritor's override to process the message

                if (tMsg != null)
                {
                    processRxMsg(tMsg);
                }
                else
                {
                    return;
                }
            }
        }
Example #6
0
        //**********************************************************************
        //**********************************************************************
        //**********************************************************************
        // Send message to the socket.

        public void sendMsg(ByteContent aMsg)
        {
            //------------------------------------------------------------------
            // Create byte buffer.
            ByteBuffer tBuffer = new ByteBuffer(mMonkey.getMaxBufferSize());

            // Copy message to buffer.
            mMonkey.putMsgToBuffer(tBuffer, aMsg);

            //------------------------------------------------------------------
            // Send buffer to socket.
            byte[] tTxBytes  = tBuffer.getBaseAddress();
            int    tTxLength = tBuffer.getPosition();

            try
            {
                int tSent = mSocket.SendTo(tTxBytes, tTxLength, SocketFlags.None, mIPEndPoint);
                Prn.print(Prn.SocketRun2, "UdpTxSocket tx message {0}", tSent);
            }
            catch
            {
                Prn.print(Prn.SocketRun2, "UdpTxSocket Send ERROR");
            }
        }
        //******************************************************************************
        //******************************************************************************
        //******************************************************************************
        //******************************************************************************
        //******************************************************************************
        //******************************************************************************
        // Copy reference types

        //---------------------------------------------------------------------------
        // copy does either a copy to the buffer or a copy from the buffer
        //
        // These either put an item to the byte buffer or get an
        // item from the byte buffer, based on the value of the
        // buffer copy direction flag.

        //******************************************************************************
        //******************************************************************************
        //******************************************************************************

        public void copy(ByteContent aContent)
        {
            aContent.copyToFrom(this);
        }
Example #8
0
        //**********************************************************************
        //**********************************************************************
        //**********************************************************************
        // Transmit message

        public void sendMsg(ByteContent aTxMsg)
        {
            mTxSocket.sendMsg(aTxMsg);
        }
Example #9
0
        //**********************************************************************
        // Inheriting classes override this method to process received messages

        public abstract void processRxMsg(ByteContent aRxMsg);
Example #10
0
 // Preprocess a message before it is sent.
 public abstract void processBeforeSend(ByteContent aMsg);
Example #11
0
        //**********************************************************************
        //**********************************************************************
        //**********************************************************************
        // Receive message from socket.

        public ByteContent receiveMsg()
        {
            //------------------------------------------------------------------
            // Guard

            if (mUdpClient == null)
            {
                return(null);
            }

            //------------------------------------------------------------------
            // Receive bytes from socket

            byte[] tRxBytes = null;

            try
            {
                tRxBytes = mUdpClient.Receive(ref mIPEndPoint);
            }
            catch
            {
                Prn.print(Prn.SocketRun1, "UdpRxSocket Receive EXCEPTION");
                return(null);
            }
            //------------------------------------------------------------------
            // Guard.

            if (tRxBytes != null)
            {
                Prn.print(Prn.SocketRun2, "UdpRxSocket rx message {0}", tRxBytes.Length);
            }
            else
            {
                Prn.print(Prn.SocketRun1, "UdpRxSocket ERROR");
                return(null);
            }

            //------------------------------------------------------------------
            // Create byte buffer.

            ByteBuffer tBuffer = new ByteBuffer(tRxBytes);

            tBuffer.setCopyFrom();
            tBuffer.setLength(tRxBytes.Length);

            //------------------------------------------------------------------
            // Copy from the receive buffer into the message parser object
            // and validate the header

            mMonkey.extractMessageHeaderParms(tBuffer);

            // If the header is not valid then error
            if (!mMonkey.mHeaderValidFlag)
            {
                Prn.print(Prn.SocketRun1, "UdpRxSocket Receive FAIL INVALID HEADER");
                return(null);
            }

            Prn.print(Prn.SocketRun3, "UdpRxSocket Receive Header {0}", mMonkey.mHeaderLength);

            //------------------------------------------------------------------
            // At this point the buffer contains the complete message.
            // Extract the message from the byte buffer into a new message
            // object and return it.

            tBuffer.rewind();
            ByteContent tRxMsg = mMonkey.getMsgFromBuffer(tBuffer);

            if (tRxMsg == null)
            {
                Prn.print(Prn.SocketRun1, "UdpRxSocket FAIL INVALID MESSAGE");
                return(null);
            }

            // Returning true  means socket was not closed
            // Returning false means socket was closed
            mRxCount++;
            return(tRxMsg);
        }