本文整理汇总了C#中IConnection.Connect方法的典型用法代码示例。如果您正苦于以下问题:C# IConnection.Connect方法的具体用法?C# IConnection.Connect怎么用?C# IConnection.Connect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IConnection
的用法示例。
在下文中一共展示了IConnection.Connect方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RunClientSubscriber
private void RunClientSubscriber(IConnection<SimpleMessage<int>, int> connection, IConnectionStubListener<SimpleMessage<int>, int> listener, ushort port)
{
listener.BindTo(new IPEndPoint(IPAddress.Any, port));
var listenerTask = Task.Run(() => RunListenerSubscriber(listener));
var ready = false;
connection.Subscribe(0, (message, size) => ready = true);
connection.Subscribe(1, (message, size) => TextMessageHandler("Client", message, size));
connection.Connect(new IPEndPoint(IPAddress.Loopback, port));
while (!ready)
Thread.CurrentThread.Join(10);
const int messages = 500;
Console.WriteLine(@"Sending {0} messages with random data", 5);
var rnd = new Random();
for (var i = 0; i < messages; ++i)
{
var data = new byte[rnd.Next(16, 256)];
rnd.NextBytes(data);
connection.Send(new SimpleMessage<int>(1, Encoding.ASCII.GetBytes(Convert.ToBase64String(data))));
}
Console.WriteLine(@"Client: Done, sending exit message");
connection.Send(new SimpleMessage<int>(2));
connection.Disconnect();
Console.WriteLine(@"Waiting for listener thread to exit");
listenerTask.Wait();
Console.WriteLine(@"Listener thread has exited");
Console.WriteLine();
Console.WriteLine(@"Press any key to exit");
Console.ReadKey();
}
示例2: Connect
public bool Connect()
{
if (_formatter == null)
{
throw new Exception("Channel formatter is not specified");
}
if (_eventListener == null)
{
throw new Exception("There is no channel event listener specified");
}
try
{
if (_connection == null)
{
_connection = new TcpConnection();
if (!string.IsNullOrEmpty(_bindIP))
_connection.Bind(_bindIP);
_connection.Connect(_serverIP, _port);
_receiverThread = new Thread(new ThreadStart(Run));
_receiverThread.IsBackground = true;
_receiverThread.Start();
return true;
}
}
catch (ConnectionException ce)
{
if (_traceProvider != null)
{
_traceProvider.TraceError(Name + ".Connect", ce.ToString());
}
throw new ChannelException(ce.Message, ce);
}
return false;
}
示例3: Connect
/** \fn void Connect(string username, string password, ConfigOptions options)
* \brief Connect to remote Sequoiadb database server
* \username Sequoiadb connection user name
* \password Sequoiadb connection password
* \options The options for connection
* \return void
* \exception SequoiaDB.BaseException
* \exception System.Exception
*/
public void Connect(string username, string password, ConfigOptions options)
{
ConfigOptions opts = options;
if (username == null)
username = "";
if (password == null)
password = "";
this.userName = username;
this.password = password;
if (options == null)
opts = new ConfigOptions();
if (connection == null)
{
// single address
if (serverAddress != null)
{
// connect
try
{
connection = new ConnectionTCPImpl(serverAddress, opts);
connection.Connect();
}
catch (System.Exception e)
{
connection = null;
throw e;
}
}
// several addresses
else if (serverAddresses != null)
{
int size = serverAddresses.Length;
Random random = new Random();
int count = random.Next(size);
int mark = count;
do
{
count = ++count % size;
try
{
ServerAddress conn = serverAddresses[count];
connection = new ConnectionTCPImpl(conn, opts);
connection.Connect();
}
catch (System.Exception)
{
if (mark == count)
{
throw new BaseException("SDB_NET_CANNOT_CONNECT");
}
continue;
}
break;
} while(mark != count);
}
else
{
throw new BaseException("SDB_NET_CANNOT_CONNECT");
}
// get endian info
isBigEndian = RequestSysInfo();
// authentication
try
{
Auth();
}
catch (BaseException e)
{
throw e;
}
}
}