Пример #1
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);
            }
        }