/// <summary> /// Returns a channel message sink that delivers messages to the specified URL or channel data object. /// </summary> /// <param name="url">The URL to which the new sink will deliver messages. Can be null.</param> /// <param name="remoteChannelData">The channel data object of the remote host to which the new sink will deliver messages. Can be null.</param> /// <param name="objectURI">When this method returns, contains a URI of the new channel message sink that delivers messages to the specified URL or channel data object. This parameter is passed uninitialized.</param> /// <returns> /// A channel message sink that delivers messages to the specified URL or channel data object, or null if the channel cannot connect to the given endpoint. /// </returns> /// <exception cref="T:System.Security.SecurityException">The immediate caller does not have infrastructure permission. </exception> public IMessageSink CreateMessageSink(string url, object remoteChannelData, out string objectURI) { objectURI = null; if (url == null) { TcpExChannelData channelData = remoteChannelData as TcpExChannelData; if (channelData != null) { url = Manager.CreateUrl(channelData.ChannelID); } else { return(null); } } if (Manager.Parse(url, out objectURI) != null) { IClientChannelSink clientChannelSink = clientSinkProvider.CreateSink(this, url, remoteChannelData); IMessageSink messageSink = clientChannelSink as IMessageSink; if (clientChannelSink != null && messageSink == null) { throw new RemotingException(LanguageResource.RemotingException_MessageSinkNotSet); } return(messageSink); } else { return(null); } }
/// <summary> /// Receives channel data. /// </summary> /// <returns>True, if receiving was successfully, otherwise false</returns> private bool ReceiveChannelInfo() { var stream = Stream; if (stream != null) { BinaryFormatter formatter = new BinaryFormatter(); _remoteChannelData = (TcpExChannelData)formatter.Deserialize(stream); Trace.WriteLine("TcpEx.Connection received remote ChannelInfo, ChannelID: {0}", _remoteChannelData.ChannelID); return(true); } return(false); }
/// <summary> /// Instructs the current channel to start listening for requests. /// </summary> /// <param name="data">Optional initialization information.</param> /// <exception cref="T:System.Security.SecurityException">The immediate caller does not have infrastructure permission. </exception> public void StartListening(object data) { if (this.port != 0) { throw new InvalidOperationException("Channel is already listening. TcpEx currently only allows listening on one port."); } if (data is int) { this.port = Manager.StartListening((int)data, this, _bindToAddress); channelData = new TcpExChannelData(this); foreach (string url in Manager.GetAddresses(this.port, Guid.Empty)) { Manager.BeginReadMessage(url, null, new AsyncCallback(messageSink.ReceiveMessage), url); } } }
private void Initialise(TypeFilterLevel typeFilterLevel, IClientChannelSinkProvider clientSinkProvider, IServerChannelSinkProvider serverSinkProvider, int port, bool listen, bool keepAlive, ulong keepAliveTime, ulong KeepAliveInterval, short maxRetries, int retryDelay, IPAddress bindToAddress) { _tcpKeepAliveEnabled = keepAlive; _tcpKeepAliveTime = keepAliveTime; _tcpKeepAliveInterval = KeepAliveInterval; _maxRetries = maxRetries; _retryDelay = retryDelay; _bindToAddress = bindToAddress; if (clientSinkProvider == null) { clientSinkProvider = new BinaryClientFormatterSinkProvider(); } if (serverSinkProvider == null) { Trace.WriteLine("Setting serialization filter: {0}", typeFilterLevel); BinaryServerFormatterSinkProvider tempProvider = new BinaryServerFormatterSinkProvider(); tempProvider.TypeFilterLevel = typeFilterLevel; serverSinkProvider = tempProvider; } // Initialise clientSinkProvider this.clientSinkProvider = clientSinkProvider; while (clientSinkProvider.Next != null) { clientSinkProvider = clientSinkProvider.Next; } clientSinkProvider.Next = new ClientTransportSinkProvider(); messageSink = new ServerTransportSink(ChannelServices.CreateServerChannelSinkChain(serverSinkProvider, this)); serverSinkProvider.GetChannelData(channelData); if (listen) { StartListening(port); } channelData = new TcpExChannelData(this); Manager.BeginReadMessage(_channelID, null, new AsyncCallback(messageSink.ReceiveMessage), _channelID); }
private void Initialise(TypeFilterLevel typeFilterLevel, IClientChannelSinkProvider clientSinkProvider, IServerChannelSinkProvider serverSinkProvider, int port, bool listen, bool keepAlive, ulong keepAliveTime, ulong KeepAliveInterval, short maxRetries, int retryDelay, IPAddress bindToAddress) { _tcpKeepAliveEnabled = keepAlive; _tcpKeepAliveTime = keepAliveTime; _tcpKeepAliveInterval = KeepAliveInterval; _maxRetries = maxRetries; _retryDelay = retryDelay; _bindToAddress = bindToAddress; if (clientSinkProvider == null) clientSinkProvider = new BinaryClientFormatterSinkProvider(); if (serverSinkProvider == null) { Trace.WriteLine("Setting serialization filter: {0}", typeFilterLevel); BinaryServerFormatterSinkProvider tempProvider = new BinaryServerFormatterSinkProvider(); tempProvider.TypeFilterLevel = typeFilterLevel; serverSinkProvider = tempProvider; } // Initialise clientSinkProvider this.clientSinkProvider = clientSinkProvider; while (clientSinkProvider.Next != null) clientSinkProvider = clientSinkProvider.Next; clientSinkProvider.Next = new ClientTransportSinkProvider(); messageSink = new ServerTransportSink(ChannelServices.CreateServerChannelSinkChain(serverSinkProvider, this)); serverSinkProvider.GetChannelData(channelData); if (listen) { StartListening(port); } channelData = new TcpExChannelData(this); Manager.BeginReadMessage(_channelID, null, new AsyncCallback(messageSink.ReceiveMessage), _channelID); }
/// <summary> /// Instructs the current channel to start listening for requests. /// </summary> /// <param name="data">Optional initialization information.</param> /// <exception cref="T:System.Security.SecurityException">The immediate caller does not have infrastructure permission. </exception> public void StartListening(object data) { if (port != 0) throw new InvalidOperationException("Channel is already listening. TcpEx currently only allows listening on one port."); if (data is int) { port = (int)data; channelData = new TcpExChannelData(this); ListenerSocket = Manager.StartListening(port, this, _bindToAddress); ListenerAddresses = Manager.GetAddresses(port, Guid.Empty, true); foreach (string url in ListenerAddresses) { Manager.BeginReadMessage(url, null, new AsyncCallback(messageSink.ReceiveMessage), url); } } }
/// <summary> /// Receives channel data. /// </summary> /// <returns>True, if receiving was successfully, otherwise false</returns> private bool ReceiveChannelInfo() { Stream stream = this.Stream; if (stream != null) { BinaryFormatter formatter = new BinaryFormatter(); _remoteChannelData = (TcpExChannelData)formatter.Deserialize(stream); Trace.WriteLine("TcpEx.Connection received remote ChannelInfo, ChannelID: {0}", _remoteChannelData.ChannelID); return true; } return false; }
/// <summary> /// Closes the connection. /// </summary> public void Close() { lock (_connectionsLockObject) { List<string> toBeDeleted = (from pair in _connections where pair.Value == this select pair.Key).ToList(); foreach (string key in toBeDeleted) { _connections.Remove(key); } } LockRead(); LockWrite(); _socketRemoteAddress = null; _socketRemotePort = 0; if (_reader != null) { try { _reader.Close(); } catch (ObjectDisposedException) { } finally { _reader = null; } } if (_writer != null) { try { _writer.Close(); } catch (ObjectDisposedException) { } finally { _writer = null; } } if (_stream != null) { try { _stream.Close(); } catch (ObjectDisposedException) { } finally { _stream = null; } } if (_socket != null) { try { _socket.Close(); } catch (ObjectDisposedException) { } finally { _socket = null; } } if (_channel != null) _channel = null; if (_remoteChannelData != null) _remoteChannelData = null; ReleaseRead(); ReleaseWrite(); _isClosed = true; }
/// <summary> /// Closes the connection. /// </summary> public void Close() { lock (_connectionsLockObject) { var toBeDeleted = from pair in _connections where pair.Value == this select pair.Key; foreach (string key in toBeDeleted.ToList()) { _connections.Remove(key); } } LockRead(); LockWrite(); _socketRemoteAddress = null; _socketRemotePort = 0; if (_reader != null) { try { _reader.Close(); } catch (ObjectDisposedException) { } finally { _reader = null; } } if (_writer != null) { try { _writer.Close(); } catch (ObjectDisposedException) { } finally { _writer = null; } } if (_stream != null) { try { _stream.Close(); } catch (ObjectDisposedException) { } finally { _stream = null; } } if (_socket != null) { try { _socket.Shutdown(SocketShutdown.Both); _socket.Close(); } catch (SocketException) { } catch (ObjectDisposedException) { } finally { _socket = null; } } if (_channel != null) { _channel = null; } if (_remoteChannelData != null) { _remoteChannelData = null; } ReleaseRead(); ReleaseWrite(); _isClosed = true; }