Source

Magnum / source / libs / magnum_net / source / magnum / net / SslSocketStream.cpp

Full commit
/* 
  Magnum 
  Copyright (C) 2002-2012 Kaya Kupferschmidt. All Rights Reserved.

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, under the terms in LICENSE.TXT.

  Kaya Kupferschmidt  (k.kupferschmidt@dimajix.de)
*/

#include "magnum/io/StreamUtils.h"
#include "magnum/net/Exception.h"
#include "magnum/net/SslSocket.h"
#include "magnum/net/SslSocketStream.h"


namespace magnum { namespace net {
using magnum::object::NullPointerException;
using magnum::object::Nil;
using magnum::types::byte;
using magnum::io::StreamUtils;


/*----------------------------------------------------------------------------
----------------------------------------------------------------------------*/
SslSocketInputStream::SslSocketInputStream(SslSocket* s) 
{
	if (s == Nil)
		throw NullPointerException();

	m_Socket = s;
}


/*----------------------------------------------------------------------------
----------------------------------------------------------------------------*/
SslSocketInputStream::~SslSocketInputStream() 
{
	m_Socket = Nil;
}


/*--------------------------------------------------------------------------*/
/**
 */
bool SslSocketInputStream::isValid() const
{
    return m_Socket != Nil;
}


/*----------------------------------------------------------------------------
----------------------------------------------------------------------------*/
size_t SslSocketInputStream::read(void *buffer, size_t size) 
{
    while (true) {
        int ret = SSL_read(m_Socket->m_SslConnection, buffer, (int)size);
        if (ret > 0) {
            return ret;
        }
        else {
            switch (SSL_get_error(m_Socket->m_SslConnection, ret)) {
                case SSL_ERROR_NONE:
                    return 0;
                case SSL_ERROR_WANT_READ:
                case SSL_ERROR_WANT_WRITE:
                    continue;
                //case SSL_ERROR_ZERO_RETURN:
                default:
                    return (size_t)EndOfStream;
            }
        }
    }
}


/*----------------------------------------------------------------------------
----------------------------------------------------------------------------*/
int64 SslSocketInputStream::skip(int64 size) 
{
    return StreamUtils::readSkip(this, size);
}


/*----------------------------------------------------------------------------
----------------------------------------------------------------------------*/
int64 SslSocketInputStream::available() 
{
	return SSL_pending(m_Socket->m_SslConnection);
}


/*----------------------------------------------------------------------------
----------------------------------------------------------------------------*/
void SslSocketInputStream::close()
{
	if (m_Socket != Nil) {
		m_Socket->shutdownInput();
		m_Socket = Nil;
	}
}


/*----------------------------------------------------------------------------
----------------------------------------------------------------------------*/
SslSocketOutputStream::SslSocketOutputStream(SslSocket* s) 
{
	if (s == Nil)
		throw NullPointerException();

	m_Socket = s;
}


/*----------------------------------------------------------------------------
----------------------------------------------------------------------------*/
SslSocketOutputStream::~SslSocketOutputStream() 
{
	m_Socket = Nil;
}


/*--------------------------------------------------------------------------*/
/**
 */
bool SslSocketOutputStream::isValid() const
{
    return m_Socket != Nil;
}


/*----------------------------------------------------------------------------
----------------------------------------------------------------------------*/
size_t SslSocketOutputStream::write(const void *buffer, size_t size) 
{
    while (true) {
        int ret = SSL_write(m_Socket->m_SslConnection, buffer, (int)size);
        if (ret > 0) {
            return ret;
        }
        else {
            switch (SSL_get_error(m_Socket->m_SslConnection, ret)) {
                case SSL_ERROR_NONE:
                    return 0;
                case SSL_ERROR_WANT_READ:
                case SSL_ERROR_WANT_WRITE:
                    continue;
                //case SSL_ERROR_ZERO_RETURN:
                default:
                    throw SocketException();
            }
        }
    }
}


/*----------------------------------------------------------------------------
----------------------------------------------------------------------------*/
bool SslSocketOutputStream::flush() 
{
	return true;
}


/*----------------------------------------------------------------------------
----------------------------------------------------------------------------*/
void SslSocketOutputStream::close()
{
	if (m_Socket != Nil) {
		m_Socket->shutdownOutput();
		m_Socket = Nil;
	}
}

} }