better socket sending/receiving
authorCharles McGarvey <chazmcgarvey@brokenzipper.com>
Thu, 13 May 2010 21:22:57 +0000 (15:22 -0600)
committerCharles McGarvey <chazmcgarvey@brokenzipper.com>
Thu, 13 May 2010 21:22:57 +0000 (15:22 -0600)
src/Main.cc
src/Moof/Socket.hh

index 4d88a709e69bb39a71e739a574823e254ff2f0ac..051b22c5a4998e71a5ecb61468227f4fafbe3cec 100644 (file)
@@ -296,46 +296,19 @@ int main(int argc, char* argv[])
        }
 
 
-       Mf::Packet packet(1000000);
-
-       packet << 5.1234f;
-       Mf::logInfo << "packet size: " << packet.size() << std::endl;
-
-       float meh3;
-       packet >> meh3;
-       Mf::logInfo << "float: " << meh3 << std::endl;
-       Mf::logInfo << "packet size: " << packet.size() << std::endl;
-
-
-       packet << uint16_t(45);
-       Mf::logInfo << "packet size: " << packet.size() << std::endl;
-       packet << uint64_t(1234567812345679ULL);
-       Mf::logInfo << "packet size: " << packet.size() << std::endl;
-
-       packet << true << false << false << true << false << true << true;
-       Mf::logInfo << "packet size: " << packet.size() << std::endl;
-
-       std::vector<short> hi;
-       hi.push_back(34);
-       hi.push_back(-12345);
-       hi.push_back(7734);
-
-       for (int a = 0; a < 15900; a++)
+       std::vector<uint8_t> hi;
+       for (int a = 0; a < 4000; a++)
        {
                hi.push_back(a);
        }
 
+       Mf::logInfo << "array size: " << hi.size() << std::endl;
+       Mf::Packet packet;
        packet << hi;
-       Mf::logInfo << "packet size: " << packet.size() << std::endl;
-
-       packet << "hello world";
-       Mf::logInfo << "packet size: " << packet.size() << std::endl;
-
-       packet << false << false << false << true << false;
-       Mf::logInfo << "packet size: " << packet.size() << std::endl;
-
 
-       Mf::SocketAddress addr = Mf::SocketAddress::broadcast("4950");
+       Mf::SocketAddress addr("4950", "155.98.111.159", SOCK_DGRAM);
+       //Mf::SocketAddress addr = Mf::SocketAddress::broadcast("4950");
+       //Mf::SocketAddress addr("4950", "155.98.109.255", SOCK_DGRAM);
        //Mf::logInfo << "local addr: " << addr.name() << std::endl;
 
        //for (it = task.addresses().begin(); it != task.addresses().end(); ++it)
@@ -344,6 +317,7 @@ int main(int argc, char* argv[])
 
                //Mf::SocketAddress addr = *it;
                Mf::Socket sock(addr);
+               sock.connect();
 
                sock.get(SO_BROADCAST, bcast);
                Mf::logInfo << "bcast: " << bcast << std::endl;
@@ -353,7 +327,9 @@ int main(int argc, char* argv[])
                sock.get(SO_BROADCAST, bcast);
                Mf::logInfo << "bcast: " << bcast << std::endl;
 
-               sock.write(packet);
+               Mf::logInfo << "sending packet of size: " << packet.size() << std::endl;
+               //sock.write(packet);
+               sock.write(&bcast, sizeof(bcast));
        //}
 
 
index 9690a2e892df42cdc59257ec270083d9f8cb98d3..c53ca06289974869f8d1b31fe438605aea827835 100644 (file)
@@ -254,6 +254,7 @@ public:
 
        Socket(const SocketAddress& address) :
                mFd(-1),
+               mIsConnected(false),
                mAddress(address)
        {
                mFd = socket(address.family(), address.type(), 0);
@@ -271,7 +272,7 @@ public:
 
        bool isConnected() const
        {
-               return mFd != -1;
+               return mIsConnected;
        }
 
        const SocketAddress& address() const
@@ -282,7 +283,9 @@ public:
 
        int connect()
        {
-               return ::connect(mFd, mAddress.address(), mAddress.size());
+               int result = ::connect(mFd, mAddress.address(), mAddress.size());
+               mIsConnected = result != -1;
+               return result;
        }
 
        int bind()
@@ -318,7 +321,7 @@ public:
        int set(int option, const std::string& value)
        {
                return setsockopt(mFd, SOL_SOCKET, option,
-                               value.c_str(), value.length());
+                               value.data(), value.length());
        }
 
        int get(int option, int& value)
@@ -345,26 +348,35 @@ public:
                return result;
        }
 
-       void write(const Packet& packet)
+
+       ssize_t write(const void* bytes, size_t size)
        {
-               write(mAddress, packet);
+               return send(mFd, bytes, size, 0);
+       }
+       ssize_t write(const void* bytes, size_t size,
+               const SocketAddress& address)
+       {
+               return sendto(mFd, bytes, size, 0,
+                       address.address(), address.size());
        }
 
-       void write(const SocketAddress& address, const Packet& packet)
+       ssize_t write(const Packet& packet)
        {
-               sendto(mFd, packet.bytes(), packet.size(), 0,
-                               address.address(), address.size());
+               return write(packet.bytes(), packet.size());
        }
 
-       Packet read()
+       ssize_t write(const Packet& packet, const SocketAddress& address)
        {
-               char buffer[1024];
-               int size = recv(mFd, buffer, sizeof(buffer), 0);
+               return write(packet.bytes(), packet.size(), address);
+       }
 
-               return Packet(buffer, size);
+
+       ssize_t read(void* bytes, size_t size)
+       {
+               return recv(mFd, bytes, size, 0);
        }
 
-       Packet read(SocketAddress& address)
+       ssize_t read(void* bytes, size_t size, SocketAddress& address)
        {
                union
                {
@@ -373,12 +385,28 @@ public:
                } addr;
                socklen_t length = sizeof(addr);
 
-               char buffer[1024];
-               int size = recvfrom(mFd, buffer, sizeof(buffer), 0,
-                               &addr.sa, &length);
+               ssize_t result = recvfrom(mFd, bytes, size, 0, &addr.sa, &length);
+               if (result != -1)
+               {
+                       address = SocketAddress(&addr.sa, length, mAddress.type());
+               }
+               return result;
+       }
+
+       ssize_t read(Packet& packet)
+       {
+               char buffer[65536];
+               ssize_t result = read(buffer, sizeof(buffer));
+               if (result != -1) packet = Packet(buffer, result);
+               return result;
+       }
 
-               address = SocketAddress(&addr.sa, length, mAddress.type());
-               return Packet(buffer, size);
+       ssize_t read(Packet& packet, SocketAddress& address)
+       {
+               char buffer[65536];
+               ssize_t result = read(buffer, sizeof(buffer), address);
+               if (result != -1) packet = Packet(buffer, result);
+               return result;
        }
 
 
@@ -400,6 +428,7 @@ private:
 
 
        int                             mFd;
+       bool                    mIsConnected;
        SocketAddress   mAddress;
 };
 
This page took 0.023925 seconds and 4 git commands to generate.