Beispiel #1
0
 /// <summary>
 /// 检测该客户端,如果不活动则删除连接
 /// </summary>
 /// <param name="theConnection">指定的客户端连接</param>
 private void RemoveInactiveNode(NODE node)
 {
     if (node.IsActive == false)
     {
         RemoveNode(node);
     }
 }
Beispiel #2
0
 private void RemoveNode(NODE node)
 {
     RWLock_ClientList.EnterWriteLock();
     try
     {
         nodes.Remove(node);
     }
     finally
     {
         RWLock_ClientList.ExitWriteLock();
     }
 }
Beispiel #3
0
        public void AddNode(NODE node)
        {
            RWLock_ClientList.EnterWriteLock();

            try
            {
                nodes.Add(node);
                node.HandleNodeClosed = new Action <NODE>((id => { nodes.Remove(id); }));
            }
            finally
            {
                RWLock_ClientList.ExitWriteLock();
                //调用外部回调函数通知新连接建立
                HandleNodeCreated?.Invoke(this, node);
            }
        }
Beispiel #4
0
 /// <summary>
 /// 关闭指定客户端连接
 /// </summary>
 /// <param name="theConnection">指定的客户端连接</param>
 public void CloseNode(NODE node)
 {
     RemoveNode(node);
     //调用外部回调函数通知连接被关闭
     HandleNodeClosed?.Invoke(this, node);
 }
Beispiel #5
0
        /// <summary>
        /// 接收来自UDP的数据并进行转发
        /// </summary>
        /// <param name="bytes"></param>
        /// <param name="EndpointString"></param>
        public void SendFromUDP(byte[] bytes, string EndpointString)
        {
            HandleUDPRsvMsg?.Invoke(bytes, EndpointString);
            string content = System.Text.Encoding.Default.GetString(bytes);
            string ID      = "";

            if (content.Length >= 19)
            {
                ID = content.Substring(9, 10);
                NODE node = GetTheNode((x =>
                {
                    var Id = (string)x.ID;
                    return(Id == ID);
                }));
                //如果不存在则新建一个NODE
                if (node == null)
                {
                    node = new NODE(ID)
                    {
                        //连接Node消息到Switch上
                        HandleNodeClosed  = HandleNodeClosed == null ? null : new Action <NODE>((nod) => { HandleNodeClosed(this, nod); }),
                        HandleNodeCreated = HandleNodeCreated == null ? null : new Action <NODE>((nod) => { HandleNodeCreated(this, nod); }),
                        HandleUDPAdded    = new Action <string>(target =>
                        {
                            HandleNodeChanged?.Invoke(this, node);
                        }),
                        HandleUDPRemoved = new Action <string>(target =>
                        {
                            HandleNodeChanged?.Invoke(this, node);
                        }),
                        HandleWebSocketAdded = new Action <string>(target =>
                        {
                            HandleNodeChanged?.Invoke(this, node);
                        }),
                        HandleWebSocketRemoved = new Action <string>(target =>
                        {
                            HandleNodeChanged?.Invoke(this, node);
                        }),
                        HandleException = new Action <Exception>(ex =>
                        {
                            HandleException(ex);
                        })
                    };
                    node.ID = ID;
                    AddNode(node);
                }

                //判断连接是否为新连接,如果是则增加消息源
                if (node.UDPConnections.Where(x => { return(EndpointString == x.EndpointString); }).FirstOrDefault() == null)
                {
                    node.AddUDPConnection(EndpointString);
                }
                else
                {
                    node.UDPConnections.Where(x => { return(EndpointString == x.EndpointString); }).FirstOrDefault().Reactive();
                }
                node.Reactive();
                //分发消息给所有在线的Websocket端口
                lock (node.WebsocketConnections)
                {
                    foreach (WSConnection ws in node.WebsocketConnections.ToArray())
                    {
                        HandleWebSocketSendMsg?.Invoke(bytes, ws.EndpointString);
                    }
                }
                //分发消息给所有在线的UDP端口
                lock (node.UDPConnections)
                {
                    foreach (UDPConnection udp in node.UDPConnections.ToArray())
                    {
                        HandleUDPSendMsg?.Invoke(bytes, udp.EndpointString);
                    }
                }
            }
        }
Beispiel #6
0
 /// <summary>
 /// 当某连接不活动超时激活该函数
 /// </summary>
 private void HandleConnClientClose(NODE node)
 {
     RemoveNode(node);
 }