using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace NetFramework
{
/// <summary>
/// 计算机如何存储大数值的体系结构
/// </summary>
public enum Endian
{
/// <summary>
/// Intel x86,AMD64,DEC VAX
/// </summary>
LITTLE_ENDIAN = ,
/// <summary>
/// Sun SPARC, Motorola 68000,Java Virtual Machine
/// </summary>
BIG_ENDIAN = ,
} public class ReceiveQueue
{
#region zh-CHS 类常量 | en Class Constants
/// <summary>
/// 字节默认的大小
/// </summary>
private const long BUFFER_SIZE = * ;
#endregion #region zh-CHS 私有成员变量 | en Private Member Variables
/// <summary>
/// 字节的头位置
/// </summary>
private long m_Head;
/// <summary>
/// 字节的尾位置
/// </summary>
private long m_Tail;
/// <summary>
/// 字节的数组
/// </summary>
private byte[] m_Buffer = new byte[BUFFER_SIZE];
/// <summary>
///
/// </summary>
private object m_LockBuffer = new object();
#endregion #region zh-CHS 属性 | en Properties
#region zh-CHS 私有成员变量 | en Private Member Variables
/// <summary>
/// 字节的大小
/// </summary>
private long m_Size;
#endregion
/// <summary>
/// 环绕缓冲区内的数据大小
/// </summary>
public long Length
{
get { return m_Size; }
} #endregion #region zh-CHS 方法 | en Method
/// <summary>
/// 给出使用环绕缓冲区内的数据
/// </summary>
/// <param name="byteBuffer">要复制到的数据的数组</param>
/// <param name="iOffset">要复制到数组的长度偏移量</param>
/// <param name="iSize">要复制多少长度的数据</param>
/// <returns>返回实际读取到的字节数</returns>
public long Dequeue(byte[] byteBuffer, long iOffset, long iSize)
{
if (byteBuffer == null)
throw new ArgumentNullException("byteBuffer", "ReceiveQueue.Dequeue(...) - byteBuffer == null error!"); if (iOffset < || iOffset >= byteBuffer.Length)
throw new Exception("ReceiveQueue.Dequeue(...) - iOffset < 0 || iOffset >= byteBuffer.Length error!"); if (iSize < || iSize > byteBuffer.Length) // 如果iLength == 0就返回空,如果iLength == 0就跳过
throw new Exception("ReceiveQueue.Dequeue(...) - iSize < 0 || iSize > byteBuffer.Length error!"); if ((byteBuffer.Length - iOffset) < iSize)
throw new Exception("ReceiveQueue.Dequeue(...) - ( byteBuffer.Length - iOffset ) < iSize error!"); if (iSize == )
return ; lock (m_LockBuffer)
{
if (iSize > m_Size)
iSize = m_Size; if (m_Head < m_Tail)
Buffer.BlockCopy(m_Buffer, (int)m_Head, byteBuffer, (int)iOffset, (int)iSize);
else
{
long rightLength = m_Buffer.Length - m_Head; if (rightLength >= iSize)
Buffer.BlockCopy(m_Buffer, (int)m_Head, byteBuffer, (int)iOffset, (int)iSize);
else
{
Buffer.BlockCopy(m_Buffer, (int)m_Head, byteBuffer, (int)iOffset, (int)rightLength);
Buffer.BlockCopy(m_Buffer, , byteBuffer, (int)(iOffset + rightLength), (int)(iSize - rightLength));
}
} m_Head = (m_Head + iSize) % m_Buffer.Length;
m_Size -= iSize; if (m_Size == )
{
m_Head = ;
m_Tail = ;
}
} return iSize;
} /// <summary>
/// 压入数据至环绕缓冲区内
/// </summary>
/// <param name="byteBuffer"></param>
/// <param name="iOffset"></param>
/// <param name="iSize"></param>
public void Enqueue(byte[] byteBuffer, long iOffset, long iSize)
{
if (byteBuffer == null)
throw new ArgumentNullException("byteBuffer", "ReceiveQueue.Enqueue(...) - byteBuffer == null error!"); if (iOffset < || iOffset >= byteBuffer.Length)
throw new Exception("ReceiveQueue.Enqueue(...) - iOffset < 0 || iOffset >= byteBuffer.Length error!"); if (iSize < || iSize > byteBuffer.Length) // 如果iLength == 0就返回空,如果iLength == 0就跳过
throw new Exception("ReceiveQueue.Enqueue(...) - iSize < 0 || iSize > byteBuffer.Length error!"); if ((byteBuffer.Length - iOffset) < iSize)
throw new Exception("ReceiveQueue.Enqueue(...) - ( byteBuffer.Length - iOffset ) < iSize error!"); lock (m_LockBuffer)
{
if ((m_Size + iSize) >= m_Buffer.Length)
SetCapacityInLock((m_Size + iSize + ) & ~); // 总是以2048的倍数来增大字节数, :( 弄得我老半天才明白原理呢! if (m_Head < m_Tail)
{
long rightLength = m_Buffer.Length - m_Tail; if (rightLength >= iSize)
Buffer.BlockCopy(byteBuffer, (int)iOffset, m_Buffer, (int)m_Tail, (int)iSize);
else
{
Buffer.BlockCopy(byteBuffer, (int)iOffset, m_Buffer, (int)m_Tail, (int)rightLength);
Buffer.BlockCopy(byteBuffer, (int)(iOffset + rightLength), m_Buffer, , (int)(iSize - rightLength));
}
}
else
Buffer.BlockCopy(byteBuffer, (int)iOffset, m_Buffer, (int)m_Tail, (int)iSize); m_Tail = (m_Tail + iSize) % m_Buffer.Length;
m_Size += iSize;
}
} /// <summary>
/// 清除数据的信息,不清除数据缓冲,用于下次使用
/// </summary>
public void Clear()
{
lock (m_LockBuffer)
{
m_Head = ;
m_Tail = ;
m_Size = ;
} } /// <summary>
///
/// </summary>
private Endian m_Endian = Endian.BIG_ENDIAN; /// <summary>
/// 包的长度
/// </summary>
public const int PacketLengthSize = ; /// <summary>
/// 包的长度
/// </summary>
public const int PacketHeadLengthSize = ; /// <summary>
/// 给出数据包的长度
/// </summary>
/// <returns></returns>
public int GetPacketLength()
{
int iReturn = ; lock (m_LockBuffer)
{
if (PacketLengthSize > m_Size)
return ; if (m_Head + PacketLengthSize < m_Buffer.Length)
{
// 保证要读取的数据在字节数组里
var index = m_Head; // 读四字节长度
if (m_Endian == Endian.LITTLE_ENDIAN)
return (m_Buffer[index] << ) | (m_Buffer[index+] << ) | (m_Buffer[index+] << ) | m_Buffer[index+];
else
return m_Buffer[index] | (m_Buffer[index + ] << ) | (m_Buffer[index + ] << ) | (m_Buffer[index + ] << ); //if (m_Endian == Endian.LITTLE_ENDIAN)
// return (m_Buffer[index] << 8) | (m_Buffer[index + 1]);
//else
// return m_Buffer[index] | (m_Buffer[index + 1] << 8);
}
} return iReturn;
} /// <summary>
/// 给出数据包头部的长度
/// </summary>
/// <returns></returns>
public int GetPacketHeadLength()
{
int iReturn = ; lock (m_LockBuffer)
{
if (PacketLengthSize + PacketHeadLengthSize > m_Size)
return ; if (m_Head + PacketLengthSize + PacketHeadLengthSize < m_Buffer.Length)
{
// 保证要读取的数据在字节数组里
var index = m_Head + PacketLengthSize; // 读四字节长度
if (m_Endian == Endian.LITTLE_ENDIAN)
return (m_Buffer[index] << ) | (m_Buffer[index + ] << ) | (m_Buffer[index + ] << ) | m_Buffer[index + ];
else
return m_Buffer[index] | (m_Buffer[index + ] << ) | (m_Buffer[index + ] << ) | (m_Buffer[index + ] << ); //if (m_Endian == Endian.LITTLE_ENDIAN)
// return (m_Buffer[index] << 8) | (m_Buffer[index + 1]);
//else
// return m_Buffer[index] | (m_Buffer[index + 1] << 8);
}
} return iReturn;
} #endregion #region zh-CHS 私有方法 | en Private Method
/// <summary>
/// 扩大缓冲数据的大小(当前都在锁中操作,因此不需要锁定的)
/// </summary>
/// <param name="iCapacity"></param>
private void SetCapacityInLock(long iCapacity)
{
byte[] newBuffer = new byte[iCapacity]; if (m_Size > )
{
if (m_Head < m_Tail)
Buffer.BlockCopy(m_Buffer, (int)m_Head, newBuffer, , (int)m_Size);
else
{
long rightLength = m_Buffer.Length - m_Head; Buffer.BlockCopy(m_Buffer, (int)m_Head, newBuffer, , (int)rightLength);
Buffer.BlockCopy(m_Buffer, , newBuffer, (int)rightLength, (int)m_Tail);
}
} m_Head = ;
m_Tail = m_Size;
m_Buffer = newBuffer;
}
#endregion
}
}
while (true)
{
if (e.ReceiveBuffer != null)
{
data = new byte[e.BytesReceived];
Array.Copy(e.ReceiveBuffer, e.Offset, data, , e.BytesReceived);
nMsgLen = e.BytesReceived;
}
if (nMsgLen <= )
{
break;
} if (package_len > && head_len > )
{
e.ReceiveBufferQueue.Enqueue(data, , e.BytesReceived);
if (e.ReceiveBufferQueue.Length == package_len + )
{
lock (this)
{
package_len = ;
head_len = ;
byte[] temp = new byte[e.ReceiveBufferQueue.Length];
int len = (int)e.ReceiveBufferQueue.Dequeue(temp, , e.ReceiveBufferQueue.Length);
ExecGateBuffers(GateIdx, Gate, temp, len);
M2Share.MainOutMessage(string.Format("=========={0}", len));
break;
}
}
} if (package_len > && head_len == )
{
Array.Reverse(data);
e.ReceiveBufferQueue.Enqueue(data, , e.BytesReceived);
head_len = e.ReceiveBufferQueue.GetPacketHeadLength();
M2Share.MainOutMessage(string.Format("head_len : {0}", head_len));
} if (package_len == )
{
Array.Reverse(data);
e.ReceiveBufferQueue.Enqueue(data, , e.BytesReceived);
package_len = e.ReceiveBufferQueue.GetPacketLength();
M2Share.MainOutMessage(string.Format("package_len : {0}", package_len));
}
break;
}

ReceiveQueue的更多相关文章

  1. twitter storm 源码走读之5 -- worker进程内部消息传递处理和数据结构分析

    欢迎转载,转载请注明出处,徽沪一郎. 本文从外部消息在worker进程内部的转化,传递及处理过程入手,一步步分析在worker-data中的数据项存在的原因和意义.试图从代码实现的角度来回答,如果是从 ...

  2. twitter storm源码走读之3--topology提交过程分析

    概要 storm cluster可以想像成为一个工厂,nimbus主要负责从外部接收订单和任务分配.除了从外部接单,nimbus还要将这些外部订单转换成为内部工作分配,这个时候nimbus充当了调度室 ...

  3. Twitter Storm中Bolt消息传递路径之源码解读

    本文初次发表于storm-cn的google groups中,现以blog的方式再次发表,表明本人徽沪一郎确实读过这些代码,:). Bolt作为task被executor执行,而executor是一个 ...

  4. worker启动executor源码分析-executor.clj

    在"supervisor启动worker源码分析-worker.clj"一文中,我们详细讲解了worker是如何初始化的.主要通过调用mk-worker函数实现的.在启动worke ...

  5. procfs

    https://www.kernel.org/doc/Documentation/filesystems/proc.txt /proc/stat cpu 493610 1050 955506 6140 ...

  6. Storm系列(十六)架构分析之Executor-Bolt

    准备消息循环的数据 函数原型: 1  let[executor-sampler (mk-stats-sampler (:storm-conf executor-data))] 主要功能: 定义tupl ...

  7. Storm系列(十五)架构分析之Executor-Spout

    Spout实现mk-threads接口用于创建与Executor对应的消息循环主函数. defmulti mk-threads executor-selector Mk-threads函数的主消息循环 ...

  8. Storm系列(十四)架构分析之Executor-输入和输出处理

    Executor的数据 mk-executor-data函数用于定义Executor中含有的数据. Executor的输入处理 根据executor-id从Worker的:executor-recei ...

  9. STORM在线业务实践-集群空闲CPU飙高问题排查

    源:http://daiwa.ninja/index.php/2015/07/18/storm-cpu-overload/ 2015-07-18AUTHORDAIWA STORM在线业务实践-集群空闲 ...

随机推荐

  1. 【原】iOS学习之应用之间的操作

    关于应用之间的相互操作,小编一直觉得非常高大上,在一次面试中被面试官一顿暴虐,今天小编就决定学习一下!经过一顿度娘,找到一些博客,不过都比较凌乱,我就打算自己整理一下! 首先要说的是每一个APP都可以 ...

  2. 根据url下载图片

    如题:在我要动手写的时候才发现不搜索根本就是写不出来,究其原因还是因为基础不扎实,由于用的少已经没有能力写出了 首先需要获取url数据流,然后写进文件里即可,仅仅两步可惜我写不出来啊跟着搜来的内容写一 ...

  3. The World's Only Advanced Operating System

    The World's Only Advanced Operating System

  4. [BZOJ3751][NOIP2014] 解方程

    Description 已知多项式方程:a0+a1*x+a2*x^2+...+an*x^n=0 求这个方程在[1,m]内的整数解(n和m均为正整数).   Input 第一行包含2个整数n.m,每两个 ...

  5. windows 10 开始菜单和cortana无法工作的问题

    过了个周末,到了实验室一开机发现报了个关键错误:开始菜单和cortana无法工作. 经过一番google ,发现问题,原来是360禁用了一个服务导致,这个服务是UserManager. 我直接去开启发 ...

  6. 微软官方提供的免费正版 Windows 8.1/Win10/7/XP/Vista 操作系统虚拟机镜像下载

    https://dev.windows.com/en-us/microsoft-edge/tools/vms/windows/

  7. css单位:em,rem解释

    em:所有浏览器都符合:1em=16px;1.具有继承性2.em的根元素是body,当设置了根元素的大小时,大小是定义的数字乘以根元素定义的大小值 rem:1rem=16pxrem不具有继承性,其根元 ...

  8. Html5+Css3制作下拉菜单的三种方式

    一.渐变式改变ol的高度 1.外部为ul标签,在每个li里嵌套一个ol列表2.设置外部li左浮动,内部ol标签绝对定位,外部li标签相对定位3.设置ol的高为0,溢出隐藏4.外部li标签:hover ...

  9. effectiveC++ 内存管理 学习笔记

    1.尽量使用初始化列表而不要再构造函数里赋值,初始化顺序和声明的顺序一致,一些类型如const,引用等,必须使用初始化.对于非内部数据类型成员对象应当采用初始化表,以获取更高的效率.example:B ...

  10. Java_equals和“==”的区别

    1. 对于基本数据类型 它们的比较,应该用“==”,比较的是他们的值. 2. 引用数据类型 “==”判断的是对象是否为同一个,也就是它们内存中的存放地址是否一样,一样,则返回true,否则返回fals ...