示例#1
0
 protected void VerifyCode(SyncCode actual, SyncCode expected, SyncReader reader, SyncWriter writer)
 {
     if (actual == SyncCode.Error)
     {
         String msg = reader.ReadString();
         throw new SyncException(String.Format("Peer reported error: {0}", msg));
     }
     else if (actual != expected)
     {
         String msg = String.Format("Unexpected code {0} when looking for {1}", actual, expected);
         writer.Write(SyncCode.Error);
         writer.Write(msg);
         throw new SyncException(msg);
     }
 }
示例#2
0
        protected override void MainCore()
        {
            IPAddress ip = GetServerIP();
            using (TcpClient client = new TcpClient())
            {
                client.Connect(ip, PORT);
                using (Stream stream = client.GetStream())
                {
                    using (SyncReader reader = new SyncReader(stream))
                    {
                        using (SyncWriter writer = new SyncWriter(stream))
                        {
                            SyncCode code = SyncCode.Error;

                            // 1. Handshake
                            writer.Write(SyncCode.Handshake);
                            writer.Write(HANDSHAKE);
                            code = reader.ReadCode();
                            VerifyCode(code, SyncCode.Ok, reader, writer);

                            // 2. Files
                            foreach (String file in Directory.GetFiles(_folder, _mask))
                            {
                                writer.Write(SyncCode.File);
                                writer.Write(file);

                                code = reader.ReadCode();
                                if (code == SyncCode.Cancel)
                                    continue;

                                VerifyCode(code, SyncCode.Ok, reader, writer);
                            }

                            writer.Write(SyncCode.Cancel);
                        }
                    }
                }
            }
        }
示例#3
0
        protected override void MainCore()
        {
            TcpListener listener = new TcpListener(IPAddress.Any, PORT);
            listener.Start();
            using (TcpClient client = listener.AcceptTcpClient())
            {
                using (Stream stream = client.GetStream())
                {
                    using (SyncReader reader = new SyncReader(stream))
                    {
                        using (SyncWriter writer = new SyncWriter(stream))
                        {
                            SyncCode code = SyncCode.Error;

                            code = reader.ReadCode();
                            VerifyCode(code, SyncCode.Handshake, reader, writer);
                            String handshake = reader.ReadString();
                            VerifyHandshake(handshake, HANDSHAKE, writer);

                            writer.Write(SyncCode.Ok);

                            while (true)
                            {
                                code = reader.ReadCode();
                                if (code == SyncCode.Cancel)
                                    break;

                                VerifyCode(code, SyncCode.File, reader, writer);
                                String file = reader.ReadString();
                                writer.Write(SyncCode.Cancel);
                            }
                        }
                    }
                }
            }
        }