📜 ⬆️ ⬇️

SyncStream - C # library for transferring data over unstable channels

Recently I found an old self-made library that implemented a simple data transfer protocol in TCP packets.
After processing with a file and chisels, nothing came out very much (:

Meet - SyncStream library for transferring data through buggy channels:



UPD: apparently, it is not obvious that the channel is not only twisted pair and TCP / IP. The library works in general with any transport - be it a radio channel or an infrared port - it is enough to write and pick up your interface class.
UPD 2: For especially persistent: the library is NOT bound to TCP / IP
')
About the principle of work

To implement multiprotocol when creating a socket, you pass a class derived from the abstract BaseImplementation class, which should contain socket-specific methods Connect (), Close (), Bind (), Listen (), Send (), Receive (). After that, all actions related directly to sending data will be made through this class.
The library has already included the appropriate class to work through TCP.

Data is sent in the form of packets .
Package contains:



“Magic sequences” are required in order to find the beginning of the next packet (restore synchronization) when “extra” data enters the channel or part of the data is lost.

About pleasant and fun use

Sample customer code:

Copy Source | Copy HTML class MainClass { public static void Main( string [] args) { Socket s = new Socket ( new TcpImplementation ()); s.Receive += new Socket . ReceiveEvent (OnReceive); s.Connect( "127.0.0.1:1111" ); while ( true ) { string ss = Console .ReadLine(); Packet p = new Packet ( "message" , new Argument ( "text" , ss)); p.Important = true ; s.Send(p); } } public static void OnReceive( Socket s, Packet p) { Console .WriteLine( "<< " + p[ "text" ]); } }
  1. Copy Source | Copy HTML class MainClass { public static void Main( string [] args) { Socket s = new Socket ( new TcpImplementation ()); s.Receive += new Socket . ReceiveEvent (OnReceive); s.Connect( "127.0.0.1:1111" ); while ( true ) { string ss = Console .ReadLine(); Packet p = new Packet ( "message" , new Argument ( "text" , ss)); p.Important = true ; s.Send(p); } } public static void OnReceive( Socket s, Packet p) { Console .WriteLine( "<< " + p[ "text" ]); } }
  2. Copy Source | Copy HTML class MainClass { public static void Main( string [] args) { Socket s = new Socket ( new TcpImplementation ()); s.Receive += new Socket . ReceiveEvent (OnReceive); s.Connect( "127.0.0.1:1111" ); while ( true ) { string ss = Console .ReadLine(); Packet p = new Packet ( "message" , new Argument ( "text" , ss)); p.Important = true ; s.Send(p); } } public static void OnReceive( Socket s, Packet p) { Console .WriteLine( "<< " + p[ "text" ]); } }
  3. Copy Source | Copy HTML class MainClass { public static void Main( string [] args) { Socket s = new Socket ( new TcpImplementation ()); s.Receive += new Socket . ReceiveEvent (OnReceive); s.Connect( "127.0.0.1:1111" ); while ( true ) { string ss = Console .ReadLine(); Packet p = new Packet ( "message" , new Argument ( "text" , ss)); p.Important = true ; s.Send(p); } } public static void OnReceive( Socket s, Packet p) { Console .WriteLine( "<< " + p[ "text" ]); } }
  4. Copy Source | Copy HTML class MainClass { public static void Main( string [] args) { Socket s = new Socket ( new TcpImplementation ()); s.Receive += new Socket . ReceiveEvent (OnReceive); s.Connect( "127.0.0.1:1111" ); while ( true ) { string ss = Console .ReadLine(); Packet p = new Packet ( "message" , new Argument ( "text" , ss)); p.Important = true ; s.Send(p); } } public static void OnReceive( Socket s, Packet p) { Console .WriteLine( "<< " + p[ "text" ]); } }
  5. Copy Source | Copy HTML class MainClass { public static void Main( string [] args) { Socket s = new Socket ( new TcpImplementation ()); s.Receive += new Socket . ReceiveEvent (OnReceive); s.Connect( "127.0.0.1:1111" ); while ( true ) { string ss = Console .ReadLine(); Packet p = new Packet ( "message" , new Argument ( "text" , ss)); p.Important = true ; s.Send(p); } } public static void OnReceive( Socket s, Packet p) { Console .WriteLine( "<< " + p[ "text" ]); } }
  6. Copy Source | Copy HTML class MainClass { public static void Main( string [] args) { Socket s = new Socket ( new TcpImplementation ()); s.Receive += new Socket . ReceiveEvent (OnReceive); s.Connect( "127.0.0.1:1111" ); while ( true ) { string ss = Console .ReadLine(); Packet p = new Packet ( "message" , new Argument ( "text" , ss)); p.Important = true ; s.Send(p); } } public static void OnReceive( Socket s, Packet p) { Console .WriteLine( "<< " + p[ "text" ]); } }
  7. Copy Source | Copy HTML class MainClass { public static void Main( string [] args) { Socket s = new Socket ( new TcpImplementation ()); s.Receive += new Socket . ReceiveEvent (OnReceive); s.Connect( "127.0.0.1:1111" ); while ( true ) { string ss = Console .ReadLine(); Packet p = new Packet ( "message" , new Argument ( "text" , ss)); p.Important = true ; s.Send(p); } } public static void OnReceive( Socket s, Packet p) { Console .WriteLine( "<< " + p[ "text" ]); } }
  8. Copy Source | Copy HTML class MainClass { public static void Main( string [] args) { Socket s = new Socket ( new TcpImplementation ()); s.Receive += new Socket . ReceiveEvent (OnReceive); s.Connect( "127.0.0.1:1111" ); while ( true ) { string ss = Console .ReadLine(); Packet p = new Packet ( "message" , new Argument ( "text" , ss)); p.Important = true ; s.Send(p); } } public static void OnReceive( Socket s, Packet p) { Console .WriteLine( "<< " + p[ "text" ]); } }
  9. Copy Source | Copy HTML class MainClass { public static void Main( string [] args) { Socket s = new Socket ( new TcpImplementation ()); s.Receive += new Socket . ReceiveEvent (OnReceive); s.Connect( "127.0.0.1:1111" ); while ( true ) { string ss = Console .ReadLine(); Packet p = new Packet ( "message" , new Argument ( "text" , ss)); p.Important = true ; s.Send(p); } } public static void OnReceive( Socket s, Packet p) { Console .WriteLine( "<< " + p[ "text" ]); } }
  10. Copy Source | Copy HTML class MainClass { public static void Main( string [] args) { Socket s = new Socket ( new TcpImplementation ()); s.Receive += new Socket . ReceiveEvent (OnReceive); s.Connect( "127.0.0.1:1111" ); while ( true ) { string ss = Console .ReadLine(); Packet p = new Packet ( "message" , new Argument ( "text" , ss)); p.Important = true ; s.Send(p); } } public static void OnReceive( Socket s, Packet p) { Console .WriteLine( "<< " + p[ "text" ]); } }
  11. Copy Source | Copy HTML class MainClass { public static void Main( string [] args) { Socket s = new Socket ( new TcpImplementation ()); s.Receive += new Socket . ReceiveEvent (OnReceive); s.Connect( "127.0.0.1:1111" ); while ( true ) { string ss = Console .ReadLine(); Packet p = new Packet ( "message" , new Argument ( "text" , ss)); p.Important = true ; s.Send(p); } } public static void OnReceive( Socket s, Packet p) { Console .WriteLine( "<< " + p[ "text" ]); } }
  12. Copy Source | Copy HTML class MainClass { public static void Main( string [] args) { Socket s = new Socket ( new TcpImplementation ()); s.Receive += new Socket . ReceiveEvent (OnReceive); s.Connect( "127.0.0.1:1111" ); while ( true ) { string ss = Console .ReadLine(); Packet p = new Packet ( "message" , new Argument ( "text" , ss)); p.Important = true ; s.Send(p); } } public static void OnReceive( Socket s, Packet p) { Console .WriteLine( "<< " + p[ "text" ]); } }
  13. Copy Source | Copy HTML class MainClass { public static void Main( string [] args) { Socket s = new Socket ( new TcpImplementation ()); s.Receive += new Socket . ReceiveEvent (OnReceive); s.Connect( "127.0.0.1:1111" ); while ( true ) { string ss = Console .ReadLine(); Packet p = new Packet ( "message" , new Argument ( "text" , ss)); p.Important = true ; s.Send(p); } } public static void OnReceive( Socket s, Packet p) { Console .WriteLine( "<< " + p[ "text" ]); } }
  14. Copy Source | Copy HTML class MainClass { public static void Main( string [] args) { Socket s = new Socket ( new TcpImplementation ()); s.Receive += new Socket . ReceiveEvent (OnReceive); s.Connect( "127.0.0.1:1111" ); while ( true ) { string ss = Console .ReadLine(); Packet p = new Packet ( "message" , new Argument ( "text" , ss)); p.Important = true ; s.Send(p); } } public static void OnReceive( Socket s, Packet p) { Console .WriteLine( "<< " + p[ "text" ]); } }
  15. Copy Source | Copy HTML class MainClass { public static void Main( string [] args) { Socket s = new Socket ( new TcpImplementation ()); s.Receive += new Socket . ReceiveEvent (OnReceive); s.Connect( "127.0.0.1:1111" ); while ( true ) { string ss = Console .ReadLine(); Packet p = new Packet ( "message" , new Argument ( "text" , ss)); p.Important = true ; s.Send(p); } } public static void OnReceive( Socket s, Packet p) { Console .WriteLine( "<< " + p[ "text" ]); } }
  16. Copy Source | Copy HTML class MainClass { public static void Main( string [] args) { Socket s = new Socket ( new TcpImplementation ()); s.Receive += new Socket . ReceiveEvent (OnReceive); s.Connect( "127.0.0.1:1111" ); while ( true ) { string ss = Console .ReadLine(); Packet p = new Packet ( "message" , new Argument ( "text" , ss)); p.Important = true ; s.Send(p); } } public static void OnReceive( Socket s, Packet p) { Console .WriteLine( "<< " + p[ "text" ]); } }
  17. Copy Source | Copy HTML class MainClass { public static void Main( string [] args) { Socket s = new Socket ( new TcpImplementation ()); s.Receive += new Socket . ReceiveEvent (OnReceive); s.Connect( "127.0.0.1:1111" ); while ( true ) { string ss = Console .ReadLine(); Packet p = new Packet ( "message" , new Argument ( "text" , ss)); p.Important = true ; s.Send(p); } } public static void OnReceive( Socket s, Packet p) { Console .WriteLine( "<< " + p[ "text" ]); } }
  18. Copy Source | Copy HTML class MainClass { public static void Main( string [] args) { Socket s = new Socket ( new TcpImplementation ()); s.Receive += new Socket . ReceiveEvent (OnReceive); s.Connect( "127.0.0.1:1111" ); while ( true ) { string ss = Console .ReadLine(); Packet p = new Packet ( "message" , new Argument ( "text" , ss)); p.Important = true ; s.Send(p); } } public static void OnReceive( Socket s, Packet p) { Console .WriteLine( "<< " + p[ "text" ]); } }


Download the library, source code, and also find some more complex client and server examples here:
GitHub Repository
Direct link to ZIP

Source: https://habr.com/ru/post/80940/


All Articles