Skip to content

Instantly share code, notes, and snippets.

@kw26
Last active September 20, 2019 10:12
Show Gist options
  • Save kw26/575eb6ca8151190ab1d5465b9dfd2cf8 to your computer and use it in GitHub Desktop.
Save kw26/575eb6ca8151190ab1d5465b9dfd2cf8 to your computer and use it in GitHub Desktop.
ventuz simple tcpclient

ventuz simple tcpclient

inputs
type name
string address
func Connect
int port
outputs
type name
string result
string state

precautions

This code is provided as is!

troubleshooting:

Error Solution
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();
}
}
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;
public async void TcpClientThread()
{
try
{
var tcpClient = new TcpClient ();
await tcpClient.ConnectAsync(this.address, this.port);
this.state = "ALIVE";
changed = true;
using (var networkStream = tcpClient.GetStream())
{
var buffer = new byte[4096];
while(true)
{
var byteCount = await networkStream.ReadAsync(buffer, 0, buffer.Length);
if (byteCount <= 0)
{
this.state = "DEAD";
changed = true;
break;
}
var response = Encoding.UTF8.GetString(buffer, 0, byteCount);
this.result = response;
changed = true;
VLog.Info("[Client] Server response was {0}", response);
}
}
}
catch (AggregateException ex)
{
ex.Handle(exception =>
{
this.state = "DEAD";
changed = true;
VLog.Info(exception.Message);
return true;
});
}
}
public Script()
{
// Note: Accessing input or output properties from this method
// will have no effect as they have not been allocated yet.
TcpClientThread();
}
// 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()
TcpClientThread();
}
// 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;
}
public void OnConnect()
{
TcpClientThread();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment