QuakeGod
2022-10-17 8587c5cbb40fd60b37d762dfafa8fef683eb4fea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
  ******************************************************************************
  * @file           : MyQueue.c
  * @brief          : MyQueue program body
  ******************************************************************************
    */
#include "../inc/MyQueue.h"
 
#include "string.h"
    int initQueue(stMyQueue * theQueue, void * pBuf, int nSize)
    {
        theQueue->buf1=pBuf;
        theQueue->Caps=nSize;
        theQueue->wp=0;
        theQueue->rp=0;
        theQueue->RecvBytes=0;
        theQueue->bFull=0;
        theQueue->bEmpty=1;
        theQueue->state=0;        
            return 0;
    }
    int EmptyQueue(stMyQueue * theQueue)
    {
        theQueue->wp=0;
        theQueue->rp=0;
        theQueue->bFull=0;
        theQueue->bEmpty=1;
        theQueue->state=0;        
        return 0;
    }
    int GetContinueEmptyRoom(stMyQueue * theQueue)
    {
        int rp=theQueue->rp;
        int wp=theQueue->wp;
        if (wp < rp)
        {
            return rp - wp;
        }else if (wp > rp)
        {
            return theQueue->Caps - wp;
        }else if (IsEmpty(theQueue))
        {
            return theQueue->Caps - wp;
        }else if (IsFull(theQueue))
        {
            return 0;
        }
        return 0;
    }
    int GetContinueData(stMyQueue * theQueue)
    {
            int rp=theQueue->rp;
          int wp=theQueue->wp;    
        if (rp < wp)
        {
            return wp - rp;
        }else if (rp > wp)
        {
            return theQueue->Caps - rp;
        }else if (IsFull(theQueue))
        {
            return theQueue->Caps - rp;
        }else if (IsEmpty(theQueue))
        {
            return 0;
        }
        return 0;
    }
    
    int AddSpace(stMyQueue * theQueue, int nSize)
    {
        if (nSize==0) return nSize;
        int wp=theQueue->wp+=nSize;
        if (wp>=theQueue->Caps) wp-=theQueue->Caps;
        theQueue->bEmpty=0;
        if (wp == theQueue->rp) theQueue->bFull=1;
        theQueue->wp = wp;
        theQueue->RecvBytes+=nSize;
        return nSize;
    }
    
    int DelData(stMyQueue * theQueue, int nSize)
    {
        if (nSize==0) return nSize;
        int rp=theQueue->rp+nSize;
        if (rp>=theQueue->Caps) rp-=theQueue->Caps;
        theQueue->bFull=0;
        theQueue->rp = rp;
        if (rp == theQueue->wp)
        {            
            theQueue->bEmpty=1;
            theQueue->wp=0;
            theQueue->rp=0;
            theQueue->state=0;                    
        }
        return nSize;
    }
    
    int CopyData(stMyQueue * theQueue, void * pData, int nSize)
    {
        int len1=GetContinueData(theQueue);
            if (nSize <= len1) 
            {
                memcpy(pData,theQueue->buf1+theQueue->rp,nSize);
                return nSize;
            }
            else
            {
                memcpy(pData,theQueue->buf1+theQueue->rp,len1);
                memcpy((char *)pData+len1,theQueue->buf1,nSize-len1);
                return nSize;
            }    
        
    }
    
    
    int PushOne(stMyQueue * theQueue, unsigned char ch)
    {
        theQueue->bEmpty=0;    
        if (!IsFull(theQueue))
        {
            theQueue->buf1[theQueue->wp]=ch;
            AddSpace(theQueue,1);
            return 0;
        }
        return -1;
    }
    int PopOne(stMyQueue * theQueue)
    {
        theQueue->bFull=0;    
        unsigned char ch=theQueue->buf1[theQueue->rp];
        if (!IsEmpty(theQueue)) 
        {
            DelData(theQueue,1);
        }
        return ch;
    }
 
    int PushIn(stMyQueue * theQueue, void * pData, int nSize)
    {
        int len1=GetContinueEmptyRoom(theQueue);
        if (nSize <= len1) 
        {
            memcpy(GetWriteBuffer(theQueue),pData,nSize);
            AddSpace(theQueue, nSize);
            return nSize;
        }
 
            memcpy(GetWriteBuffer(theQueue),pData,len1);
            AddSpace(theQueue,len1);
            int len2=GetContinueEmptyRoom(theQueue);
            if (nSize-len1 <= len2) 
            {
                memcpy(GetWriteBuffer(theQueue),(char *)pData+len1,nSize-len1);
                AddSpace(theQueue, nSize-len1);
                return nSize;
            }
            if (len2>0)
            {
                memcpy(GetWriteBuffer(theQueue),(char *)pData+len1,len2);
                AddSpace(theQueue, len2);    
                len1+=len2;
            }
            return len1;
    }
    
    int PopOut(stMyQueue * theQueue, void * pData, int nSize)
    {
//        theQueue->rp+=nSize;
//        if (theQueue->rp >= theQueue ->Caps) theQueue->rp-=theQueue->Caps;
//        theQueue->DataSize-=nSize;
        CopyData(theQueue,pData,nSize);
        DelData(theQueue,nSize);
        return nSize;
    }