📄 coordinator.cs
字号:
// **********************************************************************
//
// Copyright (c) 2003-2008 ZeroC, Inc. All rights reserved.
//
// This copy of Chat Demo is licensed to you under the terms
// described in the CHAT_DEMO_LICENSE file included in this
// distribution.
//
// **********************************************************************
using System;
using System.Security.Cryptography.X509Certificates;
using System.Windows;
using System.ComponentModel;
using System.Windows.Data;
using System.Windows.Threading;
using System.Windows.Input;
namespace ChatDemoGUI
{
//
// This class cordinates the chat client logic.
//
public class Coordinator
{
private class CertificateVerifier : IceSSL.CertificateVerifier
{
public
CertificateVerifier(X509Certificate2 caCert)
{
_caCert = caCert;
}
public bool
verify(IceSSL.ConnectionInfo info)
{
if(info.certs != null && info.certs.Length > 1)
{
if(info.certs[info.certs.Length - 1].Equals(_caCert))
{
return true;
}
}
string msg = "The server certificate does not match the official ZeroC chat server\n" +
"certificate, do you want to continue and connect to this chat server?";
if(MessageBox.Show(msg, "Chat Demo - Warning", MessageBoxButton.YesNo, MessageBoxImage.Warning) ==
MessageBoxResult.Yes)
{
return true;
}
return false;
}
private X509Certificate2 _caCert;
};
public enum ClientState { Disconnected, Connecting, Connected, ConnectionLost, Disconnecting }
public Coordinator(string[] args)
{
_app = (App)System.Windows.Application.Current;
ObjectDataProvider odp = (ObjectDataProvider)_app.Resources["UserList"];
_users = (UserList)odp.Data;
odp = (ObjectDataProvider)_app.Resources["ChatModel"];
_model = (ChatModel)odp.Data;
_args = args;
// Create a background worker thread dedicated to the login operation.
_loginWorker = new BackgroundWorker();
_loginWorker.DoWork += new DoWorkEventHandler(loginDoWork);
_loginWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(loginCompleted);
// Create a background worker thread dedicated to the logout operation.
_logoutWorker = new BackgroundWorker();
_logoutWorker.DoWork += new DoWorkEventHandler(logoutDoWork);
_logoutWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(logoutCompleted);
}
public void login(LoginInfo info)
{
lock(this)
{
setState(ClientState.Connecting);
_loginWorker.RunWorkerAsync(info);
}
}
public void logout()
{
lock(this)
{
setState(ClientState.Disconnecting);
_logoutWorker.RunWorkerAsync();
}
}
public void initEvent(string[] users)
{
System.Windows.Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)delegate()
{
for(int cont = 0; cont < users.Length; ++cont)
{
_users.Add(new User(users[cont]));
}
});
}
//
// Invoked from the ChatRoomCallback implementation when a user joins the chat.
//
public void userJoinEvent(long timestamp, string name)
{
// Begin invoke is used to ensure that this code is executed in the UI thread.
System.Windows.Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)delegate()
{
_users.Add(new User(name));
if (_chatView != null)
{
_chatView.appendMessage(ChatUtils.formatTimestamp(timestamp) + " - <system-message> - " + name +
" joined." + Environment.NewLine);
}
});
}
//
// Invoked from the ChatRoomCallback implementation when a user leaves the chat.
//
public void userLeaveEvent(long timestamp, string name)
{
System.Windows.Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)delegate()
{
int index = _users.IndexOf(new User(name));
if(index >= 0)
{
_users.RemoveAt(index);
if(_chatView != null)
{
_chatView.appendMessage(ChatUtils.formatTimestamp(timestamp) + " - <system-message> - " +
name + " left." + Environment.NewLine);
}
}
});
}
//
// Invoked from the ChatRoomCallback implementation when a user sends a message
// to the chat.
//
public void userSayEvent(long timestamp, String name, String message)
{
System.Windows.Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)delegate()
{
if(_chatView != null)
{
_chatView.appendMessage(ChatUtils.formatTimestamp(timestamp) + " - <" + name + "> " +
ChatUtils.unstripHtml(message) + Environment.NewLine);
}
});
}
public void setChatView(ChatView chatView)
{
_chatView = chatView;
}
//
// Send a message using Ice AMI.
//
public void sendMessage(string message)
{
if(_session != null)
{
try
{
ChatUtils.validateMessage(message);
_session.proxy().send_async(new AMI_ChatSession_sendI(this, _username, message), message);
}
catch(Chat.InvalidMessageException ex)
{
appendMessage("<system-message> - " + ex.reason + Environment.NewLine);
}
}
}
public void setFocus()
{
if(_model.State == ClientState.Connected && _chatView != null)
{
_chatView.setFocusToInput();
}
}
private void loginDoWork(object sender, DoWorkEventArgs e)
{
LoginInfo info = (LoginInfo)e.Argument;
e.Result = info;
try
{
string routerEndpoins = "Glacier2/router:ssl -p 4064 -h " + info.Host + " -t 10000";
Ice.InitializationData initData = new Ice.InitializationData();
initData.properties = Ice.Util.createProperties();
initData.properties.setProperty("Ice.Plugin.IceSSL", "IceSSL:IceSSL.PluginFactory");
initData.properties.setProperty("IceSSL.TrustOnly.Client", "CN=Glacier2");
initData.properties.setProperty("Ice.Default.Router", routerEndpoins);
initData.properties.setProperty("Ice.ACM.Client", "0");
initData.properties.setProperty("Ice.RetryIntervals", "-1");
string[] args = (string[])_args.Clone();
_communicator = Ice.Util.initialize(ref args, initData);
//
// Read the CA certificate bundle with this executable and add it to
// CurrentUser Root store.
//
X509Certificate2 caCert = null;
X509Store store = new X509Store("Root", StoreLocation.CurrentUser);
try
{
store.Open(OpenFlags.ReadWrite);
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream stream = assembly.GetManifestResourceStream("ChatDemoGUI.zeroc_ca_cert.pem");
if(stream != null)
{
byte[] buf = new byte[stream.Length];
stream.Read(buf, 0, buf.Length);
stream.Close();
caCert = new X509Certificate2(buf);
store.Add(caCert);
}
}
finally
{
store.Close();
}
//
// Install a certificate verifier to ensure the server certificate is signed
// by the CA cert that is bundled with the executable.
//
IceSSL.Plugin plugin = (IceSSL.Plugin)_communicator.getPluginManager().getPlugin("IceSSL");
plugin.setCertificateVerifier(new CertificateVerifier(caCert));
Glacier2.RouterPrx router = Glacier2.RouterPrxHelper.uncheckedCast(_communicator.getDefaultRouter());
Glacier2.SessionPrx s = router.createSession(info.Username, info.Password);
Chat.ChatSessionPrx session = Chat.ChatSessionPrxHelper.uncheckedCast(s);
Ice.ObjectAdapter adapter = _communicator.createObjectAdapterWithRouter("ChatDemo.Client", router);
Ice.Identity callbackId = new Ice.Identity();
callbackId.name = Ice.Util.generateUUID();
callbackId.category = router.getCategoryForClient();
Chat.ChatRoomCallbackPrx callback = Chat.ChatRoomCallbackPrxHelper.uncheckedCast(
adapter.add(new ChatRoomCallbackI(this),callbackId));
session.setCallback(callback);
long sessionPingPeriod = router.getSessionTimeout() * 1000 / 2;
_session = new Session(session,sessionPingPeriod);
info.save();
_username = ChatUtils.formatUsername(info.Username);
}
catch(Glacier2.CannotCreateSessionException ex) { if(_communicator != null)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -