metacall/mc_stream.cpp

123 lines
2.9 KiB
C++
Raw Normal View History

2011-08-28 15:45:42 +00:00
//
// Copyright (c) 2011 Alex Yatskov
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
#include "metacall.hpp"
namespace metacall {
2011-09-05 03:05:33 +00:00
//
// Constants
//
enum {
SOCKET_BUFFER_SIZE = 1024
};
2011-08-28 15:45:42 +00:00
//
// Stream
//
Stream::Stream(Socket* socket) :
2012-02-19 15:02:35 +00:00
m_socket(socket)
2011-08-28 15:45:42 +00:00
{
}
Stream::State Stream::advance() {
2012-02-19 15:02:35 +00:00
if (!m_socket->connected()) {
2011-08-28 15:45:42 +00:00
return STATE_ERROR_CONNECTION;
}
2012-02-19 15:02:35 +00:00
if (m_buffSend.bytes() > 0 && m_socket->wait(Socket::MASK_WRITE, 0)) {
const int bytesSent = m_socket->send(
m_buffSend.data(),
m_buffSend.bytes()
2011-08-28 15:45:42 +00:00
);
if (bytesSent <= 0) {
return STATE_ERROR_CONNECTION;
}
2012-02-19 15:02:35 +00:00
m_buffSend.removeFromFront(NULL, bytesSent);
2011-08-28 15:45:42 +00:00
}
2012-02-19 15:02:35 +00:00
if (m_socket->wait(Socket::MASK_READ, 0)) {
2011-09-05 03:05:33 +00:00
byte buffRecv[SOCKET_BUFFER_SIZE];
2012-02-19 15:02:35 +00:00
const int bytesRecv = m_socket->receive(
2011-09-04 03:34:30 +00:00
buffRecv,
sizeof(buffRecv)
2011-08-28 15:45:42 +00:00
);
2011-09-04 03:34:30 +00:00
if (bytesRecv <= 0) {
2011-08-28 15:45:42 +00:00
return STATE_ERROR_CONNECTION;
}
2012-02-19 15:02:35 +00:00
m_buffRecv.addToBack(buffRecv, bytesRecv);
2011-08-28 15:45:42 +00:00
}
return STATE_READY;
}
void Stream::reset() {
2012-02-19 15:02:35 +00:00
m_buffSend.clear();
m_buffRecv.clear();
2011-08-28 15:45:42 +00:00
}
Stream::State Stream::peek(PacketHeader* header, int* headerSize) {
2012-02-19 15:02:35 +00:00
if (!m_socket->connected()) {
2011-09-05 03:05:33 +00:00
return STATE_ERROR_CONNECTION;
}
2012-02-19 15:02:35 +00:00
Deserializer deserializer(&m_buffRecv);
2011-08-28 15:45:42 +00:00
if (!deserializer.read(header)) {
return STATE_PENDING_PACKET_HEADER;
}
if (header->magic != PACKET_CONST_MAGIC) {
return STATE_ERROR_PROTOCOL;
}
*headerSize = deserializer.offset();
const int packetSize = static_cast<int>(header->size) + *headerSize;
2012-02-19 15:02:35 +00:00
if (m_buffRecv.bytes() < packetSize) {
2011-08-28 15:45:42 +00:00
return STATE_PENDING_PACKET_BODY;
}
return STATE_READY;
}
2012-02-20 02:47:09 +00:00
const Socket& Stream::socket() const {
return *m_socket;
2011-08-28 15:45:42 +00:00
}
2012-02-20 02:47:09 +00:00
Socket& Stream::socket() {
return *m_socket;
2011-08-28 15:45:42 +00:00
}
}