LZ最近离职,闲着也是闲着,打算梳理下

公司做的是电商,CTO打算把2.0系统用java 语言开发,LZ目前不打算做java,所以 选择离职。离职前,在公司负责的最后一个项目 供应链系统。

系统分为 3套子系统:

1 供应链工作平台(即用户操作平台):采用CS架构,Sqlite做缓存。

2 消息中心: 后台程序,采用mina.net,scoket 长连接 保证服务消息的 推送,后台消息的提醒,和 系统对最新订单的缓存。

3 WindowsService 监控消息中心,保证消息中心 随系统的开启而启动

mina 简介:

Apache Mina Server 是一个网络通信应用框架,它主要是对基于TCP/IP、UDP/IP协议栈的通信框架,Mina 可以帮助我们快速开发高性能、高扩展性的网络通信应用,Mina 提供了事件驱动、异步(Mina 的异步IO 默认使用的是JavaNIO 作为底层支持)操作的编程模型。

mina.net 是Apache Mina Server 的.net 版本 主要用于系统的长连接通信

安装:

PM> Install-Package Mina

mina.net 主要对象:

1.AsyncSocketConnector: 发起链接

2.IoSession:mina 链接创建成功之后 客户端,服务的的数据传送

3.IoHandlerAdapter:适配器类。可以扩展

4.DemuxingProtocolCodecFactory:构建协议编码工厂

适配器主要方法:

1 MessageReceived:收到消息时触发

2.MessageSent 发送消息后触发

3.SessionClosed 关闭Session时 触发

4.SessionCreated 创建Session时 触发

5.ExceptionCaught 发生异常时 触发

6.SessionIdleSession 空闲时 触发

实现思路:

创建mina链接:

public void StartProcess(LoginContext config)
{
AsyncSocketConnector connector = new Mina.Transport.Socket.AsyncSocketConnector();
//注册协议编解码器工厂
connector.FilterChain.AddLast("encoding", new ProtocolCodecFilter(new MyMinaCodecFactory()));
//指定服务端IP 和端口号
connector.DefaultRemoteEndPoint = new IPEndPoint(IPAddress.Parse(MinaConfig.Ip), MinaConfig.Port);
//初始化 消息处理类
var headerDic = CreateHeader();
//继承IoHandlerAdapter构建适配器
MinaMessageHandler headler = new MinaMessageHandler(config, connector, headerDic); connector.Handler = headler; while (true)
{
try
{
//ClientHandler
//建立链接
session = connector.Connect().Await().Session;
break;
}
catch (Exception ex)
{
_Log.Error(ex.Message, ex);
Thread.Sleep(1000);
} }
}

  链接建立成功之后,触发mina.net 内部机制的SessionCreated 方法,登录用户

public override void SessionCreated(Mina.Core.Session.IoSession session)
{
try
{
MyBaseMessage message = new LoginRequestMessage(ClientConfig.ClientAddr,ClientConfig.SharedSecret);
(message as LoginRequestMessage).SetAutherString();
session.Write(message);
}
catch (Exception ex)
{
_Log.Error(ex.Message, ex);
}
finally
{
base.SessionCreated(session);
}
}

  重写MessageReceived方法,收到服务器消息之后,处理相应事件

  /// <summary>
/// 收到消息时 触发--处理消息,给服务器发送处理结果
/// </summary>
/// <param name="session"></param>
/// <param name="message"></param>
public override void MessageReceived(Mina.Core.Session.IoSession session, object message)
{
try
{
if (message is MyBaseMessage)
{
var m = message as MyBaseMessage; if (HeaderDic.Keys.Any(p=>p==m.GetCommandType()))
{
var messageHeader = HeaderDic[m.GetCommandType()]; messageHeader.Handle(session,m);
} }
}
catch (Exception ex)
{
_Log.Error(ex.Message, ex);
}
finally
{
base.MessageReceived(session, message);
} }

  重写 SessionClosed 事件,关闭session时,通知服务器,客户端已关闭链接

 /// <summary>
/// 关闭Session时 触发-发送关闭消息
/// </summary>
/// <param name="session"></param>
public override void SessionClosed(Mina.Core.Session.IoSession session)
{
try
{
while (true)
{
try
{
if (Connector != null)
{
if (!Connector.Disposed)
{
session = Connector.Connect().Await().Session; break;
}
else
{
break;
}
}
}
catch (Exception ex)
{
Thread.Sleep(1000);
}
}
}
catch (Exception ex)
{
_Log.Error(ex.Message, ex);
}
finally
{
base.SessionClosed(session);
}
}

  重写 ExceptionCaught 方法,发生异常时,关闭链接

/// <summary>
/// 发生异常时 触发,关闭session 重新登录
/// </summary>
/// <param name="session"></param>
/// <param name="cause"></param>
public override void ExceptionCaught(Mina.Core.Session.IoSession session, Exception cause)
{
try
{
session.Close(true);
_Log.Error(cause.Message, cause);
}
catch (Exception ex)
{
_Log.Error(ex.Message, ex);
}
finally
{
base.ExceptionCaught(session, cause);
}
}

  重写 SessionIdle 方法,session空闲时,测试心跳

 /// <summary>
/// Session 空闲时 发生
/// </summary>
/// <param name="session"></param>
/// <param name="status"></param>
public override void SessionIdle(Mina.Core.Session.IoSession session, Mina.Core.Session.IdleStatus status)
{
try
{
MyBaseMessage message = new DetectionMessage(); session.Write(message);
}
catch (Exception ex)
{
_Log.Error(ex.Message, ex);
}
finally
{
base.SessionIdle(session, status);
}
}

  构建协议编解码器工厂

 public class MyMinaCodecFactory : DemuxingProtocolCodecFactory
{
public MyMinaCodecFactory()
{
AddMessageEncoder(new MyMinaEncoder());
AddMessageDecoder(new MyMinaDecoder());
}
}

  编码器工厂,将对象 序列号成 bytes 数据

 public class MyMinaEncoder : IMessageEncoder<MyBaseMessage>
{ public void Encode(IoSession session, MyBaseMessage message, IProtocolEncoderOutput output)
{
IoBuffer buf = IoBuffer.Allocate(12);
buf.AutoExpand = true; var messageBytes = message.EncodeMessage(); buf.Put(messageBytes);
buf.Flip(); session.Write(buf); } public void Encode(IoSession session, object message, IProtocolEncoderOutput output)
{
IoBuffer buf = IoBuffer.Allocate(12);
buf.AutoExpand = true; if (message is MyBaseMessage)
{
var m = message as MyBaseMessage; var messageBytes = m.EncodeMessage(); buf.Put(messageBytes);
buf.Flip(); } session.Write(buf); }
}

  解码器工厂,将字节转换为对象

 public class MyMinaDecoder : IMessageDecoder
{
public ILog _Log = LogManager.GetLogger("MessageHandler"); public MessageDecoderResult Decodable(IoSession session,IoBuffer input)
{
try
{
if (input.Remaining < CommandConfig.messageHeaderLength)
{
return MessageDecoderResult.NeedData;
}
var headerBytes = new byte[CommandConfig.messageHeaderLength];
for (int i = 0; i < CommandConfig.messageHeaderLength; i++)
{
headerBytes[i] = input.Get(i);
} var lengthBytes = new byte[4];
var commandIdBytes = new byte[4];
var sequenceBytes = new byte[4]; Array.Copy(headerBytes, 0, lengthBytes, 0, 4);
Array.Copy(headerBytes, 4, commandIdBytes, 0, 4);
Array.Copy(headerBytes, 8, sequenceBytes, 0, 4); var messageLength = lengthBytes.ByteToUint();//Convert.ToInt32(Encoding.Default.GetString(headerBytes, 0, 4)); var messageCommand = commandIdBytes.ByteToUint();//(uint)Convert.ToInt32(Encoding.Default.GetString(headerBytes, 4, 4)); if (messageCommand==CommandConfig.connect
|| messageCommand == CommandConfig.connectResp
|| messageCommand == CommandConfig.terminate
|| messageCommand == CommandConfig.terminateResp
|| messageCommand == CommandConfig.notify
|| messageCommand == CommandConfig.notifyResp
|| messageCommand == CommandConfig.cmppActiveTest
|| messageCommand == CommandConfig.cmppActiveTestResp)
{
return MessageDecoderResult.OK;
}
return MessageDecoderResult.NotOK; }
catch (Exception ex)
{
_Log.Error(ex.Message, ex);
return MessageDecoderResult.NeedData;
}
}
}

  结语:

博客写的不多,不喜勿碰,谢谢

欢迎指点和纠正

mina.net 梳理的更多相关文章

  1. 【初码干货】在Window Server 2016中使用Web Deploy方式发布.NET Web应用的重新梳理

    在学习和工作的过程中,发现很多同事.朋友,在做.NET Web应用发布的时候,依然在走 生成-复制到服务器 这样的方式,稍微高级一点的,就是先发布到本地,再上传到服务器 这种方式不仅效率低下,而且不易 ...

  2. JAVA通信系列二:mina入门总结

    一.学习资料 Mina入门实例(一) http://www.cnblogs.com/juepei/p/3939119.html Mina入门教程(二)----Spring4 集成Mina http:/ ...

  3. Mina、Netty、Twisted一起学(八):HTTP服务器

    HTTP协议应该是目前使用最多的应用层协议了,用浏览器打开一个网站就是使用HTTP协议进行数据传输. HTTP协议也是基于TCP协议,所以也有服务器和客户端.HTTP客户端一般是浏览器,当然还有可能是 ...

  4. [SQL] SQL 基础知识梳理(一)- 数据库与 SQL

    SQL 基础知识梳理(一)- 数据库与 SQL [博主]反骨仔 [原文地址]http://www.cnblogs.com/liqingwen/p/5902856.html 目录 What's 数据库 ...

  5. [SQL] SQL 基础知识梳理(二) - 查询基础

    SQL 基础知识梳理(二) - 查询基础 [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5904824.html 序 这是<SQL 基础知识梳理( ...

  6. [SQL] SQL 基础知识梳理(三) - 聚合和排序

    SQL 基础知识梳理(三) - 聚合和排序 [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5926689.html 序 这是<SQL 基础知识梳理 ...

  7. [SQL] SQL 基础知识梳理(四) - 数据更新

    SQL 基础知识梳理(四) - 数据更新 [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5929786.html 序 这是<SQL 基础知识梳理( ...

  8. [SQL] SQL 基础知识梳理(五) - 复杂查询

    SQL 基础知识梳理(五) - 复杂查询 [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5939796.html 序 这是<SQL 基础知识梳理( ...

  9. 【Spring-web】RestTemplate源码学习——梳理内部实现过程

    2016-12-28 by 安静的下雪天  http://www.cnblogs.com/quiet-snowy-day/p/6228198.html  提示:使用手机浏览时请注意,图多费流量. 本篇 ...

随机推荐

  1. php学习测试题目

    <?php     header("content-type:text/html;charset=utf-8");   /*    1.银行给客户每天万分之四的利率,本金10 ...

  2. LBPL--基于Asp.net、 quartz.net 快速开发定时服务的插件化项目

    LBPL 这一个基于Asp.net. quartz.net 快速开发定时服务的插件化项目 由于在实际项目开发中需要做定时服务的操作,大体上可以理解为:需要动态化监控定时任务的调度系统. 为了实现快速开 ...

  3. 配置Windows Server 2012服务器远程连接支持多人同时登陆

    1.运行输入gpedit.msc 进入组策略 2.计算机配置--管理模版--windows组件--远程桌面服务--远程桌面会话主机--连接 3.找到限制连接的数量,启用,并改为100. 4.找到 将远 ...

  4. Sublime安装Package Control插件

    一.简易安装 打开Sublime text的console.打开console的快捷时ctrl+,或者在菜单栏点击View->Show Sonsole`.打开后将下面的代码复制到console中 ...

  5. Struts2的validator和WEB-INF下页面交互以及路径问题

    当我使用短路校验器时(客户端),在页面下方老是出来 FreeMarker template error!然后我就把我的页面都放在了WEB-INF中,结果很多路径都不对了,因为客户端是没有直接访问Str ...

  6. iOS开发之UIDynamic

    1.概述 什么是UIDynamic? UIDynamic是从iOS 7开始引入的一种新技术,隶属于UIKit框架. 可以认为是一种物理引擎,能模拟和仿真现实生活中的物理现象.比如:重力.弹性碰撞等现象 ...

  7. Sublime Text 3下载-汉化-插件配置

    Sublime Text 3下载 不用说是上官方下载地址:http://www.sublimetext.com/3 Sublime Text 3 免费使用方法 Sublime Text 2的时候还有一 ...

  8. Linux线程的创建

    一.线程与进程的区别 1.线程自己不拥有系统资源,只拥有一点儿在运行中必不可少的资源,但它可与同属一个进程的其它线程共享进程所拥有的全部资源. 2.进程是资源分配的基本单位.所有与该进程有关的资源,都 ...

  9. String 类的实现(5)String常用函数

      2 #include<iostream> 3 #include<stdio.h> 4 #include<assert.h> 5 #include <iom ...

  10. 【转】如何实现Flex页面跳转

    其实对于这个题目是不恰当的,因为flex中是没有页面这个概念的,页面在flex里面其实就是一个个的Canvas,vbox,hbox等等之类的东西,看到的不同页面的切换,就是这些元素一层层的堆积,或者替 ...