コード例 #1
0
ファイル: synapse_client.cs プロジェクト: scott-han/test
        publish(string topic_name, message_base msg, bool send_as_delta = false, int delta_stream_id = 1, long message_sequence_number = 0, long micros_since_epoch = 0)
        {
            if (delta_stream_id < 1)
            {
                throw new ArgumentException("delta_stream_id must be greater than 0");
            }

            stream = new MemoryStream();
            var proto = new TCompactProtocol(new TStreamTransport(stream, stream));

            if (send_as_delta == false)
            {
                msg.set_modified_flag();
                delta_stream_id = -delta_stream_id;
            }
            proto.WriteI32(delta_stream_id);
            proto.WriteI32((int)msg.get_type_id());
            msg.Write(proto);

            IModel channel;

            if (!publishing_channels.TryGetValue(topic_name, out channel))
            {
                channel = conn.CreateModel();
                if (props == null)
                {
                    props = channel.CreateBasicProperties();
                }
                publishing_channels[topic_name] = channel;
            }


            // publishing_channel.QueueDeclare(topic_name, false, false, false, null);
            // not too fast at the moment (bare bones example), TODO later will set topic_name elsewhere so as not to resend it everytime

            // explicit since epoch in microseconds time for the server's requirements
            if (micros_since_epoch != 0 && message_sequence_number != 0)
            {
                var dictionary = new Dictionary <string, object>();
                dictionary.Add("XXXXXX", message_sequence_number);
                props.ContentType = "X";
                props.Headers     = dictionary;
                props.Timestamp   = new AmqpTimestamp(micros_since_epoch);
                channel.BasicPublish("", topic_name, props, stream.ToArray());
            }
            else
            {
                channel.BasicPublish("", topic_name, null, stream.ToArray());
            }

            msg.clear_modified_flag();

            //Console.WriteLine("thrift size: " + stream.Length);
            //Console.WriteLine("thrift size: " + stream.ToArray().Length.ToString());
        }
コード例 #2
0
        publish(string topic_name, message_base msg, WaypointsType waypoints = null, bool send_as_delta = false, byte[] stream_id = null, long message_sequence_number = 0, ulong micros_since_epoch = 0)
        {
            unchecked {
                MemoryStream stream = new MemoryStream();
                var          proto  = new TCompactProtocol(new TStreamTransport(stream, stream));

                proto.WriteByte(1);

                uint flags = 0;
                if (waypoints != null)
                {
                    flags |= (uint)message_envelope <WaypointsType> .flags_enum.has_waypoints;
                }
                if (send_as_delta == true)
                {
                    flags |= (uint)message_envelope <WaypointsType> .flags_enum.is_delta;
                }
                if (stream_id != null)
                {
                    flags |= (uint)message_envelope <WaypointsType> .flags_enum.has_stream_id;
                }
                proto.WriteI32((int)flags);

                proto.WriteString(msg.GetType().Name);

                if (stream_id != null)
                {
                    proto.WriteBinary(stream_id);
                }

                if (waypoints != null)
                {
                    waypoints.Write(proto);
                }

                msg.Write(proto, !send_as_delta);

                Publishing_channel channel;
                string             topic_name_;
                lock (publishing_channels) {
                    if (!publishing_channels.TryGetValue(topic_name, out channel))
                    {
                        channel = new Publishing_channel(conn, Publishing_channel_idle_timeout, topic_name, publishing_channels, publishing_exception);
                        publishing_channels[topic_name] = channel;
                        topic_name_ = topic_name;
                    }
                    else
                    {
                        topic_name_ = "";
                    }

                    // publishing_channel.QueueDeclare(topic_name, false, false, false, null);
                    // not too fast at the moment (bare bones example), TODO later will set topic_name elsewhere so as not to resend it everytime

                    // explicit since epoch in microseconds time for the server's requirements
                    // TODO -- perhaps drop seq. number altogether or provide individually-parsable (at the server end) possibility of supplying either one, or both (currently it is both or none thereby wasting at times 8 bytes)...
                    IBasicProperties props_;
                    if (micros_since_epoch != 0 || message_sequence_number != 0)
                    {
                        var dictionary = new Dictionary <string, object>();
                        dictionary.Add("XXXXXX", (long)message_sequence_number);
                        if (props == null)
                        {
                            props = channel.AMQP_channel.CreateBasicProperties();
                        }
                        props.ContentType = "X";
                        props.Headers     = dictionary;
                        props.Timestamp   = new AmqpTimestamp((long)micros_since_epoch);
                        props_            = props;
                    }
                    else
                    {
                        props_ = null;
                    }
                    channel.Published_flag = true;
                    channel.AMQP_channel.BasicPublish("", topic_name_, props_, stream.ToArray());
                }
                return(stream.Length);
            }
        }