internal void NewIncomingConnection(Handshake info, Socket sck) { try { MyAssert.Assert(info.local.hostname == hostName); MemoryStream remoteExtraInfo = info.ExtraInfo; info.ExtraInfo = extraHandshakeInfo; Node targetNode = FindNode(info.remote); bool newConnection = (targetNode == null); if (newConnection) { targetNode = new Node(info, processQueue, null); AddNode(targetNode); } NodeProcessors proc = processorAssigner(targetNode, remoteExtraInfo); proc.Message = ProcessMessageWrap(proc.Message); proc.Disconnect = ProcessDisconnectWrap(proc.Disconnect); targetNode.notifyDisonnect = proc.Disconnect; MyAssert.Assert(targetNode.readerStatus != Node.ReadStatus.DISCONNECTED); MyAssert.Assert(targetNode.writerStatus != Node.WriteStatus.DISCONNECTED); MyAssert.Assert(!targetNode.IsClosed); if (targetNode.readerStatus != Node.ReadStatus.READY) { Log.Console("New connection {0} rejected: node already connected", info.remote); sck.Close(); return; } targetNode.AcceptReaderConnection(sck, proc.Message); if (newConnection) { onNewConnectionHook.Invoke(targetNode); } //if (targetNode.writerStatus == Node.WriteStatus.READY) // targetNode.ConnectAsync(); } catch (NodeException) // FIXME { sck.Close(); throw; } }
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; } }