Gremlin77 Posted January 11, 2012 Posted January 11, 2012 Hi, has anyone an idea how to connect the export.lua (with an UDP-Port implemented like the one by Helios) with your own c# prog? I tried the normal Visual UDP-socket, but got no result? visit my build thread Gremlin's A-10 :thumbup: http://forums.eagle.ru/showthread.php?t=86916
Moa Posted January 11, 2012 Posted January 11, 2012 1) Use Java instead of C#, it means you can run your program on any device you have (plus the tools are free - which means if you give your software to anyone else they won't have to pay for [or steal] the tools). 2) The TCP(!) sockets work fine on localhost:8079 here is the Java-code I used to read position data from a custom export.lua. This is for an un-released mod (in testing by the 104th) that shows the player's aircraft on a moving map (the excellent TC-1 by igormk), similar to the Ka-50 ABRIS or A-10C TAD, but for the aircraft of Flaming Cliffs 2: package jacmi.simclient; import java.io.BufferedInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.LineNumberReader; import java.net.ConnectException; import java.net.Socket; import java.net.SocketException; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * A class that can be run to read simulation data from a LockOn game instance. * * @author Mike. */ public class SimClient implements Runnable { private String host; private int port; private boolean started; private boolean shouldStop; private int serialNumber; private Socket socket; private InputStream inputStream; private InputStreamReader inputReader; private LineNumberReader reader; private List<SimDataListener> simDataListeners = new ArrayList<SimDataListener>(); public static void main(String args[]) { SimClient client = new SimClient(); client.setHost("localhost"); client.setPort(8079); client.run(); } public SimClient() { } public void connect(String host, int port) { if (host == null || host.trim().isEmpty()) { throw new IllegalArgumentException("Cannot connect as the specified host had an invalid value of '" + host + "'"); } if (port < 1 || port > 65535) { throw new IllegalArgumentException("Cannot connect to host '" + host + "' as the specified port " + port + " is invalid"); } // If we are already connect to the requested host and port then we // don't have to do anything. boolean socketAvailable = isSocketAvailable(); if (socketAvailable && this.host.equals(host.trim()) && (port == this.port)) { return; // The socket is already connected to the specified host and port, and is still open. } if (socket != null) { disconnect(); } try { socket = new Socket(host, port); this.host = host; this.port = port; System.out.println("Connected to simulator at host '" + host + "', port " + port); socket.setReuseAddress(true); socket.setKeepAlive(true); inputStream = socket.getInputStream(); BufferedInputStream bufferedStream = new BufferedInputStream(inputStream); inputReader = new InputStreamReader(bufferedStream); reader = new LineNumberReader(inputReader); } catch (UnknownHostException th) { System.err.println("Cannot connect as the host '" + host + "' is not known (port " + port + ")"); } catch (ConnectException ex) { // Ignore connection problems since there is the possibility that // the simulation is not yet running. } catch (Throwable th) { System.err.println("A problem occured when trying to connect to host '" + host + "', port " + port); th.printStackTrace(System.err); } } public void disconnect() { if (socket != null) { System.err.println("Disconnecting ..."); try { reader.close(); } catch (Throwable th) { // Ignore problems closing the stream. th.printStackTrace(System.err); } try { inputReader.close(); } catch (Throwable th) { // Ignore problems closing the stream. th.printStackTrace(System.err); } try { inputStream.close(); } catch (Throwable th) { // Ignore problems closing the stream. th.printStackTrace(System.err); } try { socket.close(); } catch (Throwable th) { th.printStackTrace(System.err); } finally { reader = null; inputReader = null; inputStream = null; socket = null; } } } public void run() { System.out.println("Started running ..."); int readCycle = 0; int lastReportedSerialNumber = 0; long startTime = System.currentTimeMillis(); long lastNonNullDataRead = 0; while (!shouldStop) { // Wait until we've established a connection. waitUntilConnected(); ++readCycle; if (shouldStop) { break; } // Read data from socket. boolean socketAvailable = isSocketAvailable(); if (!shouldStop && socketAvailable) { try { long startReadTimestamp = System.currentTimeMillis(); SimData latestReading = readData(); long endReadTimestamp = System.currentTimeMillis(); if (latestReading != null) { latestReading.setLocalTime(new Date()); latestReading.setSerialNumber(serialNumber); latestReading.setStartReadTimestamp(startReadTimestamp); latestReading.setEndReadTimestamp(endReadTimestamp); latestReading.setReadDurationMillis(endReadTimestamp - startReadTimestamp); ++serialNumber; // Tell any listeners that we have new data. notifySimDataListeners(latestReading); //System.out.println("Read data at T+" + (endReadTimestamp - startTime) + "ms, read duration = " + latestReading.getReadDurationMillis() + " ms"); socketAvailable = isSocketAvailable(); lastNonNullDataRead = endReadTimestamp; } else { socketAvailable = isSocketAvailable(); if ( (lastNonNullDataRead + 1000) < System.currentTimeMillis()) { lastNonNullDataRead = System.currentTimeMillis(); //System.out.println("readData() was called but returned null for the last 1s, socketAvailable = " + socketAvailable); if (socketAvailable && socket.getInputStream().available() < 0) { System.out.println("Bytes available " + socket.getInputStream().available()); } } } // Sleep for a little time before looking to read data again. try { Thread.sleep(40); } catch (InterruptedException ex) { // Ignore interruptions. } } catch (Throwable th) { th.printStackTrace(System.err); } } else { System.out.println("Socket not available for reading!"); } if (serialNumber > lastReportedSerialNumber) { //System.out.println("Read data count = " + serialNumber); //lastReportedSerialNumber = serialNumber; } } } public synchronized void stop() { shouldStop = true; } public SimData readData() throws Throwable { // If there are no bytes available to be read then we return nothing. boolean socketAvailable = isSocketAvailable(); if (!socketAvailable) { System.out.println("readData() socketAvailable = " + socketAvailable); return null; } String endDataMessage = "!!End of Sim Data!!"; List<String> lines = new ArrayList<String>(); boolean done = false; boolean reachedEndOfStream = false; boolean connectionReset = false; try { while (!done) { String line = reader.readLine(); if (line != null) { lines.add(line); if (line.trim().equals(endDataMessage)) { done = true; } } else { done = true; reachedEndOfStream = true; } } } catch (Throwable th) { if (th.getMessage().contains("Connection reset")) { connectionReset = true; } else { th.printStackTrace(System.err); } done = true; throw th; } if (reachedEndOfStream || connectionReset) { disconnect(); // Disconnect so we can re-establish a connection later. } // Convert lines into SimData object. SimData data = null; if (!lines.isEmpty()) { SimDataCodec codec = new SimDataCodec(); data = codec.parseSimData(lines); } return data; } public void waitUntilConnected() { boolean socketAvailable = isSocketAvailable(); while(!shouldStop && !socketAvailable) { try { if (socket != null) { disconnect(); } connect(host, port); socketAvailable = isSocketAvailable(); if (socketAvailable) { return; // We've got a connection, finish waiting. } } catch (Throwable th) { // Don't do anything, perhaps the simulation has not yet started. th.printStackTrace(); } // We don't have a connection. Wait a bit before trying again. final long waitTimeMillis = 500; final long waitFinishTime = System.currentTimeMillis() + waitTimeMillis; while (System.currentTimeMillis() < waitFinishTime) { try { if (shouldStop) { return; // We're stopping the program, so finish waiting. } Thread.sleep(10); // Sleep for a little bit to allow other programs to run. } catch (InterruptedException ex) { // Ignore interuptions. } } } } /** * @return the host */ public String getHost() { return host; } /** * @param host the host to set */ public void setHost(String host) { this.host = host; } /** * @return the port */ public int getPort() { return port; } /** * @param port the port to set */ public void setPort(int port) { this.port = port; } /** * @return the started */ public boolean isStarted() { return started; } public boolean isSocketAvailable() { if (socket == null) { return false; } if (socket.isClosed()) { return false; } if (!socket.isConnected()) { return false; } if (socket.isInputShutdown()) { return false; } try { if (inputStream == null) { return false; } } catch (Throwable th) { return false; } return true; } public void addSimDataListener(SimDataListener listener) { if (listener == null) { throw new IllegalArgumentException("Cannot add SimDataListener as the listener given was null"); } if (!simDataListeners.contains(listener)) { simDataListeners.add(listener); } } public void removeSimDataListener(SimDataListener listener) { if (listener == null) { throw new IllegalArgumentException("Cannot remove SimDataListener as the listener given was null"); } if (simDataListeners.contains(listener)) { simDataListeners.remove(listener); } } public void notifySimDataListeners(SimData data) { if (data == null) { throw new IllegalArgumentException("Cannot notify SimDataListener as the data given was null"); } for (SimDataListener listener : simDataListeners) { listener.dataReceived(data); } } }
boarder2 Posted January 13, 2012 Posted January 13, 2012 Hi, has anyone an idea how to connect the export.lua (with an UDP-Port implemented like the one by Helios) with your own c# prog? I tried the normal Visual UDP-socket, but got no result? Pretty simple using the UdpClient. http://msdn.microsoft.com/en-us/library/system.net.sockets.udpclient.aspx iControl DCS/DCS Virtual Cockpit - Full featured iPad Cockpit - Now with Android support! A10 Virtual Cockpit Free - Free limited functionality version of iControl DCS! DCS Virtual Cockpit - Android version! Follow on Twitter for all the latest news
boarder2 Posted January 13, 2012 Posted January 13, 2012 1) Use Java instead of C#, it means you can run your program on any device you have (plus the tools are free - which means if you give your software to anyone else they won't have to pay for [or steal] the tools). With mono and mono develop you can work in just about any environment. Never mind that Visual Studio Express is free as well. 2) The TCP(!) sockets work fine on localhost:8079 here is the Java-code I used to read position data from a custom export.lua I strongly disagree. The fact that UDP does not require a constant connection, is broadcast, and is much more lightweight than TCP lends itself much better to something like this. With current networking hardware you don't really need to worry about not receiving messages. iControl DCS/DCS Virtual Cockpit - Full featured iPad Cockpit - Now with Android support! A10 Virtual Cockpit Free - Free limited functionality version of iControl DCS! DCS Virtual Cockpit - Android version! Follow on Twitter for all the latest news
Recommended Posts