FastSocket.Net

Overview

FastSocket是一个轻量级易扩展的c#异步socket通信库,项目开始于2011年,经过近3年不断调整与改进,目前在功能和性能上均有不错的表现。

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

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

Requirements

.Net 4.0 or Mono 2.6 

Example Usage 

简单的命令行服务

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

自定义服务实现MyService

using System;
using Sodao.FastSocket.Server;
using Sodao.FastSocket.Server.Command;
using Sodao.FastSocket.SocketBase;

/// <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输出的

/// <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时,触发了ExitCommand.ExecuteCommand方法,服务端主动断开连接,终端退出。

posted @ 2013-08-16 17:05  Dev.Hong  阅读(15582)  评论(25编辑  收藏  举报