]> oss.titaniummirror.com Git - tinyos-2.x.git/commitdiff
- write a packet in one go to the tcp side
authorandreaskoepke <andreaskoepke>
Fri, 2 May 2008 09:21:22 +0000 (09:21 +0000)
committerandreaskoepke <andreaskoepke>
Fri, 2 May 2008 09:21:22 +0000 (09:21 +0000)
- some hand-written functions replaced by their library equivalents

support/sdk/cpp/sf/sfpacket.cpp
support/sdk/cpp/sf/sfpacket.h
support/sdk/cpp/sf/tcpcomm.cpp

index d3a6555a861fa4d9bf58ae669a58dc53adffb3fe..c43059c600e8739dc147dad37bb1c55d0aaa89f8 100644 (file)
 
 #include "sfpacket.h"
 
-SFPacket::SFPacket(int pType, int pSeqno)
-{
+SFPacket::SFPacket(int pType, int pSeqno) {
     length = 0;
     seqno = pSeqno;
     type = pType;
 }
 
 // copy constructor
-SFPacket::SFPacket(const SFPacket &pPacket)
-{
+SFPacket::SFPacket(const SFPacket &pPacket) {
     length = pPacket.getLength();
     type = pPacket.getType();
     seqno = pPacket.getSeqno();
@@ -55,18 +53,15 @@ SFPacket::~SFPacket()
 
 const char* SFPacket::getPayload() const
 {
-    if ( ((type == SF_PACKET_ACK) || (type == SF_PACKET_NO_ACK)))
-    {
-        return payloadBuffer;
+    if(((type == SF_PACKET_ACK) || (type == SF_PACKET_NO_ACK))) {
+        return buffer + 1;
     }
-    else
-    {
+    else {
         return NULL;
     }
 }
 
-int SFPacket::getLength() const
-{
+int SFPacket::getLength() const {
     return length;
 }
 
@@ -85,10 +80,7 @@ bool SFPacket::setPayload(const char* pBuffer, uint8_t pLength)
     if ((pLength > 0) && (pLength < cMaxPacketLength) && ((type == SF_PACKET_ACK) || (type == SF_PACKET_NO_ACK)))
     {
         length = pLength;
-        for (int i=0; i < pLength; i++)
-        {
-            payloadBuffer[i] = *(pBuffer+i);
-        }
+        memcpy(buffer + 1, pBuffer, pLength);
         return true;
     }
     DEBUG("SFPACKET::setPayload : wrong packet length = " << static_cast<int>(pLength) << " or type = " << type)
@@ -114,29 +106,23 @@ int const SFPacket::getMaxPayloadLength()
 bool SFPacket::operator==(SFPacket const& pPacket)
 {
     bool retval=false;
-    if (!((pPacket.getType() != type) || (pPacket.getLength() != length) || pPacket.getSeqno() != seqno))
-    {
-        if ((type == SF_PACKET_ACK) || (type == SF_PACKET_NO_ACK))
-        {
-            const char* cmpBuffer = pPacket.getPayload();
-            if (cmpBuffer) {
-               retval = true;
-                // compare buffers
-                for (int i=0; i < length; i++)
-                {
-                    if (payloadBuffer[i] != cmpBuffer[i])
-                    {
-                        i = length;
-                        retval = false;
-                    }
-                }
-           }
-        }
-        else
-        {
-            retval = true;
+    if((pPacket.getType() == type) && (pPacket.getLength() == length) && (pPacket.getSeqno() == seqno)) {
+        if((type == SF_PACKET_ACK) || (type == SF_PACKET_NO_ACK)) {
+            retval = (memcmp(pPacket.getPayload(), getPayload(), length) == 0);
         }
     }
     return retval;
 }
 
+    /* return the length that shall be transmitted via TCP */
+int SFPacket::getTcpLength() const {
+    return length + 1;
+}
+
+/* return the payload of the TCP packet */
+const char* SFPacket::getTcpPayload() {
+    char l = length;
+    buffer[0] = l;
+    return buffer;
+}
+
index d3f5fdefbb3fdb90b984e7c7c29f0da01fd06011..4a650edfd6f70930fb92de6a79d797e53dd707d7 100644 (file)
@@ -66,8 +66,9 @@ public:
 
 /** member vars **/
 protected:
-    /* payload buffer */
-    char payloadBuffer[cMaxPacketLength];
+    /* internal buffer */
+    char buffer[cMaxPacketLength + 1];
+    
     /* length of byte buffer */
     int length;
     /* type */
@@ -92,6 +93,12 @@ public:
     /* returns length of buffer */
     int getLength() const;
 
+    /* return the length that shall be transmitted via TCP */
+    int getTcpLength() const;
+
+    /* return the payload of the TCP packet */
+    const char* getTcpPayload();
+    
     /* returns the seqno of this packet */
     int getSeqno() const;
 
index 8e5efee52a1f6c2c08b60806ca3633bb33d56746..27231a6202995e98778a9a5c56e1bde5dd3cbeca 100644 (file)
@@ -210,17 +210,9 @@ int TCPComm::writeFD(int fd, const char *buffer, int count, int *err)
 /* writes packet */
 bool TCPComm::writePacket(int pFD, SFPacket &pPacket)
 {
-    char len = pPacket.getLength();
+    int len = pPacket.getTcpLength();
     int err;
-    if (writeFD(pFD, &len, 1, &err) != 1)
-    {
-        return false;
-    }
-    if (writeFD(pFD, pPacket.getPayload(), len, &err) != len)
-    {
-        return false;
-    }
-    return true;
+    return (writeFD(pFD, pPacket.getTcpPayload(), len, &err) == len);
 }
 
 /* checks for correct version of SF protocol */