Author | Message | Time |
---|---|---|
Sargera | Has anyone used the STL queue for their bot queue? Also, if anyone could be as so kind to, please post the source code to your queue implementation. I'd like to see some examples so I can compare it to mine which I have in the works. Thanks! :) | June 23, 2004, 7:53 AM |
Myndfyr | [quote author=Sargera link=board=17;threadid=7400;start=0#msg66772 date=1087977232] Has anyone used the STL queue for their bot queue? Also, if anyone could be as so kind to, please post the source code to your queue implementation. I'd like to see some examples so I can compare it to mine which I have in the works. Thanks! :) [/quote] Queues are not hard to implement, particularly if you figure that it's just a linked list. The fact that you don't need to implement the insert method makes it even easier. Let's say your Bnet packet structure is defined: [code] typedef struct tagBNET_MESSAGE { // ... stuff } BNET_MESSAGE, *PBNET_MESSAGE; [/code] Header: [code] typedef struct tagMSG_NODE { PBNET_MESSAGE msg; // this struct provides for a singly-linked list. a doubly-linked list would be more efficient, especially for remove operations // that occur at the end of the list. HOWEVER, since we don't need to worry about that (all remove operations will occur // at the beginning of the list), a singly-linked list will suffice for this example. tagMSG_NODE *next; } MSG_NODE, *PMSG_NODE; public class BnetMsgList { protected: int m_nCount; PMSG_NODE m_pHead, m_pTail; public: bool add(PBNET_MESSAGE msg); bool get(int index, PBNET_MESSAGE msg); // msg is an [Out] parameter. bool removeAt(int index); inline int getCount(); } public class BnetMsgQueue { protected: BnetMsgList *m_pMsgList; public: bool enqueue(PBNET_MESSAGE msg); bool dequeue(PBNET_MESSAGE msg); // msg in an [Out] parameter. inline bool hasItems(); } [/code] Implementation (off the top of my head. I don't claim to be a C++ programmer and this is untested. Tell me how it comes out if anyone tries it ;) ): [code] bool BnetMsgList::add(PBNET_MESSAGE msg) { PMSG_NODE pNewNode; pNewNode->msg = msg; if (pHead == NULL) { pHead = pTail = pNewNode; } else { pTail->next = pNewNode; pTail = pNewNode; } m_nCount++; return true; } bool BnetMsgList::get(int index, PBNET_MESSAGE msg) { if ((index >= m_nCount) || (pHead == NULL)) return false; else { PMSG_NODE pCursor; int nIdx = 0; do { if (pCursor == NULL) pCursor = pHead; else pCursor = pCursor->next; if (nIdx == index) msg = pCursor->msg; } while (pCursor != NULL); } if (msg != NULL) return true; } bool BnetMsgList::removeAt(int index) { bool bRemoved = false; if (pHead != NULL) { if (index == 0) { PMSG_NODE pCursor = pHead; pHead = pHead->next; delete pCursor; bRemoved = true; m_nCount--; } else { PMSG_NODE pPrev, pCur; pCur = pHead->next; pPrev = pHead; if (pCur != NULL) { int nIdx = 1; do { if (nIdx == index) { pPrev->next = pCur->next; delete pCur; bRemoved = true; m_nCount--; break; } else { pPrev = pCur; pCur = pCur->next; } } while (pCur != NULL); } } } return bRemoved; } inline int BnetMsgList::getCount() { return m_nCount; } bool BnetMsgQueue::enqueue(PBNET_MESSAGE msg) { return m_pMsgList->add(msg); } bool BnetMsgQueue::dequeue(PBNET_MESSAGE msg) // out { bool bSuccess = m_pMsgList->get(0, msg); if (bSuccess) m_pMsgList->removeAt(0); return bSuccess; } inline bool BnetMsgQueue::hasItems() { return (m_pMsgList->getCount() > 0); } [/code] Anyway, that's what I came up with. List I said, I'm not a C++ programmer, and this is untested. I am definitely interested to know what others came up with or how this turns out (cuz I don't plan on testing it, either). Cheers. | June 23, 2004, 8:52 AM |
Sargera | Thanks Myndfyre, but I don't think I'll be using that. I'd prefer to use the queue provided in the STL if possible for just cleaner code, and better design. I don't care too much about customizing the queue for a specific thing, as it's one of the least important things about the bot IMO. However, I'd like to know if it's possible to set delays on when elements are processed in the queue (messages). Since the queue is obviously designed for speed (one of the major notions of the STL) it's going to send those messages I insert into the queue as fast as possible to Battle.net, which comes at a cost of an IPBan which is what we're trying to avoid in the queue system (among other things). Bottom line question: How do you set delays in the queue provided in the STL? | June 24, 2004, 5:58 AM |
Myndfyr | If you're using Windows, you want to set up your bot to respond to the WM_TIMER message and set up timers for how often to dequeue a packet. Within the timer callback, send. | June 24, 2004, 7:14 AM |