public void Send(IPEndPoint des, CDNMessage msg) { try { using (TcpClient client = new TcpClient()) { client.Connect(des); if (client.Connected) { NetworkStream ns = client.GetStream(); using (StreamWriter sw = new StreamWriter(ns)) { CDNMessage newMsg = msg.Clone() as CDNMessage; newMsg.from = this.type; newMsg.AddressUpdate(this); sw.Write(Serializer <CDNMessage> .Serialize <SoapFormatter>(newMsg)); } } } } catch (Exception exp) { Console.WriteLine("Fail to send" + msg.ToString() + exp.Message); } }
private void refreshButton_Click(object sender, EventArgs e) { CDNMessage msg = new CDNMessage(); msg.Fill(CDNMessage.MSGID.SHOW); server.Send(remoteIpAddressControl.Value, msg); }
private void serverFile_DbClick(object sender, EventArgs e) { if (serverTreeView.SelectedNode is FileNode) { CDNMessage msg = new CDNMessage(); msg.Fill(CDNMessage.MSGID.DOWNLOAD, Serializer <FileNode> .Serialize <SoapFormatter>(serverTreeView.SelectedNode as FileNode)); server.Send(remoteIpAddressControl.Value, msg); } }
public object Clone() { CDNMessage msg = new CDNMessage(); msg.id = this.id; msg.content = this.content; msg.client = this.client; msg.cache = this.cache; msg.server = this.server; msg.from = this.from; msg.to = this.to; return(msg); }
protected override async Task Handle(CDNMessage msg) { try { switch (msg.id) { case CDNMessage.MSGID.TEST: { MessageBox.Show("Successfully connected!"); } break; case CDNMessage.MSGID.SHOW: { remoteRoot = Serializer <DirectoryNode> .Deserialize <SoapFormatter>(msg.content); TreeView v = (ui as CDNClientForm).serverTreeView; v.Nodes.Clear(); v.Nodes.Add(remoteRoot); } break; case CDNMessage.MSGID.DOWNLOAD: { String fileName = msg.content.Substring(0, msg.content.IndexOf("|||")); String fileContent = msg.content.Substring(msg.content.IndexOf("|||") + 3); using (FileStream fs = new FileStream(localRoot.info.FullName + "\\" + fileName, FileMode.Create, FileAccess.Write)) { using (StreamWriter sw = new StreamWriter(fs)) { sw.Write(fileContent); } } localRoot.Scan(); TreeView v = (ui as CDNClientForm).clientTreeView; v.Nodes.Clear(); v.Nodes.Add(localRoot); } break; default: break; } } catch (Exception e) { Console.WriteLine(e); } }
private void HandleClient(CDNMessage msg) { msg.to = CNDTYPE.SERVER; switch (msg.id) { case CDNMessage.MSGID.TEST: { Send(new IPEndPoint(IPAddress.Parse(msg.From().address), msg.From().port), msg); } break; case CDNMessage.MSGID.SHOW: { Send((ui as CDNCacheForm).remoteIpAddressControl.Value, msg); } break; case CDNMessage.MSGID.DOWNLOAD: { FileNode node = Serializer <FileNode> .Deserialize <SoapFormatter>(msg.content); (ui as CDNCacheForm).logListBox.Items.Add("User request: file " + node.info.Name + " at " + DateTime.Now); FileNode localNode = localRoot.Find(node.Text) as FileNode; if (localNode != null) { using (FileStream fs = File.OpenRead(localNode.info.FullName)) { using (StreamReader sr = new StreamReader(fs)) { msg.content = localNode.info.Name + "|||" + sr.ReadToEnd(); Send(new IPEndPoint(IPAddress.Parse(msg.From().address), msg.From().port), msg); } } (ui as CDNCacheForm).logListBox.Items.Add("Response: cached file " + localNode.info.Name); } else { Send((ui as CDNCacheForm).remoteIpAddressControl.Value, msg); (ui as CDNCacheForm).logListBox.Items.Add("Response: file " + node.info.Name + "downloaded from the server"); } } break; default: break; } }
public async Task Idle() { while (true) { TcpClient client = await AcceptTcpClientAsync(); NetworkStream ns = client.GetStream(); using (StreamReader sr = new StreamReader(ns)) { String context = await sr.ReadToEndAsync(); CDNMessage msg = Serializer <CDNMessage> .Deserialize <SoapFormatter>(context); msg.to = this.type; msg.AddressUpdate(this); Handle(msg); } } }
private void HandleServer(CDNMessage msg) { msg.to = CNDTYPE.CLIENT; switch (msg.id) { case CDNMessage.MSGID.TEST: { MessageBox.Show("Successfully connected!"); } break; case CDNMessage.MSGID.SHOW: { Send(new IPEndPoint(IPAddress.Parse(msg.To().address), msg.To().port), msg); } break; case CDNMessage.MSGID.DOWNLOAD: { String fileName = msg.content.Substring(0, msg.content.IndexOf("|||")); String fileContent = msg.content.Substring(msg.content.IndexOf("|||") + 3); using (FileStream fs = new FileStream(localRoot.info.FullName + "\\" + fileName, FileMode.Create, FileAccess.Write)) { using (StreamWriter sw = new StreamWriter(fs)) { sw.Write(fileContent); } } localRoot.Scan(); TreeView v = (ui as CDNCacheForm).clientTreeView; v.Nodes.Clear(); v.Nodes.Add(localRoot); Send(new IPEndPoint(IPAddress.Parse(msg.To().address), msg.To().port), msg); } break; default: break; } }
protected override async Task Handle(CDNMessage msg) { try { //This message comes from the Server if (msg.from == CNDTYPE.SERVER) { HandleServer(msg); } //This message comes from the Client else if (msg.from == CNDTYPE.CLIENT) { HandleClient(msg); } else { throw new Exception("Wrong msg route!"); } } catch (Exception e) { Console.WriteLine(e); } }
protected override async Task Handle(CDNMessage msg) { try { String content = ""; switch (msg.id) { case CDNMessage.MSGID.TEST: { } break; case CDNMessage.MSGID.SHOW: { localRoot.Scan(); content = Serializer <DirectoryNode> .Serialize <SoapFormatter>(localRoot); } break; case CDNMessage.MSGID.DOWNLOAD: { FileNode node = Serializer <FileNode> .Deserialize <SoapFormatter>(msg.content); FileNode localNode = localRoot.Find(node.Text) as FileNode; if (localNode != null) { using (FileStream fs = File.OpenRead(localNode.info.FullName)) { using (StreamReader sr = new StreamReader(fs)) { msg.content = localNode.info.Name + "|||" + sr.ReadToEnd(); Send(new IPEndPoint(IPAddress.Parse(msg.From().address), msg.From().port), msg); } } } else { Console.WriteLine("No such file!"); } using (FileStream fs = File.OpenRead(node.info.FullName)) { using (StreamReader sr = new StreamReader(fs)) { content = node.info.Name + "|||"; content += sr.ReadToEnd(); } } } break; default: break; } CDNMessage newMsg = msg.Clone() as CDNMessage; newMsg.Fill(msg.id, content); Send(new IPEndPoint(IPAddress.Parse(msg.From().address), msg.From().port), newMsg); } catch (Exception e) { Console.WriteLine(e); } }
protected virtual async Task Handle(CDNMessage msg) { }