| Vito Pantaleo |
TCP/IP e Seriale
Salve a tutti,
Come posso implementare una connessione TCP/IP e quindi sfruttare il .NET Remoting su due computer collegati tra di loro tramite cavo seriale (2 fili) e modem analogici ? |
| Andrea Piccoli |
Re: TCP/IP e Seriale
credo che la soluzione sia creare un proprio channel per la remoting.
un channel per la remoting è realizzato da una coppia di classi che ne implementano l'interfaccia lato client e lato server lato client: /// <summary>Creates a channel sink for the secure client channel through which remoting messages flow.</summary> public class SecureClientChannelSinkProvider : IClientChannelSinkProvider { e lato server /// <summary>Creates channel sinks for the secure server channel through which remoting messages flow.</summary> public class SecureServerChannelSinkProvider : IServerChannelSinkProvider mentre nei due file di configurazione dovrai mettere: // or the client, add the following line to the <clientProviders> section of the // configuration file after the the formatter provider: // <provider type="TFFNETSecureChannel.Remoting.SecureClientChannelSinkProvider, SecureChannel" /> // For the server, add the following line to the <serverProviders> section of the // configuration file before the formatter provider: // <provider type="TFFNETSecureChannel.Remoting.SecureServerChannelSinkProvider, SecureChannel" /> A questo punto all'interno dei due sink devi utilizzare un tuo carrier in cui realizzare la logica di trasmissione: ad esempio: namespace TFFNETSecureChannel.IO { /// <summary> /// A simple wrapper for any object that implements Stream. All calls /// to the carrier class are passed on to the underlying stream, EXCEPT /// for Close() and Dispose(); this allows the carrier stream to be passed /// to objects such as CryptoStream and StreamWriter such that when they close, /// the carrier stream will be closed but the underlying stream will not be. /// </summary> public class CarrierStream : Stream { /// <summary>The stream carried with this wrapper.</summary> private Stream _baseStream = null; /// <summary>Initializes the CarrierStream.</summary> /// <param name="baseStream">The stream to be carried with this wrapper.</param> public CarrierStream(Stream baseStream) { if (baseStream == null) throw new ArgumentNullException("baseStream"); _baseStream = baseStream; } /// <summary>Gets the stream to be carried with this wrapper.</summary> public Stream BaseStream { get { return _baseStream; } } /// <summary>Begins an asynchronous read operation.</summary> /// <param name="buffer">The buffer to read the data into.</param> /// <param name="offset">The byte offset in buffer at which to begin writing data read from the stream.</param> /// <param name="count">The maximum number of bytes to read.</param> /// <param name="callback">An optional asynchronous callback, to be called when the read is complete.</param> /// <param name="state">A user-provided object that distinguishes this particular asynchronous read request from other requests.</param> /// <returns>An IAsyncResult that represents the asynchronous read, which could still be pending.</returns> public override System.IAsyncResult BeginRead(byte[] buffer, int offset, int count, System.AsyncCallback callback, object state) { return _baseStream.BeginRead(buffer, offset, count, callback, state); } /// <summary>Begins an asynchronous write operation.</summary> /// <param name="buffer">The buffer to write data to. This should generally be greater than 64 kilobytes.</param> /// <param name="offset">The byte offset in buffer at which to begin writing.</param> /// <param name="count">The maximum number of bytes to write.</param> /// <param name="callback">An optional asynchronous callback, to be called when the write is complete.</param> /// <param name="state">A user-provided object that distinguishes this particular asynchronous write request from other requests.</param> /// <returns>An IAsyncResult that represents the asynchronous write, which could still be pending.</returns> public override System.IAsyncResult BeginWrite(byte[] buffer, int offset, int count, System.AsyncCallback callback, object state) { return _baseStream.BeginWrite(buffer, offset, count, callback, state); } /// <summary>When overridden in a derived class, gets a value indicating whether the /// current stream supports reading.</summary> public override bool CanRead { get { return _baseStream.CanRead; } } /// <summary>When overridden in a derived class, gets a value indicating whether the /// current stream supports seeking.</summary> public override bool CanSeek { get { return _baseStream.CanSeek; } } /// <summary>When overridden in a derived class, gets a value indicating whether the /// current stream supports writing.</summary> public override bool CanWrite { get { return _baseStream.CanWrite; } } /// <summary> /// While most calls on the carrier stream forward to the underlying stream, <c>Close()</c> does /// not, as the whole purpose of this class is to not close the underlying stream when /// <c>Close()</c> is called. /// </summary> public override void Close() { // do nothing } /// <summary>Waits for the pending asynchronous read to complete.</summary> /// <param name="asyncResult">The reference to the pending asynchronous request to finish.</param> /// <returns>The number of bytes read from the stream.</returns> public override int EndRead(System.IAsyncResult asyncResult) { return _baseStream.EndRead(asyncResult); } /// <summary>Ends an asynchronous write operation.</summary> /// <param name="asyncResult">A reference to the outstanding asynchronous I/O request.</param> public override void EndWrite(System.IAsyncResult asyncResult) { _baseStream.EndWrite(asyncResult); } /// <summary>When overridden in a derived class, clears all buffers for this stream and /// causes any buffered data to be written to the underlying device.</summary> public override void Flush() { _baseStream.Flush(); } /// <summary>When overridden in a derived class, gets the length in bytes of the stream.</summary> public override long Length { get { return _baseStream.Length; } } /// <summary>When overridden in a derived class, gets or sets the position /// within the current stream.</summary> public override long Position { get { return _baseStream.Position; } set { _baseStream.Position = value; } } /// <summary>When overridden in a derived class, reads a sequence of bytes from the /// current stream and advances the position within the stream by the number of bytes read.</summary> /// <param name="buffer">An array of bytes. When this method returns, the buffer contains /// the specified byte array with the values between offset and (offset + count) replaced by /// the bytes read from the current source.</param> /// <param name="offset">The zero-based byte offset in buffer at which to begin storing the data read from the current stream.</param> /// <param name="count">The maximum number of bytes to be read from the current stream.</param> /// <returns>The total number of bytes read into the buffer.</returns> public override int Read(byte[] buffer, int offset, int count) { return _baseStream.Read(buffer, offset, count); } /// <summary> /// Reads a byte from the stream and advances the position within the stream by one byte, or returns -1 if at the end of the stream. /// </summary> /// <returns>The unsigned byte cast to an Int32, or -1 if at the end of the stream.</returns> public override int ReadByte() { return _baseStream.ReadByte(); } /// <summary> /// When overridden in a derived class, sets the position within the current stream. /// </summary> /// <param name="offset">A byte offset relative to origin.</param> /// <param name="origin">A value of type SeekOrigin indicating the reference point /// used to obtain the new position.</param> /// <returns>The new position within the current stream.</returns> public override long Seek(long offset, System.IO.SeekOrigin origin) { return _baseStream.Seek(offset, origin); } /// <summary> /// When overridden in a derived class, sets the length of the current stream. /// </summary> /// <param name="length">The desired length of the current stream in bytes.</param> public override void SetLength(long length) { _baseStream.SetLength(length); } /// <summary> /// When overridden in a derived class, writes a sequence of bytes to the current /// stream and advances the current position within this stream by the number of bytes written. /// </summary> /// <param name="buffer">An array of bytes. This method copies count bytes from buffer to the current stream.</param> /// <param name="offset">The zero-based byte offset in buffer at which to begin copying bytes to the current stream.</param> /// <param name="count">The number of bytes to be written to the current stream.</param> public override void Write(byte[] buffer, int offset, int count) { _baseStream.Write(buffer, offset, count); } /// <summary> /// Writes a byte to the current position in the stream and advances the position within the stream by one byte. /// </summary> /// <param name="input">The byte to write to the stream.</param> public override void WriteByte(byte input) { _baseStream.WriteByte(input); } /// <summary> /// While Dispose() on a stream would normally close the stream, in this case we want /// to do nothing of the sort. <see cref="Close"/> /// </summary> public void Dispose() { // do nothing GC.SuppressFinalize(this); } } |