Skip to content

Instantly share code, notes, and snippets.

@hide1202
Created April 27, 2016 07:17
Show Gist options
  • Save hide1202/abb74755105c2282a55c3d454904854b to your computer and use it in GitHub Desktop.
Save hide1202/abb74755105c2282a55c3d454904854b to your computer and use it in GitHub Desktop.
Haste.EchoClient
using System;
using System.Net;
using System.Threading;
using Haste.Data;
using Haste.Messages;
namespace Haste.EchoClient
{
class EchoClient
{
private const int MESSAGE_CODE = 0;
private const int MESSAGE_PARAM_CODE = 0;
private NetworkConnection _connection;
private ConnectionConfig _config;
public EchoClient(ConnectionConfig config)
{
_connection = new NetworkConnection();
_config = config;
}
public void Start()
{
_connection.Configure(_config);
_connection.ResponseReceived += OnResponseReceived;
_connection.StatusChanged += OnStatusChanged;
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5056);
_connection.Connect(remoteEndPoint, new Version(0, 1, 0), null);
Thread receiveThread = new Thread(Receive);
receiveThread.Start(_connection);
}
public void Send(string input)
{
DataObject data = new DataObject();
data.SetString(MESSAGE_PARAM_CODE, input);
_connection.SendRequestMessage(MESSAGE_CODE, data, SendOptions.ReliableSend);
}
private static void Receive(object parameter)
{
NetworkConnection connection = parameter as NetworkConnection;
if (parameter == null)
{
throw new InvalidCastException("Thread's parameter is not NetworkConnection");
}
while (true)
{
connection.NetworkUpdate();
}
}
private static void OnStatusChanged(StatusCode statusCode, string s)
{
Console.WriteLine("[OnStatusChanged] {0}, {1}", statusCode, s);
}
private static void OnResponseReceived(ResponseMessage response)
{
if (response.Code == MESSAGE_CODE)
{
string message = string.Empty;
if (response.Data.GetValue(MESSAGE_PARAM_CODE, out message))
{
Console.WriteLine("[OnResponseReceived] Server message is \"{0}\"", message);
}
}
}
}
}
using System;
using System.Net;
using System.Text;
using System.Threading;
using Haste.Data;
using Haste.Messages;
namespace Haste.EchoClient
{
class Program
{
static void Main(string[] args)
{
EchoClient client = new EchoClient(new ConnectionConfig
{
ChannelCount = 5,
DisconnectionTimeout = 3000,
IsCrcEnabled = false,
MaxUnreliableCommands = 0,
MTUSize = 1350,
PingInterval = 1000,
SentCountAllowance = 3,
WarningQueueSize = 500,
});
client.Start();
while (true)
{
string input = Console.ReadLine();
if (input.ToLower() == "exit" || input.ToLower() == "quit")
break;
client.Send(input);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment