I am attempting to write an abstract layer to make Ethernet and Serial seemless in my application. I have an interface that is the what I call ILayer it has the method stubs for connect, disconnect, read and write. Next I have an AbstractLayer class that will handle the read and writes and abstract the rest of the methods to the inheriting classes. What I want is to use IOStreams or something similar so to that when I have the Ethernet or Serial classes I can new up the IO streams particular to the Ethernet or Serial making the reading and writing of data seamless. Therefore the application doesn't care what the connection type is just that it has a connection. I did the same thing in Java but I am new to C# and I am trying to do the same thing. Any ideas?
How to write abstraction for communications Ethernet and Serial
Collapse
X
-
Let expand on this I have an interface ILayer that is used in my abstract class ALayer and this abstract is used in My Ethernet or Com classes. Therefore this will abstract the communication layer, then I won't care what the actual communication as long as I can read and write from them. SO far this is what I have:
public interface ILayer {
bool isConnected();
bool reconnectSettin g();
void connect();
void disconnect();
int read(byte[] data , int length);
void write(byte[] data);
}
public abstract class ALayer : ILayer{
protected ???? inStream; //not sure what to use here to make the readers and writers for communication common.
protected ???? outStream;
private bool reconnect;
public abstract bool isConnected();
public abstract void connect();
public abstract void disconnect();
public bool reconnectSettin g() {
return false;
}
private void waitingToConnec t() {
while (reconnect && !isConnected()) {
connect();
}
}
public int read(byte[] data, int length) {
waitingToConnec t();
// add reading logic
return 0;
}
public void write(byte[] data) {
waitingToConnec t();
// add write logic.
}
}
public class Ethernet {
// So far don't know how to abstract the Out and In streams... not sure if C# will let me do something like this.
}
Comment