本文整理汇总了C#中IClient.ConnectAsync方法的典型用法代码示例。如果您正苦于以下问题:C# IClient.ConnectAsync方法的具体用法?C# IClient.ConnectAsync怎么用?C# IClient.ConnectAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IClient
的用法示例。
在下文中一共展示了IClient.ConnectAsync方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: InitializeCommandChannel
/// <summary>
/// Initialize command channel.
/// </summary>
/// <param name="connectionString">Command channel connection string.</param>
private void InitializeCommandChannel(string connectionString)
{
// Parse command channel connection settings
TransportProtocol transportProtocol;
Dictionary<string, string> settings = connectionString.ParseKeyValuePairs();
string setting;
// Verify user did not attempt to setup command channel as a TCP server
if (settings.ContainsKey("islistener") && settings["islistener"].ParseBoolean())
throw new ArgumentException("Command channel cannot be setup as a TCP server.");
// Determine what transport protocol user selected
if (settings.TryGetValue("transportProtocol", out setting) || settings.TryGetValue("protocol", out setting))
{
transportProtocol = (TransportProtocol)Enum.Parse(typeof(TransportProtocol), setting, true);
// The communications engine only recognizes the transport protocol key as "protocol"
connectionString = connectionString.ReplaceCaseInsensitive("transportProtocol", "protocol");
}
else
throw new ArgumentException("No transport protocol was specified for command channel. For example: \"transportProtocol=Tcp\".");
// Validate command channel transport protocol selection
if (transportProtocol != TransportProtocol.Tcp && transportProtocol != TransportProtocol.Serial && transportProtocol != TransportProtocol.File)
throw new ArgumentException("Command channel transport protocol can only be defined as TCP, Serial or File");
// Instantiate command channel based on defined transport layer
m_commandChannel = ClientBase.Create(connectionString);
// Setup event handlers
m_commandChannel.ConnectionEstablished += m_commandChannel_ConnectionEstablished;
m_commandChannel.ConnectionAttempt += m_commandChannel_ConnectionAttempt;
m_commandChannel.ConnectionException += m_commandChannel_ConnectionException;
m_commandChannel.ConnectionTerminated += m_commandChannel_ConnectionTerminated;
m_commandChannel.ReceiveData += m_commandChannel_ReceiveData;
m_commandChannel.ReceiveDataException += m_commandChannel_ReceiveDataException;
m_commandChannel.SendDataException += m_commandChannel_SendDataException;
m_commandChannel.UnhandledUserException += m_commandChannel_UnhandledUserException;
// Attempt connection to device over command channel
m_commandChannel.ReceiveBufferSize = m_bufferSize;
m_commandChannel.MaxConnectionAttempts = m_maximumConnectionAttempts;
m_commandChannel.ConnectAsync();
}
示例2: InitializeDataChannel
/// <summary>
/// Initialize data channel.
/// </summary>
/// <param name="settings">Key/value pairs dictionary parsed from connection string.</param>
private void InitializeDataChannel(Dictionary<string, string> settings)
{
string setting;
// Instantiate selected transport layer
switch (m_transportProtocol)
{
case TransportProtocol.Tcp:
// The TCP transport may be set up as a server or as a client, we distinguish
// this simply by deriving the value of an added key/value pair in the
// connection string called "IsListener"
if (settings.TryGetValue("islistener", out setting))
{
if (setting.ParseBoolean())
m_serverBasedDataChannel = settings.ContainsKey("receiveFrom") ? (IServer)new SharedTcpServerReference() : new TcpServer();
else
m_dataChannel = new TcpClient();
}
else
{
// If the key doesn't exist, we assume it's a client connection
m_dataChannel = new TcpClient();
}
break;
case TransportProtocol.Udp:
InitializeUdpDataChannel(settings);
break;
case TransportProtocol.Serial:
m_dataChannel = new SerialClient();
m_initiatingSerialConnection = true;
break;
case TransportProtocol.File:
m_dataChannel = new FileClient();
// For file based playback, we allow the option of auto-repeat
FileClient fileClient = (FileClient)m_dataChannel;
fileClient.FileOpenMode = FileMode.Open;
fileClient.FileAccessMode = FileAccess.Read;
fileClient.FileShareMode = FileShare.Read;
fileClient.ReceiveOnDemand = true;
fileClient.ReceiveBufferSize = ushort.MaxValue;
fileClient.AutoRepeat = m_autoRepeatCapturedPlayback;
// Setup synchronized read operation for file client operations
m_readNextBuffer = new ShortSynchronizedOperation(ReadNextFileBuffer, ex => OnParsingException(new InvalidOperationException("Encountered an exception while reading file data: " + ex.Message, ex)));
break;
default:
throw new InvalidOperationException(string.Format("Transport protocol \"{0}\" is not recognized, failed to initialize data channel", m_transportProtocol));
}
// Handle primary data connection, this *must* be defined...
if ((object)m_dataChannel != null)
{
// Setup event handlers
m_dataChannel.ConnectionEstablished += m_dataChannel_ConnectionEstablished;
m_dataChannel.ConnectionAttempt += m_dataChannel_ConnectionAttempt;
m_dataChannel.ConnectionException += m_dataChannel_ConnectionException;
m_dataChannel.ConnectionTerminated += m_dataChannel_ConnectionTerminated;
m_dataChannel.ReceiveData += m_dataChannel_ReceiveData;
m_dataChannel.ReceiveDataException += m_dataChannel_ReceiveDataException;
m_dataChannel.SendDataException += m_dataChannel_SendDataException;
m_dataChannel.UnhandledUserException += m_dataChannel_UnhandledUserException;
// Attempt connection to device
m_dataChannel.ReceiveBufferSize = m_bufferSize;
m_dataChannel.ConnectionString = m_connectionString;
m_dataChannel.MaxConnectionAttempts = m_maximumConnectionAttempts;
m_dataChannel.ConnectAsync();
}
else if ((object)m_serverBasedDataChannel != null)
{
// Setup event handlers
m_serverBasedDataChannel.ClientConnected += m_serverBasedDataChannel_ClientConnected;
m_serverBasedDataChannel.ClientDisconnected += m_serverBasedDataChannel_ClientDisconnected;
m_serverBasedDataChannel.ClientConnectingException += m_serverBasedDataChannel_ClientConnectingException;
m_serverBasedDataChannel.ServerStarted += m_serverBasedDataChannel_ServerStarted;
m_serverBasedDataChannel.ServerStopped += m_serverBasedDataChannel_ServerStopped;
m_serverBasedDataChannel.ReceiveClientData += m_serverBasedDataChannel_ReceiveClientData;
m_serverBasedDataChannel.ReceiveClientDataException += m_serverBasedDataChannel_ReceiveClientDataException;
m_serverBasedDataChannel.SendClientDataException += m_serverBasedDataChannel_SendClientDataException;
m_serverBasedDataChannel.UnhandledUserException += m_serverBasedDataChannel_UnhandledUserException;
// Listen for device connection
m_serverBasedDataChannel.ReceiveBufferSize = m_bufferSize;
m_serverBasedDataChannel.ConfigurationString = m_connectionString;
m_serverBasedDataChannel.MaxClientConnections = 1;
m_serverBasedDataChannel.Start();
}
else
{
throw new InvalidOperationException("No data channel was initialized, cannot start frame parser");
}
}
示例3: Main
static void Main(string[] args)
{
if (args.Length < 3)
{
ShowHelp();
return;
}
string protocol = args[0].ToUpper();
string connectionString, fileName;
string networkInterface = "0.0.0.0";
switch (protocol)
{
case "TCP":
if (args.Length > 3)
networkInterface = args[3];
connectionString = string.Format("protocol=TCP; server={0}; interface={1}", args[1], networkInterface);
fileName = FilePath.GetAbsolutePath(args[2]);
break;
case "UDP":
if (args.Length > 3)
networkInterface = args[3];
connectionString = string.Format("protocol=UDP; port={0}; interface={1}", args[1], networkInterface);
fileName = FilePath.GetAbsolutePath(args[2]);
break;
case "MULTICAST":
if (args.Length < 5)
{
ShowHelp();
return;
}
if (args.Length > 5)
networkInterface = args[5];
connectionString = string.Compare(args[3], "ANY", true) == 0 ?
string.Format("protocol=UDP; port={0}; server={1}; interface={2}", args[1], args[2], networkInterface) :
string.Format("protocol=UDP; port={0}; server={1}; multicastSource={2}; interface={3}", args[1], args[2], args[3], networkInterface);
fileName = FilePath.GetAbsolutePath(args[4]);
break;
default:
ShowHelp();
return;
}
Console.WriteLine("\r\nCapturing {0} stream \"{1}\" to file \"{2}\"...\r\n\r\nPress any key to complete capture...\r\n", protocol, connectionString, fileName);
stream = File.Create(fileName);
socket = ClientBase.Create(connectionString);
socket.MaxConnectionAttempts = -1;
socket.ConnectionAttempt += socket_ConnectionAttempt;
socket.ConnectionEstablished += socket_ConnectionEstablished;
socket.ConnectionException += socket_ConnectionException;
socket.ConnectionTerminated += socket_ConnectionTerminated;
socket.ReceiveData += socket_ReceiveData;
socket.ReceiveDataException += socket_ReceiveDataException;
socket.ConnectAsync();
Console.ReadKey();
socket.Dispose();
socket.ConnectionAttempt -= socket_ConnectionAttempt;
socket.ConnectionEstablished -= socket_ConnectionEstablished;
socket.ConnectionException -= socket_ConnectionException;
socket.ConnectionTerminated -= socket_ConnectionTerminated;
socket.ReceiveData -= socket_ReceiveData;
socket.ReceiveDataException -= socket_ReceiveDataException;
stream.Close();
}