Пример #1
0
        //____________________________________________________________________________
        private void ProcessSend(SocketAsyncEventArgs receiveSendEventArgs)
        {
            DataHoldingUserToken receiveSendToken = (DataHoldingUserToken)receiveSendEventArgs.UserToken;

            if (Program.watchProgramFlow == true)   //for testing
            {
                Program.testWriter.WriteLine("\r\nProcessSend, id = " + receiveSendToken.TokenId);
            }

            if (receiveSendEventArgs.SocketError == SocketError.Success)
            {
                receiveSendToken.sendBytesRemaining = receiveSendToken.sendBytesRemaining - receiveSendEventArgs.BytesTransferred;
                // If this if statement is true, then we have sent all of the
                // bytes in the message. Otherwise, at least one more send
                // operation will be required to send the data.
                if (receiveSendToken.sendBytesRemaining == 0)
                {
                    //increment total number of messages sent in this test
                    Interlocked.Increment(ref this.totalCountOfMessagesSent);

                    //incrementing count of messages sent on this connection
                    receiveSendToken.theDataHolder.NumberOfMessagesSent++;
                    StartReceive(receiveSendEventArgs);
                }
                else
                {
                    // So since (receiveSendToken.sendBytesRemaining == 0) is false,
                    // we have more bytes to send for this message. So we need to
                    // call StartSend, so we can post another send message.
                    receiveSendToken.bytesSentAlready += receiveSendEventArgs.BytesTransferred;
                    StartSend(receiveSendEventArgs);
                }
            }
            else
            {
                //If we are in this else-statement, there was a socket error.
                if (Program.watchProgramFlow == true)   //for testing
                {
                    Program.testWriter.WriteLine("ProcessSend ERROR, id " + receiveSendToken.TokenId + "\r\n");
                }
                else if (Program.writeErrorsToLog == true)
                {
                    Program.testWriter.WriteLine("ProcessSend ERROR, id " + receiveSendToken.TokenId);
                }

                // We'll just close the socket if there was a
                // socket error when receiving data from the client.
                receiveSendToken.Reset();
                StartDisconnect(receiveSendEventArgs);
            }
        }
Пример #2
0
        //____________________________________________________________________________
        private void ProcessReceive(SocketAsyncEventArgs receiveSendEventArgs)
        {
            DataHoldingUserToken receiveSendToken = (DataHoldingUserToken)receiveSendEventArgs.UserToken;

            // If there was a socket error, close the connection.
            if (receiveSendEventArgs.SocketError != SocketError.Success)
            {
                if (Program.watchProgramFlow == true)   //for testing
                {
                    Program.testWriter.WriteLine("ProcessReceive ERROR " + receiveSendEventArgs.SocketError.ToString() + ", id " + receiveSendToken.TokenId);
                }
                else if (Program.writeErrorsToLog == true)
                {
                    Program.testWriter.WriteLine("ProcessReceive ERROR " + receiveSendEventArgs.SocketError.ToString() + ", id " + receiveSendToken.TokenId);
                }

                receiveSendToken.Reset();
                StartDisconnect(receiveSendEventArgs);
                return;
            }

            //If no data was received, close the connection.
            if (receiveSendEventArgs.BytesTransferred == 0)
            {
                if (Program.watchProgramFlow == true)   //for testing
                {
                    Program.testWriter.WriteLine("ProcessReceive NO DATA, id " + receiveSendToken.TokenId);
                }
                receiveSendToken.Reset();
                StartDisconnect(receiveSendEventArgs);
                return;
            }

            if (Program.watchProgramFlow == true)   //for testing
            {
                Program.testWriter.WriteLine("\r\nProcessReceive, id " + receiveSendToken.TokenId);
            }


            Int32 remainingBytesToProcess = receiveSendEventArgs.BytesTransferred;

            if (Program.watchProgramFlow == true)   //for testing
            {
                Program.testWriter.WriteLine("ProcessReceive() if Success, id " + receiveSendToken.TokenId + ". Bytes read this op = " + receiveSendEventArgs.BytesTransferred + ".");
            }
            if (Program.watchData == true)
            {
                //This only gives us a readable string if it is operating on
                //string data.
                string tempString = Encoding.ASCII.GetString(receiveSendEventArgs.Buffer, receiveSendToken.bufferOffsetReceive, receiveSendEventArgs.BytesTransferred);

                Program.testWriter.WriteLine(receiveSendToken.TokenId + " data received = " + tempString);
            }


            // If we have not got all of the prefix then we need to work on it.
            // receivedPrefixBytesDoneCount tells us how many prefix bytes were
            // processed during previous receive ops which contained data for
            // this message. (In normal use, usually there will NOT have been any
            // previous receive ops here. So receivedPrefixBytesDoneCount would be 0.)
            if (receiveSendToken.receivedPrefixBytesDoneCount < this.socketClientSettings.ReceivePrefixLength)
            {
                remainingBytesToProcess = prefixHandler.HandlePrefix(receiveSendEventArgs, receiveSendToken, remainingBytesToProcess);

                if (remainingBytesToProcess == 0)
                {
                    // We need to do another receive op, since we do not have
                    // the message yet.
                    StartReceive(receiveSendEventArgs);

                    //Jump out of the method, since there is no more data.
                    return;
                }
            }

            // If we have processed the prefix, we can work on the message now.
            // We'll arrive here when we have received enough bytes to read
            // the first byte after the prefix.
            bool incomingTcpMessageIsReady = messageHandler.HandleMessage(receiveSendEventArgs, receiveSendToken, remainingBytesToProcess);

            if (incomingTcpMessageIsReady == true)
            {
                //In the design of our SocketClient used for testing the
                //DataHolder can contain data for multiple messages. That is
                //different from the server design, where we have one DataHolder
                //for one message.

                if (Program.watchData == true)
                {
                    string messageString = AssembleMessage(receiveSendToken);
                    Program.testWriter.WriteLine("id " + receiveSendToken.TokenId + ", data = " + messageString);
                }

                //If we have set runLongTest to true, then we will assume that
                //we cannot put the data in memory, because there would be too much
                //data. So we'll just skip writing that data, in that case. We
                //write it when runLongTest == false.
                if (Program.runLongTest == false)
                {
                    //Write to DataHolder.
                    receiveSendToken.theDataHolder.listOfMessagesReceived.Add(receiveSendToken.theDataHolder.dataMessageReceived);
                }

                //null out the byte array, for the next message
                receiveSendToken.theDataHolder.dataMessageReceived = null;

                //Reset the variables in the UserToken, to be ready for the
                //next message that will be received on the socket in this
                //SAEA object.
                receiveSendToken.Reset();

                //If we have not sent all the messages, get the next message, and
                //loop back to StartSend.
                if (receiveSendToken.theDataHolder.NumberOfMessagesSent < this.socketClientSettings.NumberOfMessages)
                {
                    //No need to reset the buffer for send here.
                    //It is reset in the StartSend method.
                    messagePreparer.GetDataToSend(receiveSendEventArgs);
                    StartSend(receiveSendEventArgs);
                }
                else
                {
                    //Since we have sent all the messages that we planned to send,
                    //time to disconnect.
                    StartDisconnect(receiveSendEventArgs);
                }
            }
            else
            {
                // Since we have NOT gotten enough bytes for the whole message,
                // we need to do another receive op. Reset some variables first.

                // All of the data that we receive in the next receive op will be
                // message. None of it will be prefix. So, we need to move the
                // receiveSendToken.receiveMessageOffset to the beginning of the
                // buffer space for this SAEA.
                receiveSendToken.receiveMessageOffset = receiveSendToken.bufferOffsetReceive;

                // Do NOT reset receiveSendToken.receivedPrefixBytesDoneCount here.
                // Just reset recPrefixBytesDoneThisOp.
                receiveSendToken.recPrefixBytesDoneThisOp = 0;

                StartReceive(receiveSendEventArgs);
            }
        }