示例#1
0
        public Node ConnectAsync(OverlayEndpoint theirInfo, Node.DisconnectProcessor dp)
        {
            dp = ProcessDisconnectWrap(dp);
            Node targetNode = FindNode(theirInfo);

            Handshake info = new Handshake(Address, theirInfo, extraHandshakeInfo);

            bool newConnection = (targetNode == null);

            if (newConnection)
            {
                targetNode = new Node(info, processQueue, dp);
                AddNode(targetNode);
            }

            MyAssert.Assert(targetNode.readerStatus != Node.ReadStatus.DISCONNECTED);
            MyAssert.Assert(targetNode.writerStatus != Node.WriteStatus.DISCONNECTED);
            MyAssert.Assert(!targetNode.IsClosed);

            if (targetNode.writerStatus == Node.WriteStatus.WRITING)
            {
                throw new NodeException("Already connected/connecting to " + targetNode.Address);
            }
            else
            {
                targetNode.ConnectAsync();
            }

            if (newConnection)
            {
                onNewConnectionHook.Invoke(targetNode);
            }

            return(targetNode);
        }
示例#2
0
        public Handshake(OverlayEndpoint local_, OverlayEndpoint remote_, MemoryStream ExtraInfo_)
        {
            remote = remote_;
            local  = local_;

            ExtraInfo = ExtraInfo_;
        }
示例#3
0
        public void SendMessage(OverlayEndpoint remote, NetworkMessage nm)
        {
            Node n = FindNode(remote);

            MyAssert.Assert(n != null);

            n.SendMessage(nm);
        }
示例#4
0
        public void SoftDisconnect(OverlayEndpoint theirInfo)
        {
            Node targetNode = FindNode(theirInfo);

            MyAssert.Assert(targetNode != null);

            targetNode.SoftDisconnect();
        }
示例#5
0
 public Node FindNode(OverlayEndpoint theirInfo)
 {
     try
     {
         return(nodes.GetValue(theirInfo));
     }
     catch (InvalidOperationException)
     {
         return(null);
     }
 }
示例#6
0
 public Node TryConnectAsync(OverlayEndpoint theirInfo, Node.DisconnectProcessor dp)
 {
     try
     {
         return(ConnectAsync(theirInfo, dp));
     }
     catch (NodeException)
     {
         return(null);
     }
 }
示例#7
0
        public bool TryCloseNode(OverlayEndpoint remote)
        {
            Node n = FindNode(remote);

            if (n != null)
            {
                MyAssert.Assert(!n.IsClosed);
                n.Disconnect();
                return(true);
            }

            return(false);
        }
示例#8
0
        private void HandshakeThread(Socket sckRead)
        {
            try
            {
                using (var h = DisposeHandle.Get(sckRead))
                    using (NetworkStream connectionStream = new NetworkStream(sckRead, false))
                    {
                        int nTp = connectionStream.ReadByte();

                        if (nTp != (int)NetworkMessageType.HANDSHAKE)
                        {
                            //Log.LogWriteLine("Invalid incoming connection message (expecting handshake): type {0} {1}", nTp, (MessageType)nTp);

                            sckRead.Close();
                            throw new Exception("Invalid incoming connection message (expecting handshake): type " + nTp + " " + (NetworkMessageType)nTp);
                        }

                        Handshake info = Serializer.Deserialize <Handshake>(Serializer.DeserializeChunk(connectionStream));

                        // swap
                        OverlayEndpoint remote = info.local;
                        OverlayEndpoint local  = info.remote;

                        info.remote = remote;
                        info.local  = local;

                        // read extra information
                        if (info.hasExtraInfo)
                        {
                            info.ExtraInfo = Serializer.DeserializeChunk(connectionStream);
                        }

                        processConnection.Invoke(info, sckRead);
                        h.Disengage();
                    }
            }
            catch (Exception e)
            {
                Log.Console("Error while processing handshake: {0}", e.Message);
                //onHandshakeError(e);
                throw;
            }
        }