本文整理汇总了C++中StreamSocket类的典型用法代码示例。如果您正苦于以下问题:C++ StreamSocket类的具体用法?C++ StreamSocket怎么用?C++ StreamSocket使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了StreamSocket类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: VectorPush
static ssize_t VectorPush(gnutls_transport_ptr_t transportptr, const giovec_t* iov, int iovcnt)
{
StreamSocket* sock = reinterpret_cast<StreamSocket*>(transportptr);
#ifdef _WIN32
GnuTLSIOHook* session = static_cast<GnuTLSIOHook*>(sock->GetIOHook());
#endif
if (sock->GetEventMask() & FD_WRITE_WILL_BLOCK)
{
#ifdef _WIN32
gnutls_transport_set_errno(session->sess, EAGAIN);
#else
errno = EAGAIN;
#endif
return -1;
}
// Cast the giovec_t to iovec not to IOVector so the correct function is called on Windows
int ret = SocketEngine::WriteV(sock, reinterpret_cast<const iovec*>(iov), iovcnt);
#ifdef _WIN32
// See the function above for more info about the usage of gnutls_transport_set_errno() on Windows
if (ret < 0)
gnutls_transport_set_errno(session->sess, SocketEngine::IgnoreError() ? EAGAIN : errno);
#endif
int size = 0;
for (int i = 0; i < iovcnt; i++)
size += iov[i].iov_len;
if (ret < size)
SocketEngine::ChangeEventMask(sock, FD_WRITE_WILL_BLOCK);
return ret;
}
示例2: StreamSocket
void *connectMainLoop(void *arg)
{
StreamSocket *socket = new StreamSocket();
if(socket)
{
int connectSock;
string connectIP;
unsigned int port;
int ret = socket->startServer(CONNECT_PORT);
while(ret >= 0)
{
connectSock = socket->accept(connectIP, port);
if(connectSock != INVALID_SOCKET)
{
cout <<"connect ip ="<<connectIP<<":"<<port<<endl;
socket->setRemoteSocket(connectSock, connectIP, port);
createThread(recvDataLoop, (void*)socket);
}
else
{
perror("\n connectMainLoop accept socket error: ");
usleep(300*1000); // 300ms
}
}
delete(socket);
socket = NULL;
}
return NULL;
}
示例3: testEOF
void SocketStreamTest::testEOF()
{
StreamSocket ss;
SocketStream str(ss);
{
EchoServer echoServer;
ss.connect(SocketAddress("localhost", echoServer.port()));
str << "hello";
assert (str.good());
str.flush();
assert (str.good());
ss.shutdownSend();
char buffer[5];
str.read(buffer, sizeof(buffer));
assert (str.good());
assert (str.gcount() == 5);
assert (std::string(buffer, 5) == "hello");
}
int c = str.get();
assert (c == -1);
assert (str.eof());
ss.close();
}
示例4: span
void EchoServer::run()
{
_ready.set();
pi::Timespan span(250000);
while (!_stop)
{
if (_socket.poll(span, Socket::SELECT_READ))
{
StreamSocket ss = _socket.acceptConnection();
try
{
char buffer[256];
int n = ss.receiveBytes(buffer, sizeof(buffer));
while (n > 0 && !_stop)
{
ss.sendBytes(buffer, n);
n = ss.receiveBytes(buffer, sizeof(buffer));
}
}
catch (pi::Exception& exc)
{
std::cerr << "EchoServer: " << exc.displayText() << std::endl;
}
}
}
}
示例5: handleInsert
void KeyValueStore::handleInsert(StreamSocket& client, const string& buf, Config::ThreadControl& control)
{
static const char sucResp = Protocol::REQ_SUC;
static const char failResp = Protocol::REQ_FAIL;
const char* ptr = buf.c_str();
while(*ptr != '\0' && *ptr != '\r' && *ptr != '\n' && *ptr != ' ') ptr++;
if(*ptr != ' ') {
printKv("Invalid request");
client.sendBytes(&failResp, sizeof(failResp));
return; // Invalid
}
string key(buf.substr(0, (ptr - buf.c_str())));
string data(buf.substr((ptr - buf.c_str()) + 1));
if(isResponsible(key, control)) {
printKv("INSERT request received");
printKv("Received key: "<< key << endl << "Value: "<< data);
memDB.insert(key, data);
// Respond to client
if(data.compare(memDB.lookup(key)) != 0)
client.sendBytes(&failResp, sizeof(failResp));
else
client.sendBytes(&sucResp, sizeof(sucResp));
KeyValueStore::doReplicate(Protocol::REPL_INS, buf, control);
} else {
pair<char, string> resp(KeyValueStore::forwardRequest(Protocol::INSERT, buf, control));
client.sendBytes(&resp.first, sizeof(resp.first));
}
}
示例6: span
void EchoServer::run()
{
_ready.set();
Poco::Timespan span(250000);
char* pBuffer = new char[_bufferSize];
while (!_stop)
{
if (_socket.poll(span, Socket::SELECT_READ))
{
StreamSocket ss = _socket.acceptConnection();
try
{
int n = ss.receiveBytes(pBuffer, _bufferSize);
while (n > 0 && !_stop)
{
ss.sendBytes(pBuffer, n);
n = ss.receiveBytes(pBuffer, _bufferSize);
}
}
catch (Poco::Exception& exc)
{
std::cerr << "EchoServer: " << exc.displayText() << std::endl;
}
}
}
delete pBuffer;
}
示例7: span
void HTTPTestServer::run()
{
_ready.set();
Poco::Timespan span(250000);
while (!_stop)
{
if (_socket.poll(span, Socket::SELECT_READ))
{
StreamSocket ss = _socket.acceptConnection();
try
{
_lastRequest.clear();
char buffer[256];
int n = ss.receiveBytes(buffer, sizeof(buffer));
while (n > 0 && !_stop)
{
_lastRequest.append(buffer, n);
if (!requestComplete())
n = ss.receiveBytes(buffer, sizeof(buffer));
else
n = 0;
}
std::string response = handleRequest();
ss.sendBytes(response.data(), (int) response.size());
Poco::Thread::sleep(1000);
}
catch (Poco::Exception& exc)
{
std::cerr << "HTTPTestServer: " << exc.displayText() << std::endl;
}
}
}
}
示例8: sizeof
pair<char, string> KeyValueStore::rcvResponse(StreamSocket& sock, char*& buf)
{
unsigned contentLength = 0;
char resp;
pair<char, string> ret;
char* msg = NULL;
sock.receiveBytes(&resp, sizeof(resp));
if(resp == Protocol::LOOKUP_SUC) {
sock.receiveBytes(&contentLength, sizeof(contentLength));
unsigned rem = contentLength;
unsigned totalRead = 0;
buf = new char[contentLength+1];
msg = buf;
buf[contentLength] = '\0';
while((totalRead += sock.receiveBytes(msg, rem)) != contentLength) {
msg = buf + totalRead;
rem = contentLength - totalRead;
}
ret = pair<char, string>(resp, buf);
delete [] buf;
buf = NULL;
} else {
ret = pair<char, string>(resp, "");
}
return ret;
}
示例9: gnutls_push_wrapper
static ssize_t gnutls_push_wrapper(gnutls_transport_ptr_t session_wrap, const void* buffer, size_t size)
{
StreamSocket* sock = reinterpret_cast<StreamSocket*>(session_wrap);
#ifdef _WIN32
GnuTLSIOHook* session = static_cast<GnuTLSIOHook*>(sock->GetIOHook());
#endif
if (sock->GetEventMask() & FD_WRITE_WILL_BLOCK)
{
#ifdef _WIN32
gnutls_transport_set_errno(session->sess, EAGAIN);
#else
errno = EAGAIN;
#endif
return -1;
}
int rv = SocketEngine::Send(sock, reinterpret_cast<const char *>(buffer), size, 0);
#ifdef _WIN32
if (rv < 0)
{
/* Windows doesn't use errno, but gnutls does, so check SocketEngine::IgnoreError()
* and then set errno appropriately.
* The gnutls library may also have a different errno variable than us, see
* gnutls_transport_set_errno(3).
*/
gnutls_transport_set_errno(session->sess, SocketEngine::IgnoreError() ? EAGAIN : errno);
}
#endif
if (rv < (int)size)
SocketEngine::ChangeEventMask(sock, FD_WRITE_WILL_BLOCK);
return rv;
}
示例10: handleRequest
/** Handling requests */
bool KeyValueStore::handleRequest(StreamSocket& client, Config::ThreadControl& control, char*& buf)
{
char req;
unsigned contentLength = 0;
client.receiveBytes(&req, sizeof(req));
if(req == Protocol::EXIT) {
return true;
}
// Always expect that content-length will be the next sent.
client.receiveBytes(&contentLength, sizeof(contentLength));
if(contentLength < 0)
throw Exception("Invalid content-length detected. Dropping client");
buf = new char[contentLength+1];
buf[contentLength] = '\0';
client.receiveBytes(buf, contentLength);
KeyValueStore::handleQuery(client, control, req, buf);
delete [] buf;
buf = NULL;
return false;
}
示例11: TEST_F
TEST_F(StreamSocketTest, StreamSocketReceiveLineInString)
{
StreamSocket stream;
EXPECT_TRUE(stream.Create(AF_INET));
EXPECT_FALSE(stream.Connect(SocketAddressInet("0.0.0.0:0")));
EXPECT_TRUE(stream.Connect(m_address)) << m_address.ToString();
struct timeval tv;
tv.tv_sec = 5;
tv.tv_usec = 0;
EXPECT_TRUE(stream.WaitWriteable(&tv, true));
std::string sent = "12345\n";
EXPECT_TRUE(stream.SendAll(sent.data(), sent.size()));
tv.tv_sec = 5;
tv.tv_usec = 0;
EXPECT_TRUE(stream.WaitReadable(&tv, true));
std::string received;
EXPECT_TRUE(stream.ReceiveLine(&received));
EXPECT_EQ(sent, received);
// Send all with timeout
tv.tv_sec = 5;
tv.tv_usec = 0;
size_t sent_size;
EXPECT_TRUE(stream.SendAll(sent.data(), sent.size(), &sent_size, &tv));
EXPECT_EQ(sent.size(), sent_size);
// peek size with 2
EXPECT_TRUE(stream.ReceiveLine(&received, 2));
EXPECT_EQ(sent, received);
}
示例12: while
void TCPServer::run()
{
while (!_stopped)
{
Poco::Timespan timeout(250000);
if (_socket.poll(timeout, Socket::SELECT_READ))
{
try
{
StreamSocket ss = _socket.acceptConnection();
// enabe nodelay per default: OSX really needs that
ss.setNoDelay(true);
_pDispatcher->enqueue(ss);
}
catch (Poco::Exception& exc)
{
ErrorHandler::handle(exc);
}
catch (std::exception& exc)
{
ErrorHandler::handle(exc);
}
catch (...)
{
ErrorHandler::handle();
}
}
}
}
示例13: handle
void handle(StreamSocket& sock) override {
while(true) {
cout << "handle called.\n";
string rcvd = sock.receive();
cout << "received data\n";
sock.send_all(rcvd);
}
}
示例14: PartialSendStreamSocket
PartialSendStreamSocket(StreamSocket& stream_socket)
: StreamSocket(
stream_socket.get_domain(),
stream_socket.get_protocol(),
static_cast<socket_t>(-1)
),
stream_socket(stream_socket.inc_ref()) {
}
示例15: sas
void LocalSocketTest::testConnect()
{
SocketAddress sas("/tmp/poco.server.tcp.sock");
ServerSocket serv(sas);
StreamSocket ss;
Timespan timeout(250000);
SocketAddress sac("/tmp/poco.client.tcp.sock");
ss.connect(sas, timeout, &sac);
}