public void SendingAndReceiving() { TcpClient clientSender; TcpClient clientReader; SpawnSockets(out clientSender, out clientReader); unnitTest_DataReceiver receiver = new unnitTest_DataReceiver(); test = new TCPDataLink(clientReader); test.SetReceiver(receiver); receiver.expectedResult = Encoding.ASCII.GetBytes("Hello there"); clientSender.GetStream().Write(receiver.expectedResult, 0, receiver.expectedResult.Length); int fail_escape = 5000; while (!receiver.receivedData && fail_escape > 0) { Thread.Sleep(1); fail_escape--; } Assert.IsFalse(receiver.failed, "Expected result is different from actual result"); receiver.expectedResult = Encoding.ASCII.GetBytes("Goodbye pal!"); clientSender.GetStream().Write(receiver.expectedResult, 0, receiver.expectedResult.Length); fail_escape = 5000; while (!receiver.receivedData && fail_escape > 0) { Thread.Sleep(1); fail_escape--; } Assert.IsFalse(receiver.failed, "Expected result is different from actual result"); }
public static bool ConnectToUnity(out Communicator communicator, string unityIp, ushort unityPort, int timeout = -1) { communicator = null; //Creates a connection to the robot using the specified hostname and port TcpClient tcpClient = new TcpClient(); IAsyncResult ar = tcpClient.BeginConnect(unityIp, unityPort, null, null); WaitHandle wh = ar.AsyncWaitHandle; try { if (!wh.WaitOne(timeout, false)) { tcpClient.Close(); wh.Close(); return(false); } tcpClient.EndConnect(ar); } catch (SocketException ex) { Console.WriteLine(ex.Message); return(false); } finally { wh.Close(); } IDataLink _dataLink = new TCPDataLink(tcpClient); IPresentationProtocol _protocol = new ProtoBufPresentation(); _dataLink.SetReceiver(_protocol); communicator = new Communicator(_dataLink, _protocol); return(true); }
public Ev3Connection(string projectName, TCPDataLink datalink) { if (projectName == null) { throw new ArgumentNullException("projectName"); } if (projectName.Length < 1) { throw new ArgumentException("projectName can't be an empty string", "projectName"); } if (datalink == null) { throw new ArgumentNullException("datalink"); } _serialNumber = null; _ev3ProjectName = projectName; _lastMessage = null; _tcpPort = -1; _tcpDataLink = datalink; _tcpDataLink.SetReceiver(new DataStreamReceiver(OnIncomingTcpData)); _receiveSem = new Semaphore(0, 1); }
private async void _OnReceive() { while (_udpClient != null) { UdpReceiveResult results; try { results = await _udpClient.ReceiveAsync(); } catch (ObjectDisposedException) { continue; } string msgStr = Encoding.ASCII.GetString(results.Buffer); Regex serialRegex = new Regex("Serial-Number: (.*)"); Regex portRegex = new Regex("Port: (.*)"); string serialNumber = null; int port = -1; Match match = serialRegex.Match(msgStr); if (match.Success) { serialNumber = match.Groups[1].Value; } /* serialNumber != null && */ if (!_connectedSerials.Contains(serialNumber)) { match = portRegex.Match(msgStr); if (match.Success) { string tcpPortStr = match.Groups[1].Value.TrimEnd(); bool success = int.TryParse(tcpPortStr, out port); if (!success) { port = -1; } } _connectedSerials.Add(serialNumber); SendUdpResponse(_udpClient, results.RemoteEndPoint); TCPDataLink dl = EstablishTcpConnection(serialNumber, results.RemoteEndPoint.Address, port); Ev3Connection con = new Ev3Connection("EV3Wifi", dl); Communicator communicator = null; if (Tools.ConnectToUnity(out communicator, _unityIP.ToString(), _unityPort, 5000)) { Console.WriteLine("Unity connected"); EV3Robot newRobot = new EV3Robot(communicator, "My ev3", con); Tools.AssignRobot(communicator, newRobot); _eventReceiver.OnRobotConnect(newRobot); } else { throw new Exception("Can't connect to unity..."); } } } }