ningshuxia
2022-11-18 b4093bc3cbe7e1c347bd9d366528a74b8a18f570
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IStation.Model;
using SuperSocket;
using SuperSocket.Channel;
using SuperSocket.ProtoBase;
using SuperSocket.Server;
 
namespace IStation.Server 
{
    /// <summary>
    /// Session会话
    /// </summary>
    internal class MySession : AppSession,Model.IMonitorDataDockingSession
    {
        /// <summary>
        /// 会话名称
        /// </summary>
        public string SessionName { get; set; }
 
        /// <summary>
        /// 注册码
        /// </summary>
        public string RegisterCode { get; set; }
 
        /// <summary>
        /// 心跳包
        /// </summary>
        public string Heartbeat { 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();
        }
 
 
    }
}