duhe
2025-04-15 7785befe1ccd0566db7d1e4b9016db8e9d1b6628
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System;
using System.Collections.Generic;
using System.Text;
using Taobao.Top.Link.Channel;
 
namespace Taobao.Top.Link.Endpoints
{
    /// <summary>logic endpoint local proxy object
    /// </summary>
    public class EndpointProxy
    {
        private IList<IChannelSender> _senders;
        private Random _random;
        private EndpointHandler _handler;
 
        /// <summary>get id
        /// </summary>
        public Identity Identity { get; internal set; }
        /// <summary>known by both side, like a sessionId
        /// </summary>
        public string Token { get; internal set; }
 
        public EndpointProxy(EndpointHandler handler)
        {
            this._senders = new List<IChannelSender>();
            this._random = new Random();
            this._handler = handler;
        }
 
        internal void Add(IChannelSender sender)
        {
            if (!this._senders.Contains(sender))
                lock (this._senders)
                    if (!this._senders.Contains(sender))
                        this._senders.Add(sender);
        }
 
        internal void Remove(IChannelSender sender)
        {
            lock (this._senders)
                this._senders.Remove(sender);
        }
 
        internal void Remove(string uri)
        {
            lock (this._senders)
            {
                for (var i = 0; i < this._senders.Count; i++)
                {
                    var channel = this._senders[i] as IClientChannel;
                    if (channel == null)
                        continue;
                    if (!channel.Uri.ToString().Equals(uri))
                        continue;
                    this._senders.Remove(channel);
                    i--;
                }
            }
        }
 
        internal void Close(string uri, string reason)
        {
            lock (this._senders)
            {
                for (var i = 0; i < this._senders.Count; i++)
                {
                    var channel = this._senders[i] as IClientChannel;
                    if (channel == null)
                        continue;
                    if (!channel.Uri.ToString().Equals(uri))
                        continue;
                    channel.Close(reason);
                    this._senders.Remove(channel);
                    i--;
                }
            }
        }
 
        /// <summary>check is there any sender can be used to send
        /// </summary>
        /// <returns></returns>
        public bool hasValidSender()
        {
            foreach (IClientChannel sender in this._senders)
            {
                if (sender.IsConnected)
                    return true;
            }
            return false;
        }
        /// <summary>send message and wait reply
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public IDictionary<string, object> SendAndWait(IDictionary<string, object> message)
        {
            return this.SendAndWait(message, Endpoint.TIMEOUT);
        }
        /// <summary>send message and wait reply
        /// </summary>
        /// <param name="message"></param>
        /// <param name="timeout">timeout in milliseconds</param>
        /// <returns></returns>
        public IDictionary<string, object> SendAndWait(IDictionary<string, object> message, int timeout)
        {
            return this.SendAndWait(null, message, timeout);
        }
        /// <summary>send message and wait reply
        /// </summary>
        /// <param name="sender">use to send, must belong this proxy</param>
        /// <param name="message"></param>
        /// <param name="timeout">timeout in milliseconds</param>
        /// <returns></returns>
        internal IDictionary<string, object> SendAndWait(IChannelSender sender, IDictionary<string, object> message, int timeout)
        {
            return this._handler.SendAndWait(this,
                    this.GetSender(sender),
                    this.CreateMessage(message),
                    timeout);
        }
        /// <summary>send message
        /// </summary>
        /// <param name="message"></param>
        public void Send(IDictionary<string, object> message)
        {
            this.Send(null, message);
        }
        /// <summary>send message
        /// </summary>
        /// <param name="sender">use to send, must belong this proxy</param>
        /// <param name="message"></param>
        internal void Send(IChannelSender sender, IDictionary<string, object> message)
        {
            this._handler.Send(this.CreateMessage(message), this.GetSender(sender));
        }
 
        private Message CreateMessage(IDictionary<string, object> message)
        {
            Message msg = new Message();
            msg.MessageType = MessageType.SEND;
            msg.Content = message;
            msg.Token = this.Token;
            return msg;
        }
        private IChannelSender GetSender(IChannelSender sender)
        {
            if (this._senders.Count == 0)
                throw new ChannelException(Text.E_NO_SENDER);
            if (this._senders.Contains(sender))
                return sender;
            return this._senders[this._random.Next(this._senders.Count)];
        }
    }
}