/// <summary> /// 实现自定义服务 /// </summary> public class MyService : CommandSocketService<AsyncBinaryCommandInfo> { /// <summary> /// 当连接时会调用此方法 /// </summary> /// <param name="connection"></param> public override void OnConnected(IConnection connection) { base.OnConnected(connection); Console.WriteLine(connection.RemoteEndPoint.ToString() + " connected"); } /// <summary> /// 当连接断开时会调用此方法 /// </summary> /// <param name="connection"></param> /// <param name="ex"></param> public override void OnDisconnected(IConnection connection, Exception ex) { base.OnDisconnected(connection, ex); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(connection.RemoteEndPoint.ToString() + " disconnected"); Console.ForegroundColor = ConsoleColor.Gray; } /// <summary> /// 当发生错误时会调用此方法 /// </summary> /// <param name="connection"></param> /// <param name="ex"></param> public override void OnException(IConnection connection, Exception ex) { base.OnException(connection, ex); Console.WriteLine("error: " + ex.ToString()); } /// <summary> /// 当服务端发送Packet完毕会调用此方法 /// </summary> /// <param name="connection"></param> /// <param name="e"></param> public override void OnSendCallback(IConnection connection, SendCallbackEventArgs e) { base.OnSendCallback(connection, e); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("send " + e.Status.ToString()); Console.ForegroundColor = ConsoleColor.Gray; } /// <summary> /// 处理未知的命令 /// </summary> /// <param name="connection"></param> /// <param name="commandInfo"></param> protected override void HandleUnKnowCommand(IConnection connection, AsyncBinaryCommandInfo commandInfo) { Console.WriteLine("unknow command: " + commandInfo.CmdName); } }
/// <summary> /// sum command /// 用于将一组int32数字求和并返回 /// </summary> public sealed class SumCommand : ICommand<AsyncBinaryCommandInfo> { /// <summary> /// 返回服务名称 /// </summary> public string Name { get { return "sum"; } } /// <summary> /// 执行命令并返回结果 /// </summary> /// <param name="connection"></param> /// <param name="commandInfo"></param> public void ExecuteCommand(IConnection connection, AsyncBinaryCommandInfo commandInfo) { if (commandInfo.Buffer == null || commandInfo.Buffer.Length == 0) { Console.WriteLine("sum参数为空"); connection.BeginDisconnect(); return; } if (commandInfo.Buffer.Length % 4 != 0) { Console.WriteLine("sum参数错误"); connection.BeginDisconnect(); return; } int skip = 0; var arr = new int[commandInfo.Buffer.Length / 4]; for (int i = 0, l = arr.Length; i < l; i++) { arr[i] = BitConverter.ToInt32(commandInfo.Buffer, skip); skip += 4; } commandInfo.Reply(connection, BitConverter.GetBytes(arr.Sum())); } }
app.config
<?xml version="1.0"?> <configuration> <configSections> <section name="socketServer" type="Sodao.FastSocket.Server.Config.SocketServerConfig, FastSocket.Server"/> </configSections> <socketServer> <servers> <server name="binary" port="8401" socketBufferSize="8192" messageBufferSize="8192" maxMessageSize="102400" maxConnections="20000" serviceType="Server.MyService, Server" protocol="asyncBinary"/> </servers> </socketServer> </configuration>
static void Main(string[] args) { SocketServerManager.Init(); SocketServerManager.Start(); Console.ReadLine(); }
static void Main(string[] args) { var client = new Sodao.FastSocket.Client.AsyncBinarySocketClient(8192, 8192, 3000, 3000); //注册服务器节点,这里可注册多个(name不能重复) client.RegisterServerNode("127.0.0.1:8401", new System.Net.IPEndPoint(System.Net.IPAddress.Parse("127.0.0.1"), 8401)); //client.RegisterServerNode("127.0.0.1:8402", new System.Net.IPEndPoint(System.Net.IPAddress.Parse("127.0.0.2"), 8401)); //组织sum参数, 格式为<<i:32-limit-endian,....N>> //这里的参数其实也可以使用thrift, protobuf, bson, json等进行序列化, byte[] bytes = null; using (var ms = new System.IO.MemoryStream()) { for (int i = 1; i <= 1000; i++) ms.Write(BitConverter.GetBytes(i), 0, 4); bytes = ms.ToArray(); } //发送sum命令 client.Send("sum", bytes, res => BitConverter.ToInt32(res.Buffer, 0)).ContinueWith(c => { if (c.IsFaulted) { Console.WriteLine(c.Exception.ToString()); return; } Console.WriteLine(c.Result); }); Console.ReadLine(); }