FastSocket.Net

https://github.com/ihaoqihao/FastSocket.Net

Overview

项目地址:https://github.com/devhong/FastSocket.Net 

在Nuget官方源中搜索fastsocket可快速安装引用

QQ群:257612438

FastSocket内置了命令行、二进制、thrift协议,基于此开发了Zookeeper, Redis, Thrift等c#异步客户端。

Requirements

.Net 4.0 or Mono 2.6

Projects using FastSocket.Net

Example Usage

1: 简单的命令行服务.

新建控制台项目,添加FastSocket.SocketBase,FastSocket.Server引用

自定义服务实现MyService

/// <summary>
/// 实现自定义服务
/// </summary>
public class MyService : CommandSocketService<StringCommandInfo>
{
    /// <summary>
    /// 当连接时会调用此方法
    /// </summary>
    /// <param name="connection"></param>
    public override void OnConnected(IConnection connection)
    {
        base.OnConnected(connection);
        Console.WriteLine(connection.RemoteEndPoint.ToString() + " connected");
        connection.BeginSend(PacketBuilder.ToCommandLine("welcome"));
    }
    /// <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>
    /// 处理未知命令
    /// </summary>
    /// <param name="connection"></param>
    /// <param name="commandInfo"></param>
    protected override void HandleUnKnowCommand(IConnection connection, StringCommandInfo commandInfo)
    {
        commandInfo.Reply(connection, "unknow command:" + commandInfo.CmdName);
    }
}

Exit命令

/// <summary>
/// 退出命令
/// </summary>
public sealed class ExitCommand : ICommand<StringCommandInfo>
{
    /// <summary>
    /// 返回命令名称
    /// </summary>
    public string Name
    {
        get { return "exit"; }
    }
    /// <summary>
    /// 执行命令
    /// </summary>
    /// <param name="connection"></param>
    /// <param name="commandInfo"></param>
    public void ExecuteCommand(IConnection connection, StringCommandInfo commandInfo)
    {
        connection.BeginDisconnect();//断开连接
    }
}

App.config配置

<?xml version="1.0"?>
<configuration>

  <configSections>
    <section name="socketServer"
             type="Sodao.FastSocket.Server.Config.SocketServerConfig, FastSocket.Server"/>
  </configSections>

  <socketServer>
    <servers>
      <server name="cmdline"
              port="8400"
              socketBufferSize="8192"
              messageBufferSize="8192"
              maxMessageSize="102400"
              maxConnections="20000"
              serviceType="CommandLine.MyService, CommandLine"
              protocol="commandLine"/>
    </servers>
  </socketServer>

</configuration>

初始化及启动服务

static void Main(string[] args)
{
    SocketServerManager.Init();
    SocketServerManager.Start();

    Console.ReadLine();
}

启动服务,然后在cmd中运行telnet 127.0.0.1 8400, 运行截图如下:

其中welcome中当连接建立时服务端发送到终端的。

connection.BeginSend(PacketBuilder.ToCommandLine("welcome"));

unknow command:Hello是因为没有对应的"Hello"命令实现由HandleUnKnowCommand输出的

protected override void HandleUnKnowCommand(IConnection connection, StringCommandInfo commandInfo)
{
    commandInfo.Reply(connection, "unknow command:" + commandInfo.CmdName);
}

当在终端中键入exit时,触发了ExitCommand.ExecuteCommand方法,服务端主动断开连接,终端退出。

2: 在服务中使用自定义二进制协议

新建控制台项目,命名为Server
添加FastSocket.SocketBase,FastSocket.Server引用
Socket命令服务类: Sodao.FastSocket.Server.CommandSocketService泛型类
其中需要实现Socket连接,断开,异常,发送完回调及处理未知命令的方法
内置的二进制命令对象: Sodao.FatSocket.Server.Command.AsyncBinaryCommandInfo
由一个command name,一个唯一标识SeqId和主题内容buffer构建。
定义服务类MyService继承CommandSocketService类,
泛型类型为上述的AsyncBinanryCommandInfo

/// <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);
    }
}
实现一个命令如示例项目中的SumCommand类,命令类需要实现ICommand泛型接口
即服务中可以进行处理的服务契约
而泛型类型即上述的AsyncBinaryCommandInfo
/// <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>
其中section name="socketServer" 为服务端默认读取的sectionName
type为反射自FastSocket.Server中的config类型
server配置中,name自定,serviceType为上述实现的服务类反射类型
协议名为asyncBinary
在Main函数中启动服务

static void Main(string[] args)
{
    SocketServerManager.Init();
    SocketServerManager.Start();

    Console.ReadLine();
}
新建控制台应用程序,命名为Client
添加FastSocket.Client,FastSocket.SocketBase引用
客户端的代码为组织命令向服务端请求
创建一个Sodao.FastSocket.Client.AsyncBinarySocketClient的实例
并通过RegisterServerNode来注册服务端节点,需要注意name必须唯一
并且地址为我们服务端运行的地址,端口为服务端配置文件中配置的端口号
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();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值