复制缓冲区出错

时间:2013-12-17 22:21:26

标签: c# tcp

我有以下错误Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.过了一段时间我的客户端连接并向我发送数据包。我使用此代码作为我的网络核心:http://www.codeproject.com/Articles/83102/C-SocketAsyncEventArgs-High-Performance-Socket-Cod我将MessageHandler.cs修改为:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;

namespace PTEmu.NetworkCore
{
    class MessageHandler
    {

        public bool HandleMessage(SocketAsyncEventArgs receiveSendEventArgs, DataHoldingUserToken receiveSendToken, Int32 remainingBytesToProcess)
        {
            bool incomingTcpMessageIsReady = false;

            //Create the array where we'll store the complete message, 
            //if it has not been created on a previous receive op.
            if (receiveSendToken.receivedMessageBytesDoneCount == 0)
            {
                receiveSendToken.GetDataHolder().dataMessageReceived = new Byte[receiveSendToken.lengthOfCurrentIncomingMessage];
            }

            // Remember there is a receiveSendToken.receivedPrefixBytesDoneCount
            // variable, which allowed us to handle the prefix even when it
            // requires multiple receive ops. In the same way, we have a 
            // receiveSendToken.receivedMessageBytesDoneCount variable, which
            // helps us handle message data, whether it requires one receive
            // operation or many.
            if (remainingBytesToProcess + receiveSendToken.receivedMessageBytesDoneCount + 4 == receiveSendToken.lengthOfCurrentIncomingMessage)
            {
                // If we are inside this if-statement, then we got 
                // the end of the message. In other words,
                // the total number of bytes we received for this message matched the 
                // message length value that we got from the prefix.

                // Write/append the bytes received to the byte array in the 
                // DataHolder object that we are using to store our data.
                Buffer.BlockCopy(receiveSendEventArgs.Buffer, 
                    receiveSendToken.receiveMessageOffset - 4,  // I want the length of the packet in the final MessagePacket aswell
                    receiveSendToken.GetDataHolder().dataMessageReceived, 
                    receiveSendToken.receivedMessageBytesDoneCount, 
                    remainingBytesToProcess);

                incomingTcpMessageIsReady = true;
            }

            else
            {
                // If we are inside this else-statement, then that means that we
                // need another receive op. We still haven't got the whole message,
                // even though we have examined all the data that was received.
                // Not a problem. In SocketListener.ProcessReceive we will just call
                // StartReceive to do another receive op to receive more data.


                Buffer.BlockCopy(receiveSendEventArgs.Buffer, 
                    receiveSendToken.receiveMessageOffset,
                    receiveSendToken.GetDataHolder().dataMessageReceived, 
                    receiveSendToken.receivedMessageBytesDoneCount, 
                    remainingBytesToProcess); // THE ERROR IS HERE.

                receiveSendToken.receiveMessageOffset = receiveSendToken.receiveMessageOffset - receiveSendToken.recPrefixBytesDoneThisOp;

                receiveSendToken.receivedMessageBytesDoneCount += remainingBytesToProcess;
            }

            return incomingTcpMessageIsReady;
        }
    }
}

我添加了一些方法来获取DataHoldingUserToken中的变量:

public DataHolder GetDataHolder() { return theDataHolder; }
public Mediator GetMediator() { return theMediator; }

错误发生在else语句中的Buffer.BlockCopy中。如果您需要更多代码,请告诉我,我不知道错误的确切位置,我在tcp / ip方面不太好。

0 个答案:

没有答案