Sign in to follow this  
  • entries
    132
  • comments
    99
  • views
    88507

Adventures in stream programming

Sign in to follow this  
Driv3MeFar

52 views

So, for one of my classes this semester I'm to implement a full networking engine. Since all of my previous attempts at this used horrible C-isms (mem copy's, C-arrays, etc.), I've decided to try and do this one in actual factual C++.

Basically, I plan to provide my own network stream class on top of std::basic_iostream. I'll provide >> and << operators for reading to and writing from my socket, and overload the overflow method to send the stream over the network.

Sounds easy enough, and it should play very nicely with my serialization layer (when I get around to writing it...). However, I've never done any stream programming before, and I'm still not very comfortable with templates (reading/writing them still fucks with my head a bit). Should be a good learning experience, and I think it will be very nice to work with once it's up and running.

I just hope I don't go crazy or run out of time first...

Anyway, code!

Here's the basic NetStream declaration (which I'll probably add a bit to as needed):
NetStream.h

#ifndef NETSTREAM_H
#define NETSTREAM_H

#include
#include "Net.h"

class Packet;

templateChar = char, typename Traits = std::char_traits<Char> >
class NetStream
: public std::basic_iostream<Char, Traits>
{
public:
typedef typename std::basic_iostream<Char, Traits>::traits_type traits_type;
typedef typename std::basic_iostream<Char, Traits>::int_type int_type;

public:
NetStream() : std::basic_iostream<Char, Traits>(&m_buffer) {}

public:
//read/write from stream (socket)
NetStream& operator>>(Packet *packet) {return *this;}
NetStream& operator<<(Packet *packet) {return *this;}

protected:
int_type overflow(int_type c = traits_type::endl) {return int_type();}

private:
NetStream(const NetStream&) {}
NetStream& operator=(const NetStream&) {return *this;}

private:
std::basic_iostream<Char, Traits> m_buffer;

SOCKET m_socket;

};

#endif





Note that none of the functions are implemented yet. But they will be. They will be.

Edit: Something's not working [sad].
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now