Custom Relay
Custom relay (Advanced)
using System;
using System.Collections.Generic;
using System.Net;
using Coherence.Brook;
using Coherence.Common;
using Coherence.Connection;
using Coherence.Transport;
public class CustomTransport : ITransport
{
// Members required by ITransport
public event Action OnOpen;
public event Action<ConnectionException> OnError;
public TransportState State { get; private set; }
public bool IsReliable => false;
public bool CanSend => true;
public int HeaderSize => 0;
public string Description => "Custom";
// In this example, data is routed over a FoobarConnection that is provided by the FoobarNetworkingService
private FoobarConnection foobarConnection;
// The networking service normally requires some way to identify the host, for example a string or IP-address
private string hostEndpoint;
// Instantiates the CustomTransport configured to connect to a specific remote host endpoint
public CustomTransport(string hostEndpoint)
{
this.hostEndpoint = hostEndpoint;
}
// This method is called when the client calls CoherenceBridge.Connect
public void Open(EndpointData _, ConnectionSettings __)
{
// Initialize the networking service
FoobarNetworkingService.Init();
// Connect to the FoobarNetworkingService
foobarConnection = FoobarNetworkingService.OpenNewOutgoingConnection(hostEndpoint);
// Mark the transport as open, i.e. ready to send and receive messages
State = TransportState.Open;
// Notify the client that the connection has been opened
OnOpen?.Invoke();
}
// This method is called when the CoherenceBridge disconnects
public void Close()
{
// Mark the transport as closed, i.e., no longer able to send or receive messages
State = TransportState.Closed;
// Disconnect from the FoobarNetworkingService
foobarConnection.Close();
// Dispose the networking service
FoobarNetworkingService.Shutdown();
}
// This method is not mandatory but can be used e.g., to send a final disconnect message before the connection is closed
public void PrepareDisconnect() { }
// This method is called each frame. The buffer should be populated with incoming messages
public void Receive(List<(IInOctetStream, IPEndPoint)> buffer)
{
// If there are any incoming messages, push them to the return buffer
while (foobarConnection.TryReceiveMessage(out var data))
{
buffer.Add((new InOctetStream(data), default));
}
}
// This method is called for each packet that the client wants to send to the Replication Server
public void Send(IOutOctetStream stream)
{
// Convert the stream to a byte-array and send it over the foobarConnection
foobarConnection.SendMessage(stream.Close().ToArray());
}
public void SendTo(IOutOctetStream stream, IPEndPoint endpoint, SessionID sessionID)
{
throw new Exception("SendTo not supported on this transport.");
}
}Last updated
Was this helpful?

