Team XSockets.NET

Custom Transport (deprecated)

In this section we will create a very basic transport to show you how to enable custom encryption/decryption.

We will no actually use any encryption/decryption in this sample. We will show the flow of the data so that you can see where you should implement your encryption/decryption.

We will also show how to configure the server to use our custom transport

Transport Implementation

The easiest way to implement a custom transport is to inherit the DefaultTransport. Then you can override the methods needed for your specific needs.

In the sample we write to the IXLogger to that we can see the flow of the data when we test the transport. We write the messages with level Error so that it will be easy to see in the log.

using System;
using System.Net.Sockets;
using System.Threading.Tasks;
using XSockets.Plugin.Framework;
using XSockets.Core.Common.Socket;
using XSockets.Core.Common.Utility.Logging;

public class MyCustomTransport : DefaultTransport
{
    /// <summary>
    /// Default ctor
    /// </summary>
    public MyCustomTransport():base()
    {

    }
    /// <summary>
    /// Ctor
    /// 
    /// By setting EncryptingTransport = true the methods Encrypt/Decrypt will be called when messages 
    /// arrive and leave the server.
    /// 
    /// Encrypt will be called when a message is about to leave the server.
    /// Decrypt will be called when a data arrives at the server.
    /// </summary>
    /// <param name="socket"></param>
    public MyCustomTransport(Socket socket) : this()
    {
        EncryptingTransport = true;
        SetSocket(socket);
    }

    public override byte[] Decrypt(byte[] data)
    {
        //TODO: Implement your custom decryption here
        Composable.GetExport<IXLogger>().Error("You should decrypt incoming data");
        return data;
    }

    public override byte[] Encrypt(byte[] data)
    {
        //TODO: Implement your custom encryption here
        Composable.GetExport<IXLogger>().Error("You should encrypt outgoing data");
        return data;
    }

    /// <summary>
    /// Makes sure that encryption is performed before data is sent
    /// </summary>
    /// <param name="buffer"></param>
    /// <returns></returns>
    public override Task<bool> Send(byte[] buffer)
    {
        Composable.GetExport<IXLogger>().Error("Send in custom transport");
        return base.Send(this.Encrypt(buffer));
    }

    /// <summary>
    /// Makes sure the custom transport is used with the socket that the client connects to
    /// </summary>
    /// <returns></returns>
    public override async Task<ITransport> Accept()
    {
        Func<IAsyncResult, ITransport> end = r =>
        {
            _tokenSource.Token.ThrowIfCancellationRequested();
            return new MyCustomTransport(Socket.EndAccept(r));
        };

        return await _taskFactory.FromAsync(Socket.BeginAccept, end, null);
    }
}

Configuration Using Our Custom Transport

All we need to do in the configuration is to register the custom transport as the prefered transport for the configuration.

using XSockets.Core.Configuration;
using XSockets.Core.XSocket.Transport;

/// <summary>
/// A custom configuration for localhost:4503
/// The configuration will use a MyCustomTransport as Transport
/// </summary>
public class MyCustomTransportConfig : ConfigurationSetting
{
    public MyCustomTransportConfig() : base("ws://localhost:4503")
    {
        this.Transport = new MyCustomTransport();
    }
}

Testing The Transport

Now we can connect to the configuration/endpoint at localhost:4503 and see that our custom transport is used. As already mentioned we will not actually encrypt/decrypt anything, but you will see where is should be done.

The next section will show some samples where we encrypt/decrypt data.

Connect

Using Putty we connect to localhost:4503

connect

Handshake

AS we have done previously in this documentation we send a handshake via Putty to tell the server what protocol to use. In this case we also want to use our custom Transport. So we will see some Error logs telling us that we should encrypt/decrypt data.

Everything will work just fine since Putty does not encrypt/decrypt and neither does the custom Transport. So we are just faking/testing the flow here.

In the picture below you can see that the log wrote a few lines

  • You should decrypt incoming data (written when the handshake was received)
  • Send in custom transport (written when the server responded with the welcome message)
  • You should encrypt outgoing data (written when the send method in the transport called the encrypt method in the transport)

handshake

Message

So now we just send a message to the generic controller of XSockets. And as expected we can see some Error logs when data goes in/out of the server using our custom Transport

handshake

results matching ""

    No results matching ""