zrg
2024-08-16 18865d8bf24382e850e661dec6cca0e8b9dba6ae
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
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;
 
namespace Taobao.Top.Link.Channel.TCP
{
    /// <summary>the channel that can send message to client via raw tcp
    /// </summary>
    public class TcpServerChannelSender : IServerChannelSender
    {
        private IDictionary<object, object> _context;
        TcpClient _tcpClient;
 
        public bool IsOpen
        {
            get { return this._tcpClient.Connected; }
        }
 
        public TcpServerChannelSender(TcpClient tcpClient)
        {
            this._tcpClient = tcpClient;
            this._context = new Dictionary<object, object>();
        }
 
        public object GetContext(object key)
        {
            object val;
            return this._context.TryGetValue(key, out val) ? val : null;
        }
        public void SetContext(object key, object value)
        {
            this._context[key] = value;
        }
 
        public void Send(byte[] data)
        {
            this._tcpClient.GetStream().Write(data, 0, data.Length);
        }
        public void Close(string reason)
        {
            this._tcpClient.Close();
        }
    }
}