v7.3.2018.0614   重构高性能资源池,减少GC压力,增加线程池,让异步任务得到平等竞争CPU的机会
大石头 authored at 2018-06-14 17:56:44
1.31 KiB
X
using System;
using System.IO;
using NewLife.Net.Sockets;

namespace NewLife.Net.Application
{
    /// <summary>Echo服务。把客户端发来的数据原样返回。</summary>
    public class EchoServer : NetServer
    {
        /// <summary>实例化一个Echo服务</summary>
        public EchoServer()
        {
            // 默认7端口
            Port = 7;

            Name = "Echo服务";
        }

        /// <summary>已重载。</summary>
        /// <param name="session"></param>
        /// <param name="stream"></param>
        protected override void OnReceive(INetSession session, Stream stream)
        {
            if (stream.Length == 0) return;

            var p = stream.Position;
            if (stream.Length > 100)
                WriteLog("Echo {0} [{1}]", session.Remote, stream.Length);
            else
                WriteLog("Echo {0} [{1}] {2}", session.Remote, stream.Length, stream.ToStr());

            //Send(e.Socket, e.Buffer, e.Offset, stream.Length, e.RemoteEndPoint);
            //session.Send(e.Buffer, e.Offset, stream.Length, e.RemoteEndPoint);
            //session.Send(e.Buffer, e.Offset, stream.Length);
            stream.Position = p;
            //session.Send(stream);
            session.Send(stream.ReadBytes());
        }
    }
}