internal SocketAsyncEventArgs GiveBack() { if (Program.watchProgramFlow == true) //for testing { receiveSendToken = (DataHoldingUserToken)this.saeaObject.UserToken; Program.testWriter.WriteLine("Mediator GiveBack() " + receiveSendToken.TokenId); } return(saeaObject); }
internal void HandleData(DataHolder incomingDataHolder) { if (Program.watchProgramFlow == true) //for testing { receiveSendToken = (DataHoldingUserToken)this.saeaObject.UserToken; Program.testWriter.WriteLine("Mediator HandleData() " + receiveSendToken.TokenId); } theDataHolder = theIncomingDataPreparer.HandleReceivedData(incomingDataHolder, this.saeaObject); }
internal void PrepareOutgoingData() { if (Program.watchProgramFlow == true) //for testing { receiveSendToken = (DataHoldingUserToken)this.saeaObject.UserToken; Program.testWriter.WriteLine("Mediator PrepareOutgoingData() " + receiveSendToken.TokenId); } theOutgoingDataPreparer.PrepareOutgoingData(saeaObject, theDataHolder); }
internal void PrepareOutgoingData(SocketAsyncEventArgs e, DataHolder handledDataHolder) { DataHoldingUserToken theUserToken = (DataHoldingUserToken)e.UserToken; if (Program.watchProgramFlow == true) //for testing { Program.testWriter.WriteLine("Mediator PrepareOutgoingData() " + theUserToken.TokenId); } theDataHolder = handledDataHolder; //In this example code, we will send back the receivedTransMissionId, // followed by the //message that the client sent to the server. And we must //prefix it with the length of the message. So we put 3 //things into the array. // 1) prefix, // 2) receivedTransMissionId, // 3) the message that we received from the client, which // we stored in our DataHolder until we needed it. //That is our communication protocol. The client must know the protocol. //Convert the receivedTransMissionId to byte array. Byte[] idByteArray = BitConverter.GetBytes(theDataHolder.receivedTransMissionId); //Determine the length of all the data that we will send back. Int32 lengthOfCurrentOutgoingMessage = idByteArray.Length + theDataHolder.dataMessageReceived.Length; //So, now we convert the length integer into a byte array. //Aren't byte arrays wonderful? Maybe you'll dream about byte arrays tonight! Byte[] arrayOfBytesInPrefix = BitConverter.GetBytes(lengthOfCurrentOutgoingMessage); //Create the byte array to send. theUserToken.dataToSend = new Byte[theUserToken.sendPrefixLength + lengthOfCurrentOutgoingMessage]; //Now copy the 3 things to the theUserToken.dataToSend. Buffer.BlockCopy(arrayOfBytesInPrefix, 0, theUserToken.dataToSend, 0, theUserToken.sendPrefixLength); Buffer.BlockCopy(idByteArray, 0, theUserToken.dataToSend, theUserToken.sendPrefixLength, idByteArray.Length); //The message that the client sent is already in a byte array, in DataHolder. Buffer.BlockCopy(theDataHolder.dataMessageReceived, 0, theUserToken.dataToSend, theUserToken.sendPrefixLength + idByteArray.Length, theDataHolder.dataMessageReceived.Length); theUserToken.sendBytesRemainingCount = theUserToken.sendPrefixLength + lengthOfCurrentOutgoingMessage; theUserToken.bytesSentAlreadyCount = 0; }
public void SendBufferSetter(SocketAsyncEventArgs e) { //Let's send back the receivedTransMissionId, followed by the //message that the client sent to the server. And we have to //prefix it with the length of the message. So we have to put 3 //things into the array. // 1) first prefix that tells the length // 2) then the receivedTransMissionId // 3) and last the message that we received from the client, which // we have stored in our trusty DataHolder until we needed it. //That is our data protocol. //Convert the receivedTransMissionId to byte array. Byte[] idByteArray = BitConverter.GetBytes(theDataHolder.receivedTransMissionId); //Determine the length of all the data that we will send back. Int32 lengthOfArrayToSend = Program.receivePrefixLength + idByteArray.Length + theDataHolder.dataMessageReceived.Length; //So, now we convert the integer which tells the length into a byte array. //Aren't byte arrays wonderful? Maybe you'll dream about byte arrays tonight Byte[] arrayOfBytesInPrefix = BitConverter.GetBytes(lengthOfArrayToSend - Program.receivePrefixLength); //Create the byte array to send. Byte[] arrayOfBytesToSend = new Byte[lengthOfArrayToSend]; //Now copy the 3 things to the arrayOfBytesToSend. Buffer.BlockCopy(arrayOfBytesInPrefix, 0, arrayOfBytesToSend, 0, Program.receivePrefixLength); Buffer.BlockCopy(idByteArray, 0, arrayOfBytesToSend, Program.receivePrefixLength, idByteArray.Length); //The message that the client sent is already in a byte array, in DataHolder. Buffer.BlockCopy(theDataHolder.dataMessageReceived, 0, arrayOfBytesToSend, Program.receivePrefixLength + idByteArray.Length, theDataHolder.dataMessageReceived.Length); // Great! Now tell SocketAsyncEventArgs object to send this byte array. //Your client will have to know the data protocol, in order to separate //the receivedTransMissionId from the message that we are echoing back, //because the message is ASCII but the receivedTransMissionId is binary. DataHoldingUserToken theUserToken = (DataHoldingUserToken)e.UserToken; Console.WriteLine("offset for send in DP = " + theUserToken.sendBufferOffset); Buffer.BlockCopy(arrayOfBytesToSend, 0, e.Buffer, theUserToken.sendBufferOffset, lengthOfArrayToSend); e.SetBuffer(e.Buffer, theUserToken.sendBufferOffset, lengthOfArrayToSend); }
internal DataHolder HandleReceivedData(DataHolder incomingDataHolder, SocketAsyncEventArgs theSaeaObject) { DataHoldingUserToken receiveToken = (DataHoldingUserToken)theSaeaObject.UserToken; if (Program.watchProgramFlow == true) //for testing { Program.testWriter.WriteLine("HandleReceivedData() " + receiveToken.TokenId); } theDataHolder = incomingDataHolder; theDataHolder.sessionId = receiveToken.SessionId; theDataHolder.receivedTransMissionId = this.ReceivedTransMissionIdGetter(); theDataHolder.remoteEndpoint = this.GetRemoteEndpoint(); if ((Program.watchData == true) & (Program.runLongTest == false)) { this.AddDataHolder(); } return(theDataHolder); }
public Int32 HandlePrefix(SocketAsyncEventArgs e, DataHoldingUserToken receiveSendToken, Int32 remainingBytesToProcess) { //receivedPrefixBytesDoneCount tells us how many prefix bytes were //processed during previous receive ops which contained data for //this message. Usually there will NOT have been any previous //receive ops here. So in that case, //receiveSendToken.receivedPrefixBytesDoneCount would equal 0. //Create a byte array to put the new prefix in, if we have not //already done it in a previous loop. if (receiveSendToken.receivedPrefixBytesDoneCount == 0) { if (Program.watchProgramFlow == true) //for testing { Program.testWriter.WriteLine("PrefixHandler, create prefix array " + receiveSendToken.TokenId); } receiveSendToken.byteArrayForPrefix = new Byte[receiveSendToken.receivePrefixLength]; } // If this next if-statement is true, then we have received >= // enough bytes to have the prefix. So we can determine the // length of the message that we are working on. if (remainingBytesToProcess >= receiveSendToken.receivePrefixLength - receiveSendToken.receivedPrefixBytesDoneCount) { if (Program.watchProgramFlow == true) //for testing { Program.testWriter.WriteLine("PrefixHandler, enough for prefix " + receiveSendToken.TokenId + ". remainingBytesToProcess = " + remainingBytesToProcess); } //Now copy that many bytes to byteArrayForPrefix. //We can use the variable receiveMessageOffset as our main //index to show which index to get data from in the TCP //buffer. Buffer.BlockCopy(e.Buffer, receiveSendToken.receiveMessageOffset - receiveSendToken.receivePrefixLength + receiveSendToken.receivedPrefixBytesDoneCount, receiveSendToken.byteArrayForPrefix, receiveSendToken.receivedPrefixBytesDoneCount, receiveSendToken.receivePrefixLength - receiveSendToken.receivedPrefixBytesDoneCount); remainingBytesToProcess = remainingBytesToProcess - receiveSendToken.receivePrefixLength + receiveSendToken.receivedPrefixBytesDoneCount; receiveSendToken.recPrefixBytesDoneThisOp = receiveSendToken.receivePrefixLength - receiveSendToken.receivedPrefixBytesDoneCount; receiveSendToken.receivedPrefixBytesDoneCount = receiveSendToken.receivePrefixLength; receiveSendToken.lengthOfCurrentIncomingMessage = BitConverter.ToInt32(receiveSendToken.byteArrayForPrefix, 0); if (Program.watchData == true) { //Now see what integer the prefix bytes represent, for the length. StringBuilder sb = new StringBuilder(receiveSendToken.byteArrayForPrefix.Length); sb.Append(" Token id " + receiveSendToken.TokenId + ". " + receiveSendToken.receivePrefixLength + " bytes in prefix:"); foreach (byte theByte in receiveSendToken.byteArrayForPrefix) { sb.Append(" " + theByte.ToString()); } sb.Append(". Message length: " + receiveSendToken.lengthOfCurrentIncomingMessage); Program.testWriter.WriteLine(sb.ToString()); } } //This next else-statement deals with the situation //where we have some bytes //of this prefix in this receive operation, but not all. else { if (Program.watchProgramFlow == true) //for testing { Program.testWriter.WriteLine("PrefixHandler, NOT all of prefix " + receiveSendToken.TokenId + ". remainingBytesToProcess = " + remainingBytesToProcess); } //Write the bytes to the array where we are putting the //prefix data, to save for the next loop. Buffer.BlockCopy(e.Buffer, receiveSendToken.receiveMessageOffset - receiveSendToken.receivePrefixLength + receiveSendToken.receivedPrefixBytesDoneCount, receiveSendToken.byteArrayForPrefix, receiveSendToken.receivedPrefixBytesDoneCount, remainingBytesToProcess); receiveSendToken.recPrefixBytesDoneThisOp = remainingBytesToProcess; receiveSendToken.receivedPrefixBytesDoneCount += remainingBytesToProcess; remainingBytesToProcess = 0; } // This section is needed when we have received // an amount of data exactly equal to the amount needed for the prefix, // but no more. And also needed with the situation where we have received // less than the amount of data needed for prefix. if (remainingBytesToProcess == 0) { receiveSendToken.receiveMessageOffset = receiveSendToken.receiveMessageOffset - receiveSendToken.recPrefixBytesDoneThisOp; receiveSendToken.recPrefixBytesDoneThisOp = 0; } return(remainingBytesToProcess); }
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) { if (Program.watchProgramFlow == true) //for testing { Program.testWriter.WriteLine("MessageHandler, creating receive array " + receiveSendToken.TokenId); } receiveSendToken.theDataHolder.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 == 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. if (Program.watchProgramFlow == true) //for testing { Program.testWriter.WriteLine("MessageHandler, length is right for " + receiveSendToken.TokenId); } // 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, receiveSendToken.theDataHolder.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. if (Program.watchProgramFlow == true) //for testing { Program.testWriter.WriteLine(" MessageHandler, length is short for " + receiveSendToken.TokenId); } Buffer.BlockCopy(receiveSendEventArgs.Buffer, receiveSendToken.receiveMessageOffset, receiveSendToken.theDataHolder.dataMessageReceived, receiveSendToken.receivedMessageBytesDoneCount, remainingBytesToProcess); receiveSendToken.receiveMessageOffset = receiveSendToken.receiveMessageOffset - receiveSendToken.recPrefixBytesDoneThisOp; receiveSendToken.receivedMessageBytesDoneCount += remainingBytesToProcess; } return(incomingTcpMessageIsReady); }