/** * 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); }
/** * Check incoming data from socket every time and push it * into buffer if data present * */ public void ReceiveThread() { byte[] buffer = new byte[BUFF_SIZE]; // ShutdownEvent is a ManualResetEvent signaled by // Client when its time to close the socket. var count = 0; while (!shouldStop_) { try { count++; // We could use the ReadTimeout property and let Read() // block. However, if no data is received prior to the // timeout period expiring, an IOException occurs. // While this can be handled, it leads to problems when // debugging if we are wanting to break when exceptions // are thrown (unless we explicitly ignore IOException, // which I always forget to do). if (!stream.DataAvailable) { // Give up the remaining time slice. Thread.Sleep(10); } else { int bRead = stream.Read(buffer, 0, buffer.Length); // see if something is there if (bRead > 0) { //G2Log.Write("HubSocket: Received " + bRead + " bytes !"); Reader.Read(buffer, bRead); buffer = new byte[BUFF_SIZE]; } else { G2Log.Write("HubSocket: Connection killed ? " + this.peer.ToString()); // connection closed } } } catch (IOException ex) { G2Log.Write("ReceiveThread exception : " + ex.ToString()); } } }
/** * 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; }