Exemple #1
0
 //____________________________________________________________________________
 private void ProcessDisconnectAndCloseSocket()
 {
     token.Reset();
 }
Exemple #2
0
        /// <summary>
        /// If buffer still has data after call this function, it will return true.
        /// </summary>
        /// <param name="receiveSendEventArgs"></param>
        /// <param name="receiveSendToken"></param>
        /// <param name="remainingBytesToProcess"></param>
        /// <returns></returns>
        private bool ReceiveOneMessage(byte[] buffer, DataHoldingUserToken receiveSendToken,
                                       ref int remainingBytesToProcess)
        {
            // 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.state == DataHoldingUserToken.State.Prefix)
            {
                remainingBytesToProcess = prefixHandler.HandlePrefix(buffer, receiveSendToken,
                                                                     remainingBytesToProcess);

                if (remainingBytesToProcess == 0)
                {
                    //Jump out of the method, since there is no more data.
                    return(false);
                }
            }

            if (receiveSendToken.state == DataHoldingUserToken.State.Error)
            {
                StartDisconnect();
            }

            // 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(buffer, receiveSendToken,
                                                                          ref remainingBytesToProcess);

            if (incomingTcpMessageIsReady)
            {
                // Process the data received, this function should use the data in dataReceived, and Deserialize the data
                // then the dataReceived will be reused  to receive other data.
                //OnMessageRecieved(receiveSendToken.SessionId, receiveSendToken.dataReceived);
                byte[] b;
                int    length = 0;
                if (receiveSendToken.currentMessageCompressed)
                {
                    try
                    {
                        length = LZ4Codec.Decode32(receiveSendToken.dataReceived.GetBuffer(), 0, (int)receiveSendToken.dataReceived.Length, mDecodeBuffer, 0, mDecodeBuffer.Length, false);
                        b      = mDecodeBuffer;
                    }
                    catch (Exception ex)
                    {
                        Logger.Error("Message can not unwrap, this may caused by a hacker's attack." + ex.ToString());
                        b = null;
                    }
                }
                else
                {
                    b      = receiveSendToken.dataReceived.GetBuffer();
                    length = (int)receiveSendToken.dataReceived.Length;
                }

                if (b != null)
                {
                    using (var ms = new MemoryStream(b, 0, length))
                    {
                        var message = SerializerUtility.Deserialize(ms);
                        receiveSendToken.Reset();

                        if (message.Type == (int)MessageType.Ping)
                        {
                            latency = DateTime.Now - DateTime.FromBinary((long)message.ClientId);
                        }
                        else
                        {
                            if (MessageReceived != null)
                            {
                                MessageReceived(message);
                            }
                        }
                    }
                }

                // continue to process another message which is not completed.
                if (remainingBytesToProcess > 0)
                {
                    return(true);
                }

                return(false);
            }
            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;
                return(false);
            }
        }