Allow external processing of objects in my connection system.
This commit is contained in:
parent
880b23db4c
commit
d9a6e8215c
92
Conn.cs
92
Conn.cs
@ -11,29 +11,30 @@ namespace lib
|
||||
|
||||
|
||||
|
||||
public class Conn
|
||||
|
||||
public interface IProcess
|
||||
{
|
||||
void process( object obj );
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public class Conn
|
||||
{
|
||||
public Socket Sock { get { return m_socket; } }
|
||||
public Stream Stream { get { return m_streamNet; } }
|
||||
public IFormatter Formatter { get { return m_formatter; } }
|
||||
|
||||
|
||||
public Conn( Socket sock, IFormatter formatter )
|
||||
public Conn( Socket sock, IProcess proc )
|
||||
{
|
||||
m_socket = sock;
|
||||
|
||||
//sock.DontFragment = true;
|
||||
sock.NoDelay = true;
|
||||
|
||||
m_streamNet = new NetworkStream( m_socket );
|
||||
//m_streamBufIn = new BufferedStream( m_streamNet );
|
||||
|
||||
m_formatter = formatter;
|
||||
//m_formatter = new VersionFormatter();
|
||||
|
||||
//mm_memStream = new MemoryStream( mm_buffer );
|
||||
|
||||
|
||||
m_proc = proc;
|
||||
}
|
||||
|
||||
public object recieveObject()
|
||||
@ -44,17 +45,16 @@ public class Conn
|
||||
public object recieveObject( Stream stream )
|
||||
{
|
||||
object obj = null;
|
||||
lock( m_formatter )
|
||||
|
||||
var formatter = new XmlFormatter2();
|
||||
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
obj = m_formatter.Deserialize( stream );
|
||||
}
|
||||
catch( System.Xml.XmlException e )
|
||||
{
|
||||
lib.Log.error( "Outer Exception {0}", e.ToString() );
|
||||
//lib.Log.error( "Inner Exception {0}", e.InnerException.ToString() );
|
||||
}
|
||||
obj = formatter.Deserialize( stream );
|
||||
}
|
||||
catch( System.Xml.XmlException e )
|
||||
{
|
||||
lib.Log.error( "Outer Exception {0}", e.ToString() );
|
||||
}
|
||||
|
||||
return obj;
|
||||
@ -62,44 +62,46 @@ public class Conn
|
||||
|
||||
public void send( object obj )
|
||||
{
|
||||
lock( m_formatter )
|
||||
|
||||
var formatter = new XmlFormatter2();
|
||||
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
var ms = new MemoryStream( 1024 );
|
||||
m_formatter.Serialize( ms, obj );
|
||||
var ms = new MemoryStream( 1024 );
|
||||
formatter.Serialize( ms, obj );
|
||||
|
||||
//var str = System.Text.Encoding.Default.GetString( mm_buffer, 0, (int)ms.Position );
|
||||
//lib.Log.info( "Sent data {0} of length {1}", str, ms.Position );
|
||||
//lib.Log.info( "Sent {0}", obj );
|
||||
//var str = System.Text.Encoding.Default.GetString( mm_buffer, 0, (int)ms.Position );
|
||||
//lib.Log.info( "Sent data {0} of length {1}", str, ms.Position );
|
||||
//lib.Log.info( "Sent {0}", obj );
|
||||
|
||||
byte[] byteSize = BitConverter.GetBytes( (uint)ms.Position );
|
||||
m_streamNet.Write( byteSize, 0, 4 );
|
||||
m_streamNet.Write( ms.GetBuffer(), 0, (int)ms.Position );
|
||||
byte[] byteSize = BitConverter.GetBytes( (uint)ms.Position );
|
||||
m_streamNet.Write( byteSize, 0, 4 );
|
||||
m_streamNet.Write( ms.GetBuffer(), 0, (int)ms.Position );
|
||||
|
||||
m_streamNet.Flush();
|
||||
}
|
||||
catch( Exception e )
|
||||
{
|
||||
lib.Log.warn( $"Exception sending obj {obj} of {e}" );
|
||||
throw;
|
||||
}
|
||||
m_streamNet.Flush();
|
||||
}
|
||||
catch( Exception e )
|
||||
{
|
||||
lib.Log.warn( $"Exception sending obj {obj} of {e}" );
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void recieve( object obj )
|
||||
{
|
||||
//Log.log.msg( "Recieved " + obj.ToString() );
|
||||
if( m_proc != null ) m_proc.process( obj );
|
||||
}
|
||||
|
||||
private Socket m_socket;
|
||||
Socket m_socket;
|
||||
|
||||
NetworkStream m_streamNet;
|
||||
|
||||
IProcess m_proc;
|
||||
|
||||
|
||||
|
||||
private NetworkStream m_streamNet;
|
||||
//private BufferedStream m_streamBufIn;
|
||||
//private BufferedStream m_streamBufOut;
|
||||
|
||||
private IFormatter m_formatter;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user