为我的tcp客户端创建消息缓冲区或sendq [英] Creating a message buffer, or sendq for my tcp client

查看:153
本文介绍了为我的tcp客户端创建消息缓冲区或sendq的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯,我的项目是IRC Bot.然后,所有传出的信息都通过方法进行控制并格式化为原始数据,然后被发送到一个主void,后者将数据发送到服务器.我正在寻找一种创建SendQ的方法,如果这是所有外出信息的正确术语,请参见以下示例:

我想说的是,如果我向空方发送了20条消息,则要立即发送的前3-4条消息,那么4-10条消息会稍有停顿,并且会有更大的停顿,直到一切都有机会赶上...

所以我最初的想法是让每条消息在时钟上增加10秒,如果时间超过30秒,则开始放慢发送速度,直到时钟再次在30秒以下,并设置其他间隔...例如,将sendrate 0设置为小于30,则sendrate 1将设置为大于30,然后sendrate 2将为60,以此类推...然后,每秒,它将变成-1.因此,如果我连续发送4条消息,则ID必须等待10秒,直到再次发送成功.

摘要:我正在寻找一种方法来减慢我的机器人的原始消息发送速度,从而使我的机器人不会从服务器中泛滥.

PS:id还喜欢以某种方式添加某种(可选)消息优先级设置,以便在Que中获得更高优先级的消息先于其他消息发送


这是C#,顺便说一句

_____


感谢您的回答,但是它不是我一直在寻找的...消息将从线程添加到队列中,只是不断地在队列中添加或暂停.所以id需要某种线程来管理队列,然后将它们添加到队列中时发送消息

Well, my project is an IRC Bot. and all outgoing information gets controlled and formatted to raw data by methods, and then gets sent to one main void, which then sends the data to the server. Im looking for a way to create a SendQ, if that is the correct term for all out going information, here is an example:

Id want say, if i sent 20 messages to the void the first 3-4 messages to be send immediately, then the 4-10 would have a slight pause, and a greater pause, untill everything has a chance to catch up...

so my origanal thought, was have each message add 10 seconds to a clock, and if the time reachs over 30 seconds then start slowing down the send-rate untill the clock reachs under 30 seconds again, and have other intervals as well... like have sendrate 0 be under 30, sendrate 1 would be over 30, then sendrate 2 would be 60 etc... and then like, every second, it would -1 the clock. so, if i send 4 messages in succession, id have to wait 10 seconds untill sendrate was instant again.

summary: im looking a way to slow raw message sending for my bot, so that my bot will not flood off of the server.

PS: id also like to add some sort of (optional) message priority setting somehow, so that higher priority messages in the Que will get sent before others


This is C#, btw

_____


thanks for your answer, however its not quite what i was looking for... messages would be added to the queue from a thread, that just constantly adds, or pauses, things to the queue. so id need some sort of thread to manage the queue and then send messages as they are added to the queue

推荐答案

我是印度的srikanth,我去了通过您的问题并认为这很有趣,我实现了C#,但是请告诉我是否错误,以便我可以发送正确的实现

所以代码需要额外的2个命名空间

1)System.Collections.Generic;
2)System.Threading;

我假设您的消息是字符串,如果不是字符串,则将其更改为适当的类型
声明优先级类型的枚举器
hi I''m srikanth from India, i went through your question and thought it is interesting , I implemented it C#, but tell me if i''m wrong, so that i can send the correct implementation

so the code requires 2 namespaces extra

1) System.Collections.Generic;
2) System.Threading;

I assumed your message is string, if it is not a string then change it to appropriate type
Declare an enumerator for priority type
public enum priority { higher, medium, lower };


创建一个用于存储消息和优先级类型的类


create a class for storing message and priority type

public class QPriority
    {
        priority messagePr;
        string Qmessage;
      public  QPriority(priority pr, string message)
        {
            messagePr = pr;
            Qmessage = message;
        }
        public priority messagePriority
        {
            get
            {
                return messagePr;
            }
        }
        public string myMessage
        {
            get
            {
                return Qmessage;
            }
        }
    }


然后创建类型为QPriority的类的泛型类型列表


then create a generic type list of type class QPriority

List<QPriority> messageQueue = new List<QPriority>(); 



如下添加您的消息和优先级



Add your message and priority as following

messageQueue.Add(new QPriority(priority.higher, "message 1"));
messageQueue.Add(new QPriority(priority.lower, "message 2"));
messageQueue.Add(new QPriority(priority.lower , "message 3"));
messageQueue.Add(new QPriority(priority.medium , "message 4"));
messageQueue.Add(new QPriority(priority.higher , "message 5"));
messageQueue.Add(new QPriority(priority.lower , "message 6"));
messageQueue.Add(new QPriority(priority.lower, "message 7"));
messageQueue.Add(new QPriority(priority.medium, "message 8"));



然后声明一个函数,以根据分配的优先级进行排序



then declare a function to implement sorting according to the priorities assigned

public  List<string> sortQ(List<QPriority> queueMessage)
        {
            List<string> higherQ = new List<string>();
            List<string> mediumQ = new List<string>();
            List<string> lowerQ = new List<string>();
            List<string> finalQ = new List<string>();
            foreach (QPriority qp in queueMessage)
            {
                if (qp.messagePriority  == priority.higher)
                    higherQ.Add(qp.myMessage );
                else if (qp.messagePriority == priority.medium)
                    mediumQ.Add(qp.myMessage);
                else if (qp.messagePriority == priority.lower)
                    lowerQ.Add(qp.myMessage);
            }
            finalQ.AddRange(higherQ);
            finalQ.AddRange(mediumQ);
            finalQ.AddRange(lowerQ);
            return finalQ;

        }



现在通过传递messageQueue作为参数来调用sortQ函数,并将返回的列表捕获为



now call the sortQ function by passing messageQueue as parameter and catch the returned list as

List<string> returnedQ = sortQ(messageQueue);



现在,每发送完一条消息,就会放慢消息的速度



now come the part of slowing the messages after each message has been sent

int sleepCount = 0;//intialise to zero
            int maxSleepCount = 3000;// maximum time elapse after it again the time elapse will be zero
            List<string> returnedQ = sortQ(messageQueue);
            foreach (string str in returnedQ)
            {
                //put your message sending code here
                sleepCount += 1000;//change the value to your convinience but remember the value is in milliseconds
                if (sleepCount == maxSleepCount)
                {
                    sleepCount = 0;
                }
                Thread.Sleep(sleepCount);
                
            }


这篇关于为我的tcp客户端创建消息缓冲区或sendq的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆