Pārlūkot izejas kodu

PacketQueue implemented

mikymod 13 gadi atpakaļ
vecāks
revīzija
a2f465b27e
1 mainītis faili ar 41 papildinājumiem un 9 dzēšanām
  1. 41 9
      src/network/PacketQueue.cpp

+ 41 - 9
src/network/PacketQueue.cpp

@@ -24,40 +24,72 @@ PacketQueue::~PacketQueue()
 //-------------------------------------------------------------
 //-------------------------------------------------------------
 bool PacketQueue::add(const PacketData& pd)
 bool PacketQueue::add(const PacketData& pd)
 {	
 {	
+	if (get_space_left() < sizeof(PacketData))
+	{
+		return false;
+	}
 	// update last sequence number
 	// update last sequence number
-	m_last = pd.sequence; 
-	// update end index
-	m_end_index += sizeof(PacketData);
+	write_uint16(pd.sequence);
+	write_int32(pd.time);
+	write_int32(pd.time);
+	last++; 
+	return true;
 }
 }
 
 
 //-------------------------------------------------------------
 //-------------------------------------------------------------
 bool PacketQueue::get(PacketData& pd)
 bool PacketQueue::get(PacketData& pd)
-{
-
+{	
+	// empty queue
+	if (first == last)
+	{
+		return false;
+	}
+
+	pd.sequence = read_uint16();
+	pd.time = read_int32();
+	pd.size = read_int32();
+
+	assert(pd.sequence == first);
+	first++;
+	return true;
 }
 }
 
 
 //-------------------------------------------------------------
 //-------------------------------------------------------------
 size_t PacketQueue::get_total_size() const
 size_t PacketQueue::get_total_size() const
 {
 {
-
+	if (m_start_index <= m_end_index) 
+	{
+		return endIndex - startIndex;
+	} 
+	else 
+	{
+		return sizeof(buffer) - startIndex + endIndex;
+	}
 }
 }
 
 
 //-------------------------------------------------------------
 //-------------------------------------------------------------
 size_t PacketQueue::get_space_left() const
 size_t PacketQueue::get_space_left() const
 {
 {
-
+	if (m_start_index <= m_end_index) 
+	{
+		return sizeof(buffer) - (endIndex - startIndex) - 1;
+	} 
+	else 
+	{
+		return (startIndex - endIndex) - 1;
+	}
 }
 }
 
 
 //-------------------------------------------------------------
 //-------------------------------------------------------------
 int32_t PacketQueue::get_first() const
 int32_t PacketQueue::get_first() const
 {
 {
-
+	return m_first;
 }
 }
 
 
 //-------------------------------------------------------------
 //-------------------------------------------------------------
 int32_t PacketQueue::get_last() const
 int32_t PacketQueue::get_last() const
 {
 {
-
+	return m_last;
 }
 }
 
 
 //-------------------------------------------------------------
 //-------------------------------------------------------------