升级程序启动后,首先会连接服务器
private void Connect()
{
try
{
int port = int.Parse(System.Configuration.ConfigurationManager.AppSettings["Port"]);
connnectionInfo = new ConnectionInfo(IPAddress, port);
connection = TCPConnection.GetConnection(connnectionInfo);
}
catch (Exception)
{
throw new InvalidDataException("升级出现问题 .");
}
}
启动升级方法
private void StartUpdate()
{
//重新获取了一下服务器的升级信息
UpgradeConfig upgradeConifg = connection.SendReceiveObject<string, UpgradeConfig>("GetUpMsg", "ResUpMsg", 8000, "Test", customOptions, customOptions);
if (upgradeConifg.AppVersion > 0)
{
//在本地显示需要下载的文件列表
listBox1.DataSource = upgradeConifg.FileList;
textBox1.Text = upgradeConifg.AppVersion.ToString();
//需下载的文件的数量
upFilesCount = upgradeConifg.FileList.Count;
//发送信息请求下载升级文件
string filePath = AppDomain.CurrentDomain.BaseDirectory;
//请求服务器端发送需要升级的文件
//因为服务器端本身是知道需要发送哪些升级文件的,现在只需要此处发出一个信息,说服务器可以开始发送需升级文件了,我已经准备好接收了
//服务器收到逻辑类型为"GetUpFile"的信息后,会开始发送文件
//filePath 这个参数代表服务器升级文件传到客户端后,存放到客户端的位置。
//这样做的好处是,服务器端的文件发来后,本身就携带要存放的位置的信息,个人感觉这样比较方便
connection.SendObject<string>("GetUpFile", filePath, customOptions);
}
else
{
MessageBox.Show("不需要升级");
}
}
我们看一下服务器的处理方法
服务器端首先会在构造函数中注册一个GetUpFile逻辑类型
NetworkComms.AppendGlobalIncomingPacketHandler<string>("GetUpFile", IncomingReqUpMsg);
对应的IncomingReqUpMsg方法
// 客户端请求获取升级文件 的处理程序
//clientFilePath 是客户端传来的一个路径,此路径服务器端会回传客户端,此路径代表升级文件在客户端的存放位置
private void IncomingReqUpFile(PacketHeader header, Connection connection, string clientFilePath)
{
try
{
if (!File.Exists(UPConfigFilePath))
{
upgradeConfig = new UpgradeConfig();
upgradeConfig.Save(UPConfigFilePath);
}
else
{
//获取配置文件
upgradeConfig = UpgradeConfig.LoadConfig(UPConfigFilePath);
}
//从配置文件中,获取文件名列表,这个地方配置文件中只有文件名,因为服务器知道升级文件的存储位置
List<string> filenames = upgradeConfig.FileList;
for (int i = 0; i < filenames.Count; i++)
{
string serverFilePath = AppDomain.CurrentDomain.BaseDirectory + "UpgradeFile\\" + filenames[i].ToString();
//生成一个文件ID
//因为有的文件比较大,可能1个G,需要分块发送,fileID用于标志是否为同一个文件
string fileID = FileIDCreator.GetNextFileID(NetworkComms.NetworkIdentifier.ToString ());
//把文件交给文件发送器,进行发送//判断当前客户端连接的状态
if (connection.ConnectionInfo.ConnectionState == NetworkCommsDotNet.ConnectionState.Established)
{
sendFileDict.AddSendFile(fileID, serverFilePath, clientFilePath + filenames[i].ToString(), connection, fileOptions, sendFileParam);
}
}
}
catch (Exception ex)
{
LogTools.LogException(ex, "IncomingReqUpFile");
}
}
www.networkcomms.cn
www.cnblogs.com/networkcomms 编辑
[源码下载]Demo2.模拟简单登陆-效果图 基于networkcomms2.3.1
[源码下载]Demo1 客户端从服务器获取信息(基于networkcomms2.3.1)
相关文章: