private string ParseNoMoreMessages(string inputLine, string command, IEnumerator token) { UnityEngine.Debug.Log("OCMessageHandler::ParseNoMoreMessages"); string answer = null; if (_state == READING_MESSAGES) { UnityEngine.Debug.Log("onLine: From [" + _messageFrom + "] to [" + _messageTo + "] Type [" + _messageType + "]: " + _message.ToString()); OCMessage message = OCMessage.CreateMessage(_messageFrom, _messageTo, _messageType, _message.ToString()); if (message == null) { UnityEngine.Debug.Log("Could not factory message from the following string: [" + _message.ToString() + "]"); } if (_useMessageBuffer) { UnityEngine.Debug.Log("Using message buffer..."); _messageBuffer.Add(message); _networkElement.PullMessage(_messageBuffer); _messageBuffer.Clear(); } else { UnityEngine.Debug.Log("Not using message buffer...pulling instead..."); _networkElement.PullMessage(message); } // reset variables to default values _lineCount = 0; _messageTo = ""; _messageFrom = ""; _messageType = OCMessage.MessageType.NONE; _message.Remove(0, _message.Length); _state = DOING_NOTHING; // quit reading state answer = OCNetworkElement.OK_MESSAGE; } else { UnityEngine.Debug.Log("onLine: Unexpected command [" + command + "]. Discarding line [" + inputLine + "]"); answer = OCNetworkElement.FAILED_MESSAGE; } return(answer); }
/// <summary> /// Parse a text line from message received. /// </summary> /// <param name="inputLine"> /// The raw data that received by server socket. /// </param> /// <returns> /// An 'OK' string if the line was successfully parsed, /// a 'FAILED' string if something went wrong, /// null if there is still more to parse. /// </returns> public string parse(string inputLine) { // UnityEngine.Debug.Log("OldMessageHandler.parse(" + inputLine + ")"); string answer = null; char selector = inputLine[0]; string contents = inputLine.Substring(1); if (selector == 'c') { string[] tokenArr = contents.Split(' '); IEnumerator token = tokenArr.GetEnumerator(); token.MoveNext(); string command = token.Current.ToString(); if (command.Equals("NOTIFY_NEW_MESSAGE")) { UnityEngine.Debug.Log(OCLogSymbol.CONNECTION + "NOTIFY_NEW_MESSAGE!"); if (token.MoveNext()) // Has more elements { // Get new message number. int numberOfMessages = int.Parse(token.Current.ToString()); this.ne.NotifyNewMessages(numberOfMessages); answer = OCNetworkElement.OK_MESSAGE; UnityEngine.Debug.Log(OCLogSymbol.CONNECTION + "Notified about [" + numberOfMessages + "] messages in Router."); } else { answer = OCNetworkElement.FAILED_MESSAGE; } } else if (command.Equals("UNAVAILABLE_ELEMENT")) { if (token.MoveNext()) // Has more elements { // Get unavalable element id. string id = token.Current.ToString(); System.Console.WriteLine(OCLogSymbol.DETAILEDINFO + "Unavailable element message received for [" + id + "]."); this.ne.MarkAsUnavailable(id); answer = OCNetworkElement.OK_MESSAGE; } else { answer = OCNetworkElement.FAILED_MESSAGE; } } else if (command.Equals("AVAILABLE_ELEMENT")) { if (token.MoveNext()) // Has more elements { string id = token.Current.ToString(); UnityEngine.Debug.Log(OCLogSymbol.CONNECTION + "Available element message received for [" + id + "]."); this.ne.MarkAsAvailable(id); answer = OCNetworkElement.OK_MESSAGE; } else { answer = OCNetworkElement.FAILED_MESSAGE; } } else if (command.Equals("START_MESSAGE")) // Parse a common message { if (this.state == READING_MESSAGES) { // A previous message was already read. UnityEngine.Debug.Log(OCLogSymbol.CONNECTION + "START_MESSAGE: From [" + this.currentMessageFrom + "] to [" + this.currentMessageTo + "] Type [" + this.currentMessageType + "]"); OCMessage message = OCMessage.CreateMessage(this.currentMessageFrom, this.currentMessageTo, this.currentMessageType, this.currentMessage.ToString()); if (message == null) { UnityEngine.Debug.LogError(OCLogSymbol.ERROR + "Could not factory message from the following string: " + this.currentMessage.ToString()); } if (this.useMessageBuffer) { this.messageBuffer.Add(message); if (messageBuffer.Count > this.maxMessagesInBuffer) { this.ne.PullMessage(this.messageBuffer); this.messageBuffer.Clear(); } } else { this.ne.PullMessage(message); } this.lineCount = 0; this.currentMessageTo = ""; this.currentMessageFrom = ""; this.currentMessageType = OCMessage.MessageType.NONE; this.currentMessage.Remove(0, this.currentMessage.Length); } else { if (this.state == DOING_NOTHING) { // Enter reading state from idle state. this.state = READING_MESSAGES; } else { UnityEngine.Debug.LogError(OCLogSymbol.ERROR + "Unexepcted command [" + command + "]. Discarding line [" + inputLine + "]"); } } if (token.MoveNext()) { this.currentMessageFrom = token.Current.ToString(); if (token.MoveNext()) { this.currentMessageTo = token.Current.ToString(); if (token.MoveNext()) { this.currentMessageType = (OCMessage.MessageType) int.Parse(token.Current.ToString()); } else { answer = OCNetworkElement.FAILED_MESSAGE; } } else { answer = OCNetworkElement.FAILED_MESSAGE; } } else { answer = OCNetworkElement.FAILED_MESSAGE; } this.lineCount = 0; } else if (command.Equals("NO_MORE_MESSAGES")) { if (this.state == READING_MESSAGES) { // UnityEngine.Debug.Log("onLine (NO_MORE_LINES_IN_CURRENT_MESSAGE): From [" + this.currentMessageFrom + // "] to [" + this.currentMessageTo + // "] Type [" + this.currentMessageType + "]: " + this.currentMessage.ToString()); OCMessage message = OCMessage.CreateMessage(this.currentMessageFrom, this.currentMessageTo, this.currentMessageType, this.currentMessage.ToString()); if (message == null) { UnityEngine.Debug.LogError(OCLogSymbol.ERROR + "Could not factory message from the following string: [" + this.currentMessage.ToString() + "]"); } if (this.useMessageBuffer) { this.messageBuffer.Add(message); this.ne.PullMessage(messageBuffer); this.messageBuffer.Clear(); } else { this.ne.PullMessage(message); } // reset variables to default values this.lineCount = 0; this.currentMessageTo = ""; this.currentMessageFrom = ""; this.currentMessageType = OCMessage.MessageType.NONE; this.currentMessage.Remove(0, this.currentMessage.Length); this.state = DOING_NOTHING; // quit reading state answer = OCNetworkElement.OK_MESSAGE; } else { UnityEngine.Debug.LogError(OCLogSymbol.ERROR + "Unexpected command [" + command + "]. Discarding line [" + inputLine + "]"); answer = OCNetworkElement.FAILED_MESSAGE; } } else { UnityEngine.Debug.LogError(OCLogSymbol.ERROR + "onLine: Unexpected command [" + command + "]. Discarding line [" + inputLine + "]"); answer = OCNetworkElement.FAILED_MESSAGE; } // end processing command. } // end processing selector 'c' else if (selector == 'd') { if (this.state == READING_MESSAGES) { if (this.lineCount > 0) { this.currentMessage.Append("\n"); } this.currentMessage.Append(contents); this.lineCount++; } else { UnityEngine.Debug.LogError(OCLogSymbol.ERROR + "Unexpected dataline. Discarding line [" + inputLine + "]"); answer = OCNetworkElement.FAILED_MESSAGE; } } // end processing selector 'd' else { UnityEngine.Debug.LogError(OCLogSymbol.ERROR + "Invalid selector [" + selector + "]. Discarding line [" + inputLine + "]."); answer = OCNetworkElement.FAILED_MESSAGE; } // end processing selector return(answer); }
private string ParseStartMessage(string inputLine, string command, IEnumerator token) { UnityEngine.Debug.Log("OCMessageHandler::ParseStartMessage"); string answer = null; if (_state == READING_MESSAGES) { // A previous message was already read. OCLogger.Debugging("onLine: From [" + _messageFrom + "] to [" + _messageTo + "] Type [" + _messageType + "]"); OCMessage message = OCMessage.CreateMessage(_messageFrom, _messageTo, _messageType, _message.ToString()); if (message == null) { OCLogger.Error("Could not factory message from the following string: " + _message.ToString()); } if (_useMessageBuffer) { _messageBuffer.Add(message); if (_messageBuffer.Count > _maxMessagesInBuffer) { _networkElement.PullMessage(_messageBuffer); _messageBuffer.Clear(); } } else { _networkElement.PullMessage(message); } _lineCount = 0; _messageTo = ""; _messageFrom = ""; _messageType = OCMessage.MessageType.NONE; _message.Remove(0, _message.Length); } else { if (_state == DOING_NOTHING) { // Enter reading state from idle state. _state = READING_MESSAGES; } else { OCLogger.Error("onLine: Unexepcted command [" + command + "]. Discarding line [" + inputLine + "]"); } } if (token.MoveNext()) { _messageFrom = token.Current.ToString(); if (token.MoveNext()) { _messageTo = token.Current.ToString(); if (token.MoveNext()) { _messageType = (OCMessage.MessageType) int.Parse(token.Current.ToString()); } else { answer = OCNetworkElement.FAILED_MESSAGE; } } else { answer = OCNetworkElement.FAILED_MESSAGE; } } else { answer = OCNetworkElement.FAILED_MESSAGE; } _lineCount = 0; return(answer); }