/// <summary> /// Asynchronously receives data from the Scala Agent server. This must /// be called after Communicator.Connect(). /// </summary> public void Receive() { //Debug.Log("Beginning receive thread."); //Console.WriteLine("Beginning receive thread."); Message message = new Message(); this.networkStream.BeginRead(message.Buffer, 0, Message.BufferSize, this.OnReceive, message); }
/// <summary> /// A blcoking receive. /// </summary> /// <returns>The received message.</returns> public string RecieveBlocking() { Message message = new Message(); int bytesRead; //Get the bytes! try { bytesRead = this.networkStream.Read(message.Buffer, 0, Message.BufferSize); //Debug.Log("Read " + bytesRead + " bytes from server."); //Console.WriteLine("Read " + bytesRead + " bytes from server."); } catch (ObjectDisposedException) { //No big deal, the other thread closed the socket. return null; } //TODO: Figure out why there's nonsense here and remove it, rather than filter here. List<byte> filter = new List<byte>(); //string received; for (int i = 0; i < bytesRead; i++) { if (message.Buffer[i] != 0) { //if (message.Buffer[i] == this.delimiter) { // //Store data received thus far. // if (filter.Count > 0) message.Builder.Append(UTF8Encoding.UTF8.GetString(filter.ToArray())); // received = message.Builder.ToString(); // this.Handle(received); // //Dump filter, and builder. // filter.Clear(); // message.Builder.Remove(0, message.Builder.Length); //} else { // filter.Add(message.Buffer[i]); //} filter.Add(message.Buffer[i]); } } return UTF8Encoding.UTF8.GetString(filter.ToArray()); }