/** * Centralized connections with all peers * Get an existing connection * OR create one and register it with the peer * */ public static TCPConnection getPeerConnection(NodePeer p) { TCPConnection con; Sock.TryGetValue(p,out con); if(con != null) return con; con = new TCPConnection(p.Address,p.Port); Sock.Add(p,con); return con; }
/** * Read the response header to see if there are packets * sometimes , browsable packet is present but servent has disactivated for G2 network * (shaeraza) * return true if servent will stream g2packets * return false if not (html code listing neighbours / or nothing) * */ public bool ReadResponseHeader(TCPConnection con) { ByteBuffer resp = con.Read(); if (resp == null) return false; string text = BinaryUtils.getStringFromBytes(resp.Bytes,(int)resp.DataOffset); string sequence = HttpHeader.nl + HttpHeader.nl; string[] splitter = {sequence}; string[] header = text.Split(splitter,StringSplitOptions.RemoveEmptyEntries); if (header.Length < 2) return false; // check if the http header is OK i.e. will the servent stream g2 packets ? if (!ParseBrowsingResponse(header[0])) { return false; } // remove the http header part from the buffer int dataOffset = text.IndexOf(sequence) + sequence.Length; resp.Dequeue(dataOffset); // put available data into the reader reader = new G2PacketReader(this.Peer); reader.Read(resp); return true; }
private bool SendHttpRequest(TCPConnection con) { HttpHeader header = new HttpHeader(); string request = @"GET / HTTP/1.1" + HttpHeader.nl; request += HttpHeader.ACCEPT_KEY + ": " + @"text/html, " + HttpHeader.G2ACCEPT_DEFAULT_VALUE + HttpHeader.nl; request += HttpHeader.USER_AGENT_KEY + ": " + Settings.USER_AGENT + HttpHeader.nl; request += HttpHeader.ACCEPT_ENCODING_KEY + @": identity" + HttpHeader.nl; request += @"Connection: close" + HttpHeader.nl ; request += @"Host: " + Peer.Address.ToString() + ":" + Peer.Port; request += HttpHeader.nl + HttpHeader.nl; bool succ = con.Send(new ByteBuffer(BinaryUtils.getSimpleBytesFromString(request))); return succ; }
// tells if we have found a hub ready to connect or not public GHandshake(NodePeer remoteHost) { this.remote_host = remoteHost; tcp = TCPConnection.getPeerConnection(remoteHost); Encoding = HttpHeader.ENCODING_IDENTITY; }