コード例 #1
0
ファイル: Example.cs プロジェクト: jonefmc/zproto
        /// <summary>
        /// Send the Example to the socket.
        /// </summary>
        public void Send(IOutgoingSocket output)
        {
            if (output is RouterSocket)
                output.SendMore(RoutingId);

            int frameSize = 2 + 1;          //  Signature and message ID
            switch (Id)
            {
                case MessageId.Log:
                    frameSize += 2;            //  Sequence
                    frameSize += 2;            //  Version
                    frameSize += 1;            //  Level
                    frameSize += 1;            //  Event
                    frameSize += 2;            //  Node
                    frameSize += 2;            //  Peer
                    frameSize += 8;            //  Time
                    frameSize += 1 + Host.Length;
                    frameSize += 4;
                    if (Data != null)
                        frameSize += Data.Length;
                    break;
                case MessageId.Structures:
                    frameSize += 2;            //  Sequence
                    frameSize += 4;            //  Size is 4 octets
                    if (Aliases != null)
                    {
                        foreach(string s in Aliases)
                        {
                            frameSize += 4 + s.Length;
                        }
                    }
                    frameSize += 4;            //  Size is 4 octets
                    if (Headers != null)
                    {
                        int headersSize = 0;

                        foreach (var pair in Headers)
                        {
                            headersSize += 1 + pair.Key.Length;
                            headersSize += 4 + pair.Value.Length;
                        }

                        frameSize += headersSize;
                    }

                    break;
                case MessageId.Binary:
                    frameSize += 2;            //  Sequence
                    frameSize += 4;            //  Flags
                    frameSize += 4;            //  Size is 4 octets
                    if (PublicKey != null)
                        frameSize += PublicKey.Length;
                    frameSize  += 16;    //  Identifier
                    break;
                case MessageId.Types:
                    frameSize += 2;            //  Sequence
                    frameSize += 1 + ClientForename.Length;
                    frameSize += 1 + ClientSurname.Length;
                    frameSize += 1 + ClientMobile.Length;
                    frameSize += 1 + ClientEmail.Length;
                    frameSize += 1 + SupplierForename.Length;
                    frameSize += 1 + SupplierSurname.Length;
                    frameSize += 1 + SupplierMobile.Length;
                    frameSize += 1 + SupplierEmail.Length;
                    break;
            }

            //  Now serialize message into the buffer
            Msg msg = new Msg();
            msg.InitPool(frameSize);

            try
            {
                m_offset = 0;
                m_buffer = msg.Data;

                // put signature
                PutNumber2(0xAAA0 | 0);

                // put message id
                PutNumber1((byte)Id);
                bool sendContent = false;
                int frames = 1;              //  Total number of frames to send

                switch (Id)
                {
                    case MessageId.Log:
                        PutNumber2(Sequence);
                        PutNumber2(3); // Version
                        PutNumber1(Level);
                        PutNumber1(Event);
                        PutNumber2(Node);
                        PutNumber2(Peer);
                        PutNumber8(Time);
                        PutString(Host);
                        if (Data != null)
                            PutLongString(Data);
                        else
                            PutNumber4(0);    //  Empty string
                    break;
                    case MessageId.Structures:
                        PutNumber2(Sequence);

                        if (Aliases != null)
                        {
                            PutNumber4((UInt32)Aliases.Count);

                            foreach (string s in Aliases)
                            {
                                PutLongString(s);
                            }
                        }
                        else
                            PutNumber4(0);    //  Empty string array

                        if (Headers != null)
                        {
                            PutNumber4((UInt32)Headers.Count);

                            foreach(var pair in Headers)
                            {
                                PutString(pair.Key);
                                PutLongString(pair.Value);
                            }
                        }
                        else
                            PutNumber4(0);    //  Empty dictionary

                    break;
                    case MessageId.Binary:
                        PutNumber2(Sequence);
                        PutOctets(Flags, 4);

                        if (PublicKey != null)
                        {
                            PutNumber4((UInt32)PublicKey.Length);
                            Buffer.BlockCopy(PublicKey, 0, m_buffer, m_offset, PublicKey.Length);
                            m_offset += PublicKey.Length;
                        }
                        else
                            PutNumber4(0);    //  Empty chunk

                        // copy guid
                        Buffer.BlockCopy(Identifier.ToByteArray(), 0, m_buffer, m_offset, 16);
                        m_offset += 16;
                        frames++;
                        frames += Content != null ? Content.FrameCount : 1;
                        sendContent = true;
                    break;
                    case MessageId.Types:
                        PutNumber2(Sequence);
                        PutString(ClientForename);
                        PutString(ClientSurname);
                        PutString(ClientMobile);
                        PutString(ClientEmail);
                        PutString(SupplierForename);
                        PutString(SupplierSurname);
                        PutString(SupplierMobile);
                        PutString(SupplierEmail);
                    break;
                }

                //  Now send the data frame
                output.Send(ref msg, --frames == 0 ? SendReceiveOptions.None : SendReceiveOptions.SendMore);

                //  Now send any frame fields, in order
                if (Id == MessageId.Binary)
                {
                    if (--frames == 0)
                        output.Send(Address != null ? Address : new byte[0]);
                    else
                        output.SendMore(Address != null ? Address : new byte[0]);
                }

                //  Now send the Content if necessary
                if (sendContent)
                {
                    if (Content != null)
                    {
                        output.SendMessage(Content);
                    }
                    else
                        output.Send(new byte[0]);
                }
            }
            finally
            {
                m_buffer = null;
                msg.Close();
            }
        }
コード例 #2
0
ファイル: Example.cs プロジェクト: xaqq/zproto
        /// <summary>
        /// Send the Example to the socket.
        /// </summary>
        public void Send(IOutgoingSocket output)
        {
            if (output is RouterSocket)
            {
                output.SendMore(RoutingId);
            }

            int frameSize = 2 + 1;                      //  Signature and message ID

            switch (Id)
            {
            case MessageId.Log:
                frameSize += 2;                                    //  Sequence
                frameSize += 2;                                    //  Version
                frameSize += 1;                                    //  Level
                frameSize += 1;                                    //  Event
                frameSize += 2;                                    //  Node
                frameSize += 2;                                    //  Peer
                frameSize += 8;                                    //  Time
                frameSize += 1 + Host.Length;
                frameSize += 4;
                if (Data != null)
                {
                    frameSize += Data.Length;
                }
                break;

            case MessageId.Structures:
                frameSize += 2;                                    //  Sequence
                frameSize += 4;                                    //  Size is 4 octets
                if (Aliases != null)
                {
                    foreach (string s in Aliases)
                    {
                        frameSize += 4 + s.Length;
                    }
                }
                frameSize += 4;                                    //  Size is 4 octets
                if (Headers != null)
                {
                    int headersSize = 0;

                    foreach (var pair in Headers)
                    {
                        headersSize += 1 + pair.Key.Length;
                        headersSize += 4 + pair.Value.Length;
                    }

                    frameSize += headersSize;
                }

                break;

            case MessageId.Binary:
                frameSize += 2;                                    //  Sequence
                frameSize += 4;                                    //  Flags
                frameSize += 4;                                    //  Size is 4 octets
                if (PublicKey != null)
                {
                    frameSize += PublicKey.Length;
                }
                frameSize += 16;                             //  Identifier
                break;

            case MessageId.Types:
                frameSize += 2;                                    //  Sequence
                frameSize += 1 + ClientForename.Length;
                frameSize += 1 + ClientSurname.Length;
                frameSize += 1 + ClientMobile.Length;
                frameSize += 1 + ClientEmail.Length;
                frameSize += 1 + SupplierForename.Length;
                frameSize += 1 + SupplierSurname.Length;
                frameSize += 1 + SupplierMobile.Length;
                frameSize += 1 + SupplierEmail.Length;
                break;
            }

            //  Now serialize message into the buffer
            Msg msg = new Msg();

            msg.InitPool(frameSize);

            try
            {
                m_offset = 0;
                m_buffer = msg.Data;

                // put signature
                PutNumber2(0xAAA0 | 0);

                // put message id
                PutNumber1((byte)Id);
                bool sendContent = false;
                int  frames      = 1;                        //  Total number of frames to send

                switch (Id)
                {
                case MessageId.Log:
                    PutNumber2(Sequence);
                    PutNumber2(3);                             // Version
                    PutNumber1(Level);
                    PutNumber1(Event);
                    PutNumber2(Node);
                    PutNumber2(Peer);
                    PutNumber8(Time);
                    PutString(Host);
                    if (Data != null)
                    {
                        PutLongString(Data);
                    }
                    else
                    {
                        PutNumber4(0);                                    //  Empty string
                    }
                    break;

                case MessageId.Structures:
                    PutNumber2(Sequence);

                    if (Aliases != null)
                    {
                        PutNumber4((UInt32)Aliases.Count);

                        foreach (string s in Aliases)
                        {
                            PutLongString(s);
                        }
                    }
                    else
                    {
                        PutNumber4(0);                                    //  Empty string array
                    }
                    if (Headers != null)
                    {
                        PutNumber4((UInt32)Headers.Count);

                        foreach (var pair in Headers)
                        {
                            PutString(pair.Key);
                            PutLongString(pair.Value);
                        }
                    }
                    else
                    {
                        PutNumber4(0);                                    //  Empty dictionary
                    }
                    break;

                case MessageId.Binary:
                    PutNumber2(Sequence);
                    PutOctets(Flags, 4);

                    if (PublicKey != null)
                    {
                        PutNumber4((UInt32)PublicKey.Length);
                        Buffer.BlockCopy(PublicKey, 0, m_buffer, m_offset, PublicKey.Length);
                        m_offset += PublicKey.Length;
                    }
                    else
                    {
                        PutNumber4(0);                                    //  Empty chunk
                    }
                    // copy guid
                    Buffer.BlockCopy(Identifier.ToByteArray(), 0, m_buffer, m_offset, 16);
                    m_offset += 16;
                    frames++;
                    frames     += Content != null ? Content.FrameCount : 1;
                    sendContent = true;
                    break;

                case MessageId.Types:
                    PutNumber2(Sequence);
                    PutString(ClientForename);
                    PutString(ClientSurname);
                    PutString(ClientMobile);
                    PutString(ClientEmail);
                    PutString(SupplierForename);
                    PutString(SupplierSurname);
                    PutString(SupplierMobile);
                    PutString(SupplierEmail);
                    break;
                }

                //  Now send the data frame
                output.Send(ref msg, --frames == 0 ? SendReceiveOptions.None : SendReceiveOptions.SendMore);

                //  Now send any frame fields, in order
                if (Id == MessageId.Binary)
                {
                    if (--frames == 0)
                    {
                        output.Send(Address != null ? Address : new byte[0]);
                    }
                    else
                    {
                        output.SendMore(Address != null ? Address : new byte[0]);
                    }
                }

                //  Now send the Content if necessary
                if (sendContent)
                {
                    if (Content != null)
                    {
                        output.SendMessage(Content);
                    }
                    else
                    {
                        output.Send(new byte[0]);
                    }
                }
            }
            finally
            {
                m_buffer = null;
                msg.Close();
            }
        }