|
using System; |
|
using System.Linq; |
|
using Ventuz.Kernel; |
|
using System.Diagnostics; |
|
using System.Collections.Generic; |
|
using System.Linq; |
|
using System.Net; |
|
using System.Net.Sockets; |
|
using System.Text; |
|
using System.Threading; |
|
using System.Threading.Tasks; |
|
|
|
public class Script : ScriptBase, System.IDisposable |
|
{ |
|
|
|
// This member is used by the Validate() method to indicate |
|
// whether the Generate() method should return true or false |
|
// during its next execution. |
|
private bool changed; |
|
private static TcpClient tcpClient; |
|
private NetworkStream netStream; |
|
private static byte[] buffer = new byte[4096]; |
|
|
|
private void BeginReadCallback(IAsyncResult ar) |
|
{ |
|
try |
|
{ |
|
var bytesRead = netStream.EndRead(ar); |
|
if (bytesRead == 0) |
|
{ |
|
((Action) ar.AsyncState)(); |
|
} |
|
else |
|
{ |
|
var respon = Encoding.ASCII.GetString(buffer, 0, bytesRead); |
|
VLog.Info(respon); |
|
this.result = respon; |
|
changed = true; |
|
netStream.BeginRead(buffer, 0, buffer.Length, this.BeginReadCallback, ar.AsyncState); |
|
} |
|
} |
|
catch (Exception e) |
|
{ |
|
this.state = "DEAD"; |
|
changed = true; |
|
VLog.Info("Generic Exception"); |
|
} |
|
} |
|
|
|
public void tcpworker() |
|
{ |
|
try |
|
{ |
|
tcpClient = new TcpClient(); |
|
object o = new object(); |
|
tcpClient.BeginConnect(this.address, this.port, asyncResult => |
|
{ |
|
VLog.Info("connect completed"); |
|
try |
|
{ |
|
this.state = "ALIVE"; |
|
changed = true; |
|
tcpClient.EndConnect(asyncResult); |
|
VLog.Info("client connected"); |
|
|
|
netStream = tcpClient.GetStream(); |
|
netStream.BeginRead(buffer, 0, buffer.Length, BeginReadCallback, netStream); |
|
} |
|
catch (Exception e) |
|
{ |
|
this.state = "DEAD"; |
|
changed = true; |
|
VLog.Info("Generic Exception"); |
|
} |
|
lock (o) Monitor.Pulse(o); |
|
}, null); |
|
} |
|
catch (Exception e) |
|
{ |
|
netStream.Close(); |
|
tcpClient.Close(); |
|
this.state = "DEAD"; |
|
changed = true; |
|
VLog.Info("Generic Exception"); |
|
} |
|
} |
|
|
|
public Script() |
|
{ |
|
// Note: Accessing input or output properties from this method |
|
// will have no effect as they have not been allocated yet. |
|
//tcpworker(); |
|
} |
|
|
|
// This Method is called if the component is unloaded/disposed |
|
public virtual void Dispose() |
|
{ |
|
} |
|
|
|
// This Method is called if an input property has changed its value |
|
public override void Validate() |
|
{ |
|
// Remember: set changed to true if any of the output |
|
// properties has been changed, see Generate() |
|
//tcpworker(); |
|
} |
|
|
|
// This Method is called every time before a frame is rendered. |
|
// Return value: if true, Ventuz will notify all nodes bound to this |
|
// script node that one of the script's outputs has a |
|
// new value and they therefore need to validate. For |
|
// performance reasons, only return true if output |
|
// values really have been changed. |
|
public override bool Generate() |
|
{ |
|
if (changed) |
|
{ |
|
changed = false; |
|
return true; |
|
} |
|
|
|
return false; |
|
} |
|
|
|
|
|
|
|
// This Method is called if the function/method Conneect is invoked by the user or a bound event. |
|
// Return true, if this component has to be revalidated! |
|
public void OnConneect() |
|
{ |
|
tcpworker(); |
|
} |
|
|
|
|
|
|
|
} |