Skip to content

Instantly share code, notes, and snippets.

@XSockets
Created January 13, 2014 09:15
Show Gist options
  • Save XSockets/8396972 to your computer and use it in GitHub Desktop.
Save XSockets/8396972 to your computer and use it in GitHub Desktop.
Shows a simple custom protocol (plugin) for XSockets.NET. You will from 3.0.3 be able to communicate between any clients no matter protocol. This means that you with ease can talk between any devices talking TCP/IP.
/// <summary>
/// A really simple/stupid protocol.
/// The magic here is that clients connected to this protocol (TelNet, Arduino, Netduino or whatever) will be able
/// to communicate cross-protocol with client talking RFC6455 for example (or any other implemented protocol).
/// </summary>
public class SimpleProtocol : XSocketProtocol
{
/// <summary>
/// Extract the path (controller name) from the handshake
/// </summary>
public Regex GetPathRegex
{
get { return new Regex(@".+?(?= "+this.ProtocolPattern+")", RegexOptions.None); }
}
/// <summary>
/// A simple identifier fot the protocol since the ProtocolPattern might be complex or unfriendly to read.
/// </summary>
public override string ProtocolIdentifier
{
get { return "SimpleProtocolIdentifier"; }
}
/// <summary>
/// The string to identify the protocol in the handshake
/// </summary>
public override string ProtocolPattern
{
get { return "SimpleProtocol"; }
}
/// <summary>
/// The string to return after handshake
/// </summary>
public override string HostResponse
{
get { return "SimpleProtocolHostResponse"; }
}
/// <summary>
/// Perform any extra logic for handshake, build a hostresponse etc
/// </summary>
/// <returns></returns>
public override bool DoHandshake()
{
Response = HostResponse;
return true;
}
/// <summary>
/// Extract the Controller to connect to.
/// In this simple protocol the handshake might look like "Generic SimpleProtocol"
/// You could ofcourse just split on " " but...
/// since you probably want a more complex protocol RegEx will probably be used in real life.
/// </summary>
public override void SetPath()
{
this.Path = this.GetPathRegex.Match(this.Handshake).Groups[0].ToString();
}
public override IXSocketProtocol NewInstance()
{
return new SimpleProtocol();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment