using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
using SuperSocket;
|
using SuperSocket.Channel;
|
using SuperSocket.ProtoBase;
|
using SuperSocket.Server;
|
|
namespace IStation.Server
|
{
|
/// <summary>
|
/// Session会话
|
/// </summary>
|
internal class MySession : AppSession
|
{
|
/// <summary>
|
/// 会话名称
|
/// </summary>
|
public string SessionName { get; set; }
|
|
|
/// <summary>
|
/// 是否连接
|
/// </summary>
|
public bool IsConnected
|
{
|
get
|
{
|
return this.State == SessionState.Connected;
|
}
|
}
|
|
/// <summary>
|
/// 关闭事件
|
/// </summary>
|
public event Action SessionClosedEvent;
|
|
protected override ValueTask OnSessionConnectedAsync()
|
{
|
LogHelper.Info(this.RemoteEndPoint.ToString() + "连接成功!");
|
return base.OnSessionConnectedAsync();
|
}
|
|
protected override ValueTask OnSessionClosedAsync(CloseEventArgs e)
|
{
|
this.SessionClosedEvent?.Invoke();
|
LogHelper.Info($"{this.SessionName},Socket连接关闭,关闭原因:{e.Reason}");
|
return base.OnSessionClosedAsync(e);
|
}
|
|
/// <summary>
|
/// 发送文本
|
/// </summary>
|
public async void Send(string message)
|
{
|
var bts=Encoding.UTF8.GetBytes(message);
|
var memory=new ReadOnlyMemory<byte>(bts);
|
await ((IAppSession)this).SendAsync(memory);
|
}
|
|
/// <summary>
|
/// 发送字节
|
/// </summary>
|
public async void Send(byte[] data)
|
{
|
var memory = new ReadOnlyMemory<byte>(data);
|
await ((IAppSession)this).SendAsync(memory);
|
}
|
|
/// <summary>
|
/// 发送字节
|
/// </summary>
|
public void Send(byte[] data, int offset, int length)
|
{
|
var bts = new byte[length];
|
Buffer.BlockCopy(data, offset, bts, 0, length);
|
Send(bts);
|
}
|
|
/// <summary>
|
/// 关闭
|
/// </summary>
|
public async void Close(string reason)
|
{
|
LogHelper.Info($"{this.SessionName} 准备关闭会话,原因:{reason}");
|
await base.CloseAsync();
|
}
|
|
|
}
|
}
|