Exemplo n.º 1
0
        //------------------------------------------------------------------------------------------------------------------------
        private void _YPServer_OnNewChannel(Server Server, YPChannel.Channel Channel)
        {
            var id = new LANDiscoverer.RemoteEndpointID()
            {
                IPAddress = Channel.RemoteIdentifier,
                ID        = 0,
            };

            lock (RemoteNodes)
            {
                //get or create entry for remote node
                var remInfo = RemoteNodes.TryGetOrDefault(id);
                if (remInfo == null)
                {
                    //create entry for remote node
                    remInfo = new RemoteNode(Node, TimeSpan.FromMinutes(5), this)
                    {
                        RemoteEndpointID = id,
                        DiscoveryMessage = null,
                        RemoteNodeKey    = default(NodeKey),
                    };

                    //add to discovered remote nodes
                    RemoteNodes.ForceAdd(id, remInfo);
                }

                //hookevents
                hookNewRemoteNodeEvents(remInfo);

                //setup channel
                remInfo.SetupChannel(Channel);
            }
        }
Exemplo n.º 2
0
        //-------------------------------------------------------------------------------------------------------------------------
        private bool YPChannel_Negotiation(YPChannel.Channel Channel)
        {
            DebugEx.TraceLog($"RemoteNode : Negotiation started with node. (ip:{IPAddress} port:{RemotePort} nodekey:{RemoteNodeKey})");

            //check
            if (Channel.ChannelRole != YPChannel.ChannelRole.Server)
            {
                DebugEx.Assert("Should not be here");
                return(false);
            }

            //---------------------
            // Association Request
            //---------------------
            {
                //send request
                var req = new AssociationRequest()
                {
                    UnsafeNodeKey = Node.NodeKey,
                    PublicKey     = null,
                };
                var rsp = Channel.SendRequest <AssociationResponse>(req);
                if (rsp == null)
                {
                    return(false);
                }

                //keep discovery message
                DiscoveryMessage    = rsp.DiscoveryMessage;
                RemoteEndpointID.ID = rsp.DiscoveryMessage.Id;
                //examine discovery message
                //..

                //keep node key
                RemoteNodeKey = rsp.UnsafeNodeKey;
                if (RemoteNodeKey.IsInvalid)
                {
                    return(false);
                }

                //keep public key

                /*
                 * if (rsp.PublicKey == null || rsp.PublicKey.Length == 0 || rsp.PublicKey.Length > 1024 * 20)
                 *  return false;
                 * else*/
                PublicKey = rsp.PublicKey;
            }

            //---------------------
            // Authentication Request
            //---------------------
            //..

            //all ok
            return(true);
        }
Exemplo n.º 3
0
 //-------------------------------------------------------------------------------------------------------------------------
 void unhookChannel(YPChannel.Channel _channel)
 {
     if (_channel == null)
     {
         return;
     }
     _channel.OnClosedEvent     -= YPChannel_OnClosedEvent;
     _channel.OnOpenEvent       -= YPChannel_OnOpenEvent;
     _channel.OnMessageReceived -= YPChannel_OnMessageReceived;
     if (_channel.ChannelRole == YPChannel.ChannelRole.Server)
     {
         _channel.NegotiationHandler = null;
     }
 }
Exemplo n.º 4
0
        //-------------------------------------------------------------------------------------------------------------------------
        private void YPChannel_OnMessageReceived(YPChannel.Channel Channel, YPChannel.YPMessage Message)
        {
            var _msg = Message.Payload;

            //handle negotiation
            if (_msg is AssociationRequest && Channel.ChannelRole == YPChannel.ChannelRole.Client)
            {
                DebugEx.TraceLog($"RemoteNode : Sending negotiation AssociationResponse. (ip:{IPAddress} port:{RemotePort} nodekey:{RemoteNodeKey})");
                var rsp = new AssociationResponse()
                {
                    DiscoveryMessage = NodeDiscoverManager.DiscoveryMessage,
                    UnsafeNodeKey    = Node.NodeKey,
                    PublicKey        = null,
                };
                Channel.SendResponseAsync(rsp, Message.MessageID);
            }
            else if (_msg is AuthenticationRequest && Channel.ChannelRole == YPChannel.ChannelRole.Client)
            {
                //...
            }
            else if (_msg is VirtualBlockEventMsg)
            {
                var msg = _msg as VirtualBlockEventMsg;
                var rsp = new GenericRsp()
                {
                    IsSuccess = false
                };
                //validate msg
                if (msg.BlockEvents.Any(b => ((BlockKey)b.BlockKey).GraphKey.NodeKey != Node.NodeKey))
                {
                    DebugEx.Assert("Received VirtualBlockEventMsg that was not for me");
                    Channel.SendResponse(rsp, Message.MessageID);
                    return;
                }
                //respond with success
                rsp.IsSuccess = true;
                Channel.SendResponse(rsp, Message.MessageID);
                //raise event
                OnVBMReceived?.Invoke(this, msg);
            }
            else
            {
                DebugEx.Assert("Unkown message received");
            }
        }
Exemplo n.º 5
0
        //-------------------------------------------------------------------------------------------------------------------------
        public void SetupChannel(YPChannel.Channel _channel)
        {
            if (_channel == null)
            {
                DebugEx.Assert("null channel");
                return;
            }

            //check for previous channels
            if (_channel.ChannelRole == YPChannel.ChannelRole.Client && ClientChannel != null)
            {
                try { ClientChannel.Close("channel setup (1)"); } catch { }
                try { ClientChannel.Dispose(); } catch { }
                unhookChannel(ClientChannel);
                ClientChannel = null;
            }
            else if (_channel.ChannelRole == YPChannel.ChannelRole.Server && ServerChannel != null)
            {
                try { ServerChannel.Close("channel setup (2)"); } catch { }
                try { ServerChannel.Dispose(); } catch { }
                unhookChannel(ServerChannel);
                ServerChannel = null;
            }

            //keep new channel
            if (_channel is YPChannel.Transport.Sockets.ServerChannel)
            {
                ServerChannel = _channel as YPChannel.Transport.Sockets.ServerChannel;
            }
            else if (_channel is YPChannel.Transport.Sockets.Client)
            {
                ClientChannel = _channel as YPChannel.Transport.Sockets.Client;
            }
            else
            {
                DebugEx.Assert("Should not be here");
            }

            //hook channel events
            hookChannel(_channel);
        }
Exemplo n.º 6
0
 //-------------------------------------------------------------------------------------------------------------------------
 private void YPChannel_OnClosedEvent(YPChannel.Channel Channel, string Message)
 {
     _disconnect();
     //raise events
     OnChannelClose?.Invoke(this);
 }
Exemplo n.º 7
0
 //-------------------------------------------------------------------------------------------------------------------------
 private void YPChannel_OnOpenEvent(YPChannel.Channel Channel)
 {
     DebugEx.TraceLog($"RemoteNode : Openned channel with node. (ip:{IPAddress} port:{RemotePort} nodekey:{RemoteNodeKey})");
     //raise events
     OnChannelOpen?.Invoke(this);
 }