|
|
Choosing A Webhost: |
CVS update of carob (include/JavaSocket.hpp src/JavaSocket.cpp): msg#00098db.carob.cvs
Date: Friday, February 24, 2006 @ 18:30:34 Author: gilles Path: /cvsroot/carob/carob Modified: include/JavaSocket.hpp (1.25 -> 1.26) src/JavaSocket.cpp (1.42 -> 1.43) Changed receive/sendFromSocket function to return void: they both throw exception in case of error, if they return, this is success Added java-byte array serialization functions ------------------------+ include/JavaSocket.hpp | 30 ++++++++++++---- src/JavaSocket.cpp | 87 ++++++++++++++++++++++++++--------------------- 2 files changed, 72 insertions(+), 45 deletions(-) Index: carob/include/JavaSocket.hpp diff -u carob/include/JavaSocket.hpp:1.25 carob/include/JavaSocket.hpp:1.26 --- carob/include/JavaSocket.hpp:1.25 Wed Feb 1 16:19:18 2006 +++ carob/include/JavaSocket.hpp Fri Feb 24 18:30:33 2006 @@ -25,6 +25,8 @@ #include "StringCodecs.hpp" #include "CarobException.hpp" +#include "Common.hpp" // for java_byte: cannot forward declare typedefs + #include <string> #include <netinet/in.h> //for in_addr_t @@ -149,6 +151,22 @@ void readJavaBool(bool& b) const throw (SocketIOException, UnexpectedException); /** + * Reads an array of bytes from the socket. The array must be allocated ! + * @param length number of bytes to read + * @param data allocated array of (at least) length bytes + * @throws SocketIOException + */ + void readJavaBytes(int32_t length, java_byte* data) const + throw (SocketIOException, UnexpectedException); + /** + * Writes an array of bytes to the socket. + * @param length number of bytes to write + * @param data array of (at least) length bytes + * @throws SocketIOException + */ + void writeJavaBytes(int32_t length, java_byte* data) const + throw (SocketIOException, UnexpectedException); + /** * Gets the socket file descriptor * @return the file descriptor */ @@ -170,13 +188,12 @@ * @param buf data to be send * @param len length of buf * @param flags send option, see recv man page - * @return true if the data could be send successfully * @throws SocketIOException */ - bool sendToSocket(const std::wstring& fctName, + void sendToSocket(const std::wstring& fctName, const std::wstring& objName, const void* buf, int len, - int flags) - const throw (SocketIOException, UnexpectedException); + int flags) const throw (SocketIOException, + UnexpectedException); /** * Wrapper around recv(...) function to handle errors and throw exceptions * @param fctName name of the calling function (for logging purposes) @@ -184,10 +201,9 @@ * @param buf buffer in which to put the received data in * @param len length of buf * @param flags send option, see send man page - * @return true if the data could be send successfully - * @throws SocketIOException + * @throws SocketIOException in case of error */ - bool receiveFromSocket(const std::wstring& fctName, + void receiveFromSocket(const std::wstring& fctName, const std::wstring& objName, void *buf, int len, int flags) const throw (SocketIOException, UnexpectedException); Index: carob/src/JavaSocket.cpp diff -u carob/src/JavaSocket.cpp:1.42 carob/src/JavaSocket.cpp:1.43 --- carob/src/JavaSocket.cpp:1.42 Fri Feb 24 12:54:12 2006 +++ carob/src/JavaSocket.cpp Fri Feb 24 18:30:33 2006 @@ -218,10 +218,8 @@ int32_t netlen = htonl(utf8str.length()); //First write number of bytes to follow as - if (sendToSocket(fctName, L"UTF string", &netlen, sizeof(netlen), SOCKET_SEND_FLAGS)) - { - sendToSocket(fctName, L"UTF string", utf8str.data(), utf8str.length(), SOCKET_SEND_FLAGS); - } + sendToSocket(fctName, L"UTF string", &netlen, sizeof(netlen), SOCKET_SEND_FLAGS); + sendToSocket(fctName, L"UTF string", utf8str.data(), utf8str.length(), SOCKET_SEND_FLAGS); return utf8str.length(); } @@ -236,24 +234,21 @@ uint16_t lenRec; size_t sizeRead = 0; //First get size - if (receiveFromSocket(fctName, L"UTF string size", &lenRecNet, sizeof(lenRecNet), 0)) - { - lenRec = ntohl(lenRecNet); // number of bytes to come + receiveFromSocket(fctName, L"UTF string size", &lenRecNet, sizeof(lenRecNet), 0); + lenRec = ntohl(lenRecNet); // number of bytes to come - uint8_t* utfStr = new uint8_t[lenRec]; - if (receiveFromSocket(fctName, L"UTF string", utfStr, lenRec, 0)) - { - const std::string received(reinterpret_cast<char*>(utfStr), lenRec); + uint8_t* utfStr = new uint8_t[lenRec]; + receiveFromSocket(fctName, L"UTF string", utfStr, lenRec, 0); + const std::string received(reinterpret_cast<char*>(utfStr), lenRec); - try { - s = fromUTF8(received); - } catch (CodecException) { - delete[] utfStr; throw; - } - sizeRead = (size_t)lenRec; - } - delete[] utfStr; + try { + s = fromUTF8(received); + } catch (CodecException) { + delete[] utfStr; throw; } + sizeRead = (size_t)lenRec; + + delete[] utfStr; return sizeRead; } @@ -274,10 +269,8 @@ // so we must convert it after reception int32_t rec = 0; - if (receiveFromSocket(fctName, L"Int32", &rec, sizeof(rec), 0)) - { - i = ntohl(rec); - } + receiveFromSocket(fctName, L"Int32", &rec, sizeof(rec), 0); + i = ntohl(rec); } void JavaSocket::writeJavaLong(int64_t i) const throw (SocketIOException, @@ -297,16 +290,14 @@ // so we must convert it after reception int64_t rec = 0; - if (receiveFromSocket(fctName, L"Int64", &rec, sizeof(rec), 0)) - { - i = ntohll(rec); - } + receiveFromSocket(fctName, L"Int64", &rec, sizeof(rec), 0); + i = ntohll(rec); } void JavaSocket::writeJavaBool(bool b) const throw (SocketIOException, UnexpectedException) { - wstring fctName(L"JavaSocket::WriteBoolean"); + wstring fctName(L"JavaSocket::writeJavaBool"); uint8_t byteToSend = b; // the implicit casts do it right //No byte order problem here: the only value read is '0' for false //all other values are true @@ -316,11 +307,25 @@ void JavaSocket::readJavaBool(bool& b) const throw (SocketIOException, UnexpectedException) { - wstring fctName(L"JavaSocket::ReadBoolean"); + wstring fctName(L"JavaSocket::readJavaBool"); uint8_t rec; - if (receiveFromSocket(fctName, L"Boolean", &rec, sizeof(rec), 0)) - b = rec; // the implicit cast does it right + receiveFromSocket(fctName, L"Boolean", &rec, sizeof(rec), 0); + b = rec; // the implicit cast does it right +} + +void JavaSocket::readJavaBytes(int32_t length, java_byte* data) const + throw (SocketIOException, UnexpectedException) +{ + wstring fctName(L"JavaSocket::readJavaBytes"); + receiveFromSocket(fctName, L"JavaBytes", data, length, 0); +} + +void JavaSocket::writeJavaBytes(int32_t length, java_byte* data) const + throw (SocketIOException, UnexpectedException) +{ + wstring fctName(L"JavaSocket::writeJavaBytes"); + sendToSocket(fctName, L"JavaBytes", data, length, SOCKET_SEND_FLAGS); } int JavaSocket::recvFully(void *buf, const int len, const int flags) @@ -345,21 +350,28 @@ return len; } -bool JavaSocket::sendToSocket(const wstring& fctName, const wstring& objName, +void JavaSocket::sendToSocket(const wstring& fctName, const wstring& objName, const void* buf, int len, int flags) const throw (SocketIOException, UnexpectedException) { + // in non blocking mode, send will block until len bytes are send. So no need + // to loop here int ret = send(socketFd, buf, len, flags); if ( ret == -1 ) { - wstring msg = L'(' + fctName + L"): could not write " + objName + L" to socket"; - throw SocketIOException(msg); - return false; + throw SocketIOException(L'(' + fctName + L"): could not write " + + objName + L" to socket"); + } + if (ret != len) + { + // should never happen + throw SocketIOException(L'(' + fctName + L"): only " + toWString(ret) + + L"/" + toWString(len) + L" bytes send while sending " + objName + + L" to socket"); } - return true; } -bool JavaSocket::receiveFromSocket(const wstring& fctName, +void JavaSocket::receiveFromSocket(const wstring& fctName, const wstring& objName, void *buf, int len, int flags) const throw (SocketIOException, UnexpectedException) { @@ -372,7 +384,6 @@ { throw SocketIOException(fctName + L"Peer reset connection while reading integer."); } - return true; } const uint64_t JavaSocket::ntohll(const uint64_t &n) const
|
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | CVS update of carob (include/DriverSocket.hpp src/DriverSocket.cpp), gilles-Tt5JLJuBijYiZlD9aYmxOGD2FQJk+8+b |
|---|---|
| Next by Date: | CVS update of carob (3 files), gilles-Tt5JLJuBijYiZlD9aYmxOGD2FQJk+8+b |
| Previous by Thread: | CVS update of carob (include/JavaSocket.hpp src/JavaSocket.cpp), gilles-Tt5JLJuBijYiZlD9aYmxOGD2FQJk+8+b |
| Next by Thread: | CVS update of carob/src (SQLDataSerialization.cpp), marc-Tt5JLJuBijYiZlD9aYmxOGD2FQJk+8+b |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
Free MagazinesCisco NewsReceive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business. subscribe Systems Management News, the newspaper for IT systems administration and data center managers! Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field. subscribe The Enterprise Newsweekly eWeek is the essential technology information source for builders of e-business. subscribe Oracle Magazine Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company. subscribe Total Telecom Total Telecom is "The Economist of the communications industry". subscribe |
Home
| advertise | OSDir is
an inevitable website.
|