/// <summary> /// /// </summary> /// <param name="netWorkStream"></param> /// <param name="data"></param> /// <returns></returns> protected override bool DataTransfer(NetworkStream netWorkStream, FileMSG data) { this.SendData(netWorkStream, data, "开始上传文件..."); Answer answer = this.ReciveAnswer(netWorkStream); if (answer != null) this.RaiseChanged(answer.Message); this.RaiseChanged(DateTime.Now + @"【与教师机客户端断开连接】"); return true; }
/// <summary> /// 上传文件。 /// </summary> /// <param name="data"></param> public bool Upload(FileMSG data) { bool result = false; if (data == null) return result; int dataSize = 0; if (this.maxDataSize > 0 && !this.ValidDataSize(this.maxDataSize, data, out dataSize)) { string msg = string.Format("数据过大({0}M,超过了规定传输的{1:N2}M)!", dataSize, dataSize - this.maxDataSize); MessageBox.Show(msg, "上传文件", MessageBoxButtons.OK, MessageBoxIcon.Information); return result; } try { using (TcpClient client = new TcpClient()) { if (!client.Connected) { client.Connect(this.host); this.RaiseChanged(string.Format("{0}【与{1}:{2}建立连接...】", DateTime.Now, this.host.Address, this.host.Port)); } data.UIP = string.Format("{0}", this.host.Address); NetworkStream stream = client.GetStream(); if (stream != null) { try { result = this.DataTransfer(stream, data); } catch (Exception e) { this.SendAnswer(stream, MSGKind.UploadFile, "上传文件异常:" + e.Message); throw e; } finally { client.Close(); } } } } catch (SocketException e) { this.RaiseChanged(string.Format("{0}【发生网络故障,{1}】", DateTime.Now, e.SocketErrorCode)); this.OnExceptionRecord(e, typeof(TcpClientService)); } catch (Exception e) { this.OnExceptionRecord(e, typeof(TcpClientService)); if (this.uploadFileMaxCount-- > 0) { result = this.Upload(data); } } return result; }
/// <summary> /// /// </summary> /// <param name="netWorkStream"></param> /// <param name="data"></param> /// <returns></returns> protected override bool DataTransfer(NetworkStream netWorkStream, FileMSG data) { IssueWorkFile file = data as IssueWorkFile; this.RaiseChanged(string.Format("{0}【准备下发文件{1}...】", DateTime.Now, file.WorkName)); this.SendData(netWorkStream, file, string.Format("开始下发文件{0}...", file.WorkName)); this.RaiseChanged(string.Format("{0}【下发文件{1}完毕!】", DateTime.Now, file.WorkName)); Answer answer = this.ReciveAnswer(netWorkStream); if (answer != null && answer.SourceKind == MSGKind.IssueWorkFile) { this.RaiseChanged(answer.Message); } this.RaiseChanged(DateTime.Now + @"【将与学生机客户端断开连接】"); return true; }
/// <summary> /// /// </summary> /// <param name="maxDataSize"></param> /// <param name="data"></param> /// <param name="dataSize"></param> /// <returns></returns> protected override bool ValidDataSize(int maxDataSize, FileMSG data, out int dataSize) { dataSize = 0; UploadFileMSG file = data as UploadFileMSG; if (file != null && file.Files != null && file.Files.Count > 0) { foreach (StudentWorkFile swf in file.Files) { dataSize += (int)swf.Size; } dataSize = dataSize / 1024 / 1024; return maxDataSize - dataSize > 0; } return true; }
/// <summary> /// 验证最大数据大小。 /// </summary> /// <param name="maxDataSize"></param> /// <param name="data"></param> /// <param name="dataSize"></param> /// <returns></returns> protected abstract bool ValidDataSize(int maxDataSize, FileMSG data, out int dataSize);
/// <summary> /// 数据传输。 /// </summary> /// <param name="netWorkStream"></param> /// <param name="data"></param> protected abstract bool DataTransfer(NetworkStream netWorkStream, FileMSG data);
/// <summary> /// /// </summary> /// <param name="maxDataSize"></param> /// <param name="data"></param> /// <param name="dataSize"></param> /// <returns></returns> protected override bool ValidDataSize(int maxDataSize, FileMSG data, out int dataSize) { dataSize = 0; return true; }
/// <summary> /// /// </summary> /// <param name="data"></param> protected void OnDataArrival(FileMSG data) { SocketDataArrivalEventHandler<FileMSG> handler = this.DataArrival; if (handler != null && data != null) handler(data); }
/// <summary> /// /// </summary> /// <param name="data"></param> private void ReceiveIssueWorkFile(FileMSG data) { try { IssueWorkFile file = data as IssueWorkFile; if (file != null) { lock (this) { string hostIP = string.Format("{0}", this["host_ip"]); Student stu = this["student"] as Student; if ((hostIP == file.UIP) && (stu != null && stu.StudentID == file.StudentID) && (file.Data != null)) { string path = this.CreateFilePath(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), file.WorkName, string.Empty, ".zip", 1); using (FileStream stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write)) { byte[] buf = file.Data; stream.Write(buf, 0, buf.Length); } } } } } catch (Exception x) { MessageBox.Show(x.Message, "接收下发数据时发生异常"); Program.GlobalExceptionHandler(x); } }