libt2n: (gerd) add hello peek
[libt2n] / src / connection.cpp
index 9cffb89..d339f9f 100644 (file)
@@ -30,8 +30,6 @@ namespace libt2n
 
 connection::packet_size_indicator connection::bytes_available()
 {
-    // max packet size is unsigned int
-
     // no size information -> no packet
     if (buffer.size() < sizeof(packet_size_indicator))
         return 0;
@@ -65,6 +63,29 @@ bool connection::get_packet(std::string& data)
         return false;
 }
 
+/** @brief get (maybe incomplete) data of the next packet from the buffer. Does not remove the data
+            from the connection buffer.
+    @param[out] data the data package
+    @retval full size of the packet when it will be complete
+*/
+unsigned int connection::peek_packet(std::string& data)
+{
+    // no size information -> no packet
+    if (buffer.size() < sizeof(packet_size_indicator))
+        return 0;
+
+    packet_size_indicator psize=ntohl(*((packet_size_indicator*)(buffer.data())));
+
+    // not the full data available?
+    packet_size_indicator currsize=psize;
+    if (buffer.size() < currsize+sizeof(packet_size_indicator))
+        currsize=buffer.size()-sizeof(packet_size_indicator);
+
+    data.assign(buffer,sizeof(packet_size_indicator),currsize);
+
+    return psize;
+}
+
 /// send a blob to the peer
 void connection::write(const std::string& data)
 {