/// <summary> /// Parses a single quote message from a reading file. /// </summary> /// <param name="r">the <c>BinaryReader</c> object reading the target file.</param> /// <returns>The parsed <c>QuoteMessage</c> object or null if all available objects have been read.</returns> private QuoteMessage ReadQuoteMessage() { try { QuoteMessage result = null; int length = IPAddress.NetworkToHostOrder(m_reader.ReadInt32()); ushort funcCode = Bytes.NetworkToHostOrder(m_reader.ReadUInt16()); int bodyLength = IPAddress.NetworkToHostOrder(m_reader.ReadInt32()); byte[] body = new byte[length]; int count = m_reader.Read(body, BidMessage.HeaderLength, bodyLength); if (count == bodyLength) { result = (QuoteMessage)BidMessage.Create(FunctionCodes.Quote, body, 0, length); } return(result); } catch (EndOfStreamException) { return(null); } }
/// <summary> /// Examines the entire buffer and parses all <c>QuoteMessage</c>s in it. /// </summary> private void ParseMessages() { while (m_writeIndex - m_readIndex > BidMessage.HeaderLength) { int length = 0; ushort funcCode = 0; int bodyLength = 0; for (; m_writeIndex - m_readIndex >= BidMessage.HeaderLength; m_readIndex++) { int offset = m_readIndex; length = m_buffer.ToInt32(offset); offset += sizeof(int); funcCode = m_buffer.ToUInt16(offset); offset += sizeof(ushort); bodyLength = m_buffer.ToInt32(offset); if (length - bodyLength == BidMessage.HeaderLength) { break; } } // loop guard is true iff the next message is successfully located // break if this fails or only part of next message is found if (m_writeIndex - m_readIndex < BidMessage.HeaderLength || m_writeIndex - m_readIndex < length) { break; } if (funcCode == (ushort)FunctionCodes.Quote) { QuoteMessage message = (QuoteMessage)BidMessage.Create(FunctionCodes.Quote, m_buffer, m_readIndex, length); OnQuoteMessageReceived(message); } m_readIndex += length; } // incomplete message if (m_readIndex != 0) { Array.Copy(m_buffer, m_readIndex, m_buffer, 0, m_writeIndex - m_readIndex); m_writeIndex -= m_readIndex; m_readIndex = 0; } }
/// <summary> /// Uses the credentials to authenticate with the remote server. /// Goes to states <c>Create</c>, <c>Receive</c>, and <c>Close</c>. /// </summary> /// <returns>Time to wait till next state is run, in milliseconds.</returns> private int Authenticate() { ChangeStatus(QuoteProviderStatus.Authenticate); try { SessionKeyRequestMessage sessionKeyRequest = new SessionKeyRequestMessage(m_username); int length = sessionKeyRequest.GetBytes(m_buffer, 0); int count = m_client.Send(m_buffer, 0, length, SocketFlags.None); count = m_client.Receive(m_buffer); length = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(m_buffer, 0)); SessionKeyReplyMessage sessionKeyReply = (SessionKeyReplyMessage)BidMessage.Create(FunctionCodes.SessionKeyReply, m_buffer, 0, length); uint sessionKey = sessionKeyReply.SessionKey; LoginRequestMessage LoginRequest = new LoginRequestMessage(m_username, m_password, sessionKey); length = LoginRequest.GetBytes(m_buffer, 0); count = m_client.Send(m_buffer, 0, length, SocketFlags.None); count = ReceiveMessage(); length = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(m_buffer, 0)); LoginReplyMessage loginReply = (LoginReplyMessage)BidMessage.Create(FunctionCodes.LoginReply, m_buffer, 0, length); m_maxHeartbeatInterval = loginReply.MaxHeartbeatInterval; if (m_maxHeartbeatInterval > 0) { m_retryTimes = 0; m_readIndex = m_writeIndex = 0; m_lastReception = m_lastHeartbeat = (uint)Environment.TickCount; m_runByState = Receive; } else { throw new Exception("Invalid login."); } } catch (SocketException se) { OnErrorOccurred(se, false); return(RetryOrGiveup(m_status)); } catch (Exception ex) { OnErrorOccurred(ex, true); m_runByState = Close; } return(0); }