CStream - Polyart streaming library
Polyart streams are used to allow data to be saved or loaded through different mediums without requiring seperate functions. Each endpoint represents either a save/send or load/receive through a particular medium.
All endpoints are derived from CStream.
class CStream{
CStream();
virtual int Serialize(void *Data, int Size);
int Saving;
};
All polylib stream classes
- CFileIn: Save to a file
- CFileOut: Load from a file
- CPipe: Creates the connection for both ends of the pipe(Not an endpoint)
- CPipe.In: Send to a pipe
- CPipe.Out: Receive from a pipe
- CSocketStream: Creates the connection for both ends of the socket (Not an endpoint)
- CSocketStream.In: Send to a socket
- CSocketStream.Out: Receive from a socket
- CNullStream: Serializing this will do nothing. It is often used as the default output.
- CBufferStream: This is a read resetable pipe. You may send data to this and read the data multiple times. Call reset to start the read position from the beginning. (NOTE: possibly create a buffer reader that is created by this class) The buffer also has unlimited size, it is expanded as necessary.
- CBufferStream.In:
- CBufferStream.Out:
- CLengthStream: The actual data sent to this will be ignored however the length of the stream is recorded.
- CHashStream: A unique DWORD hash of the data sent though this is retained.
Quick file read example
CFileIn File;
//CFileOut File; //Replacing FileIn with FileOut is the only change needed to save to file instead of load
int E;
int A;
E|=File.Open("MyFile.txt");
E|=File.Serialize(&A,sizeof(A));
//or
CStreamEnd *Stream=&File;
SRZ(A); // SRZ(A) macro is equivlent to E|=File.Serialize(&A,sizeof(A))
Examples of all current stream classes
void main(){
int E=0;//If any SRZ fails E will be set to 1
CStream *Stream;
CFileIn/CFileOut Test
CFileOut FOut;
CFileIn FIn;
int A=1;
FOut.Open("Blah.txt");
Stream=&FOut;//Write to file
SRZ(A);
FOut.Clear();
A=5;
FIn.Open("Blah.txt");
Stream=&FIn;//Read from file
SRZ(A);
FIn.Clear();//Close file
CPipe Test
CPipe Pipe;
A=4;
Stream=&Pipe.Out;//Write data to pipe
SRZ(A);
A=5;
Stream=&Pipe.In; //Read data from pipe
SRZ(A);
Pipe.Clear();//Pipes cant be closed but this will erase its contents
CStreamNet Test
CSocket Listener;
Listener.Listen(6666);
CStreamNet Client;
CStreamNet Server;
Client.Connect("127.0.0.1",6666);
Sleep(100);
Listener.Sync();
Listener.Accept(&Server);
Sleep(100);
A=4;
Stream=&Server.Out; //Send from server to client
SRZ(A);
A=5;
Stream=&Client.Out; //Send from client to server
SRZ(A);
A=6;
Client.Sync();
Server.Sync();
Sleep(100);
Client.Sync();
Server.Sync();
Stream=&Client.In;
SRZ(A);
Stream=&Server.In;
SRZ(A);
Client.Sync();
Server.Sync();
Listener.Clear();//Stop listening
Server.Clear(); //Disconnect
Client.Clear(); //Disconnect
}
Copyright 2004 © Polyart. All rights reserved.