zoukankan      html  css  js  c++  java
  • 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方法,服务端主动断开连接,终端退出。

  • 相关阅读:
    Java实现 洛谷 P1423 小玉在游泳
    Java设置session超时(失效)的时间
    How Tomcat works — 八、tomcat中的session管理
    三种常用的MySQL建表语句
    mysql和oracle的区别(功能性能、选择、使用它们时的sql等对比)
    oracle 基础表 mysql版
    oracle员工表和部门表基本操作
    Oracle
    java生成6位随机数
    用Ajax图片上传、预览、修改图片
  • 原文地址:https://www.cnblogs.com/T-MAC/p/fastsocket.html
Copyright © 2011-2022 走看看