public RecieveUserToken([NotNull] Socket socket, RecieveState initalState, [NotNull] TaskCompletionSource <TcpMessage> recieveTaskCompletionSource, [NotNull] AsyncSocketMessageTransport parent) { if (socket == null) { throw new ArgumentNullException("socket"); } if (recieveTaskCompletionSource == null) { throw new ArgumentNullException("recieveTaskCompletionSource"); } if (parent == null) { throw new ArgumentNullException("parent"); } Socket = socket; State = initalState; RecieveTaskCompletionSource = recieveTaskCompletionSource; Parent = parent; }
void Recieve() { while (true) { try { if (paused) { continue; } if (state != State.CONNECTED) { StateChangedEvent.WaitOne(); continue; } byte[] buf = new byte[4]; // 4 bytes for "pic:" or "pong" int expected_size = 0; if (_recieve_state == RecieveState.PINGPONG) { lock (InStream) { InStream.Read(buf, 0, buf.Length); } last_ping = DateTime.UtcNow; String command = Encoding.ASCII.GetString(buf).Trim(); if (command.StartsWith("pic")) { buf = new byte[8]; // 8 bytes for image size InStream.Read(buf, 0, buf.Length); expected_size = Int32.Parse(Encoding.ASCII.GetString(buf).Trim()); Console.WriteLine("Expecting a picture, {0} bytes", expected_size); _recieve_state = RecieveState.PICTURE; } else if (command.StartsWith("wifi")) { Console.WriteLine(command); buf = new byte[4]; // 4 bytes for recieved handshake json length InStream.Read(buf, 0, buf.Length); Console.WriteLine(Encoding.ASCII.GetString(buf)); expected_size = Int32.Parse(Encoding.ASCII.GetString(buf).Trim()); buf = Common.ReadAll(InStream, expected_size); WifiCallback(buf); } else if (command != "pong") { // Unexpected data in buffer... // Recover by reconnecting? Console.WriteLine("junk on the buffer :( '" + command + "'"); Disconnect(); Connect(this.device); } } // This is a regular if, not an else if! if (_recieve_state == RecieveState.PICTURE) { byte[] picbuf = Common.ReadAll(InStream, expected_size, 8192 * 2); Console.WriteLine("Recieved picture? {0} bytes", picbuf.Length); Bitmap bitmap = BitmapFactory.DecodeByteArray(picbuf, 0, picbuf.Length); Console.WriteLine("Bitmap (hopefully) ready, sending over to main thread"); handler.ObtainMessage(0, bitmap).SendToTarget(); _recieve_state = RecieveState.PINGPONG; } } catch (Java.IO.IOException e) { Console.WriteLine("Recieving failed, connection might be lost"); Console.WriteLine(e); SetState(State.NOT_CONNECTED); Connect(device); } } }
public void recieve_buffered() { if (connection_state != ConnectionState.Connected) { return; } while (client.Available > 0) { if (recieve_state == RecieveState.Key) { if (client.Available < 3) { break; } byte[] cur_key = new byte[3]; if (client.GetStream().Read(cur_key, 0, 3) == 3) { // Got key, check it bool valid_key = true; for (int i = 0; i < 3; i++) { if (cur_key[0] != CommunicationDefinitions.key[0]) { valid_key = false; } } if (valid_key) { recieve_state = RecieveState.Header; } else { recieve_state = RecieveState.Key; } } } else if (recieve_state == RecieveState.Header) { if (client.Available < 1) { break; } if (client.GetStream().Read(recieve_buffer, 0, 1) == 1) { if (CommunicationDefinitions.PACKET_SIZES.ContainsKey((CommunicationDefinitions.TYPE)recieve_buffer[0])) { recieve_size = CommunicationDefinitions.PACKET_SIZES[(CommunicationDefinitions.TYPE)recieve_buffer[0]]; recieve_state = RecieveState.Data; } else { recieve_state = RecieveState.Key; } } } else if (recieve_state == RecieveState.Data) { if (client.Available < recieve_size) { break; } if (client.GetStream().Read(recieve_buffer, 1, recieve_size) == recieve_size) { CommunicationDefinitions.TYPE type = (CommunicationDefinitions.TYPE)recieve_buffer[0]; if (type == CommunicationDefinitions.TYPE.DATAAGGREGATOR_STATE) { StateData.dataaggregator_state.Deserialize(recieve_buffer); parent.update_indicators(); } else if (type == CommunicationDefinitions.TYPE.VISION) { StateData.vision_data.Deserialize(recieve_buffer); } else if (type == CommunicationDefinitions.TYPE.SENSOR_STATE) { StateData.sensor_state_data.Deserialize(recieve_buffer); } else if (type == CommunicationDefinitions.TYPE.VISION_IMAGE) { parent.vision_view.SetImage(recieve_buffer.Skip(1).ToArray()); } else if (type == CommunicationDefinitions.TYPE.ROBOT_STATE) { StateData.robot_state_data.Deserialize(recieve_buffer); } else if (type == CommunicationDefinitions.TYPE.REALSENSE) { StateData.realsense_data.Deserialize(recieve_buffer); } else { Console.WriteLine("Invalid Type"); } recieve_state = RecieveState.Key; } } } }