基线 SVN r240
SVN-Revision: r240
This commit is contained in:
@@ -0,0 +1,183 @@
|
||||
/******************************
|
||||
* 说明:日志输入Socket协议
|
||||
* 创建人:龚宇超
|
||||
* 创建日期:2018-01-23
|
||||
* 修改人:
|
||||
* 修改日期:
|
||||
* 修改备注:
|
||||
* 版本:1.0.0.0
|
||||
******************************/
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Net.Sockets;
|
||||
using System.IO;
|
||||
using log4net.Core;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace Lskj.SocketService
|
||||
{
|
||||
/// <summary>
|
||||
/// 日志输入Socket协议
|
||||
/// </summary>
|
||||
public class LogOutputSocketProtocol : BaseSocketProtocol
|
||||
{
|
||||
private LogFixedBuffer _logFixedBuffer;
|
||||
public LogFixedBuffer LogFixedBuffer { get { return _logFixedBuffer; } }
|
||||
|
||||
public LogOutputSocketProtocol(AsyncSocketServer asyncSocketServer, AsyncSocketUserToken asyncSocketUserToken)
|
||||
: base(asyncSocketServer, asyncSocketUserToken)
|
||||
{
|
||||
_socketFlag = "LogOutput";
|
||||
_logFixedBuffer = new LogFixedBuffer();
|
||||
lock (SocketApp.AsyncSocketSvr.LogOutputSocketProtocolMgr)
|
||||
{
|
||||
SocketApp.AsyncSocketSvr.LogOutputSocketProtocolMgr.Add(this);
|
||||
}
|
||||
|
||||
SendResponse();
|
||||
}
|
||||
|
||||
public override void Close()
|
||||
{
|
||||
lock (SocketApp.AsyncSocketSvr.LogOutputSocketProtocolMgr)
|
||||
{
|
||||
SocketApp.AsyncSocketSvr.LogOutputSocketProtocolMgr.Remove(this);
|
||||
}
|
||||
}
|
||||
|
||||
public override bool ProcessReceive(byte[] buffer, int offset, int count)
|
||||
{
|
||||
_activeDT = DateTime.UtcNow;
|
||||
if (count == 1)
|
||||
{
|
||||
if (buffer[0] == (byte)Keys.Escape)
|
||||
return false;
|
||||
else
|
||||
return SendResponse();
|
||||
}
|
||||
else
|
||||
return SendResponse();
|
||||
}
|
||||
|
||||
public override bool SendCallback()
|
||||
{
|
||||
bool result = base.SendCallback();
|
||||
if (_logFixedBuffer.DataCount > 0)
|
||||
{
|
||||
result = DoSendBuffer(_logFixedBuffer.FixedBuffer, 0, _logFixedBuffer.DataCount);
|
||||
_logFixedBuffer.Clear();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
//主动发送,如果没有回调的时候,需要主动下发,否则等待回调
|
||||
public bool InitiativeSend()
|
||||
{
|
||||
if (!_sendAsync)
|
||||
{
|
||||
return SendCallback();
|
||||
}
|
||||
else
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool SendResponse()
|
||||
{
|
||||
return InitiativeSend();
|
||||
}
|
||||
}
|
||||
|
||||
public class LogOutputSocketProtocolMgr : Object
|
||||
{
|
||||
private List<LogOutputSocketProtocol> _list;
|
||||
|
||||
public LogOutputSocketProtocolMgr()
|
||||
{
|
||||
_list = new List<LogOutputSocketProtocol>();
|
||||
}
|
||||
|
||||
public int Count()
|
||||
{
|
||||
return _list.Count;
|
||||
}
|
||||
|
||||
public LogOutputSocketProtocol ElementAt(int index)
|
||||
{
|
||||
return _list.ElementAt(index);
|
||||
}
|
||||
|
||||
public void Add(LogOutputSocketProtocol value)
|
||||
{
|
||||
_list.Add(value);
|
||||
}
|
||||
|
||||
public void Remove(LogOutputSocketProtocol value)
|
||||
{
|
||||
_list.Remove(value);
|
||||
}
|
||||
}
|
||||
|
||||
public class LogFixedBuffer : Object
|
||||
{
|
||||
private byte[] _fixedBuffer;
|
||||
public byte[] FixedBuffer { get { return _fixedBuffer; } }
|
||||
private int _dataCount;
|
||||
public int DataCount { get { return _dataCount; } }
|
||||
|
||||
public LogFixedBuffer()
|
||||
{
|
||||
_fixedBuffer = new byte[1024 * 16]; //申请大小为16K
|
||||
_dataCount = 0;
|
||||
}
|
||||
|
||||
public void WriteBuffer(byte[] buffer, int offset, int count)
|
||||
{
|
||||
if ((_fixedBuffer.Length - _dataCount) >= count) //如果长度够,则复制内存
|
||||
{
|
||||
Array.Copy(buffer, offset, _fixedBuffer, _dataCount, count);
|
||||
_dataCount = _dataCount + count;
|
||||
}
|
||||
}
|
||||
|
||||
public void WriteBuffer(byte[] buffer)
|
||||
{
|
||||
WriteBuffer(buffer, 0, buffer.Length);
|
||||
}
|
||||
|
||||
public void WriteString(string value)
|
||||
{
|
||||
byte[] tmpBuffer = Encoding.ASCII.GetBytes(value);
|
||||
WriteBuffer(tmpBuffer);
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
_dataCount = 0;
|
||||
}
|
||||
}
|
||||
|
||||
//扩展log4net的日志输出
|
||||
class LogSocketAppender : log4net.Appender.AppenderSkeleton
|
||||
{
|
||||
public LogSocketAppender()
|
||||
{
|
||||
Name = "LogSocketAppender";
|
||||
}
|
||||
|
||||
protected override void Append(LoggingEvent loggingEvent)
|
||||
{
|
||||
string strLoggingMessage = RenderLoggingEvent(loggingEvent);
|
||||
byte[] tmpBuffer = Encoding.Default.GetBytes(strLoggingMessage);
|
||||
lock (SocketApp.AsyncSocketSvr.LogOutputSocketProtocolMgr)
|
||||
{
|
||||
for (int i = 0; i < SocketApp.AsyncSocketSvr.LogOutputSocketProtocolMgr.Count(); i++)
|
||||
{
|
||||
SocketApp.AsyncSocketSvr.LogOutputSocketProtocolMgr.ElementAt(i).LogFixedBuffer.WriteBuffer(tmpBuffer);
|
||||
SocketApp.AsyncSocketSvr.LogOutputSocketProtocolMgr.ElementAt(i).InitiativeSend();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user