예제 #1
0
        private void replyRequest(PackageBuffer pkgBuffer)
        {
            for (int i = 0; i < request_queue.Count; ++i)
            {
                int id    = request_queue[i];
                int index = -1;
                for (int j = 0; j < history_queue.Count; ++j)
                {
                    if (id < history_queue[j].id)
                    {
                        break;
                    }
                    else if (id == history_queue[j].id)
                    {
                        index = j;
                        break;
                    }
                }
                if (index == -1)    // already expired

                {
                }
                else
                {
                    packMessage(history_queue[index], pkgBuffer);
                }
            }

            request_queue.Clear();
        }
예제 #2
0
 private void packHeartbeat(PackageBuffer pkgBuffer)
 {
     if (pkgBuffer.len == PackageBuffer.PACKAGE_LEN)
     {
         pkg_queue.Add(pkgBuffer);
         pkgBuffer = new PackageBuffer();
     }
     pkgBuffer.buffer[pkgBuffer.len++] = (byte)TYPE_HEARTBEAT;
 }
예제 #3
0
        private void sendMessages(PackageBuffer pkgBuffer)
        {
            for (int i = 0; i < send_queue.Count; ++i)
            {
                packMessage(send_queue[i], pkgBuffer);
            }

            history_queue.AddRange(send_queue);
            send_queue.Clear();
        }
예제 #4
0
        private void packRequest(PackageBuffer pkgBuffer, int id)
        {
            if ((PackageBuffer.PACKAGE_LEN - pkgBuffer.len) < 3)
            {
                pkg_queue.Add(pkgBuffer);
                pkgBuffer = new PackageBuffer();
            }

            pkgBuffer.len = fillHeader(pkgBuffer.buffer, pkgBuffer.len, TYPE_REQUEST, id);
        }
예제 #5
0
        private void genPackages()
        {
            PackageBuffer pkgBuffer = new PackageBuffer();

            requestMissing(pkgBuffer);
            replyRequest(pkgBuffer);
            sendMessages(pkgBuffer);

            if (pkgBuffer.len == 0)
            {
                packHeartbeat(pkgBuffer);
            }

            pkg_queue.Add(pkgBuffer);
        }
예제 #6
0
        private void packMessage(Message msg, PackageBuffer pkgBuffer)
        {
            int lenData    = msg.data.Length;
            int lenPackage = (lenData < 128) ? lenData + 3 : lenData + 4;

            if ((PackageBuffer.PACKAGE_LEN - pkgBuffer.len) < lenPackage)
            {
                pkg_queue.Add(pkgBuffer);
                pkgBuffer = new PackageBuffer();
            }

            pkgBuffer.len = fillHeader(pkgBuffer.buffer, pkgBuffer.len, lenData + TYPE_DATA, msg.id);
            Array.Copy(msg.data, 0, pkgBuffer.buffer, pkgBuffer.len, lenData);
            pkgBuffer.len += lenData;
        }
예제 #7
0
        private void requestMissing(PackageBuffer buffer)
        {
            int id = recv_id_min;

            for (int i = 0; i < recv_queue.Count; ++i)
            {
                if (recv_queue[i].id > id)
                {
                    for (int j = id; j < recv_queue[i].id; j++)
                    {
                        packRequest(buffer, j);
                    }
                }
                id = recv_queue[i].id + 1;
            }
        }