Exemple #1
0
        public File Load(string path)
        {
            // 初始化
            p     = 0;
            index = new List <long>();

            fs = new FileStream(path, FileMode.Open, FileAccess.Read);

            byte[] bytes = new byte[header.GetSize()];
            fs.Read(bytes, 0, bytes.Length);
            header = (Header)StructBytes.BytesToStructure(bytes, typeof(Header));

            bytes = new byte[header.listenPointsAsBytesCount];
            fs.Read(bytes, 0, bytes.Length);
            listenPoints = new List <IPEndPoint>();
            for (int i = 0; i < header.listenPointsAsBytesCount; i += (4 + 2))
            {
                ReadOnlySpan <byte> ipb = new ReadOnlySpan <byte>(bytes, i, 4);
                listenPoints.Add(new IPEndPoint(new IPAddress(ipb), (int)BitConverter.ToUInt16(bytes, i + 4)));
                //listenPoints.Add(new IPandPort(bytes[i + 0] + "." + bytes[i + 1] + "." + bytes[i + 2] + "." + bytes[i + 3], BitConverter.ToUInt16(bytes, i + 4)));
            }

            bytes = new byte[header.notesAsBytesCount];
            fs.Read(bytes, 0, bytes.Length);
            notes = Encoding.UTF8.GetString(bytes);

            // 建立文件内索引
            bytes = new byte[4];
            while (fs.Position < fs.Length)
            {
                index.Add(fs.Position);

                fs.Read(bytes, 0, 4);
                var ol = BitConverter.ToInt32(bytes);
                fs.Read(bytes, 0, 4);
                var cl = BitConverter.ToInt32(bytes);

                fs.Seek(cl, SeekOrigin.Current);
            }

            // 返回文件数据开头
            fs.Seek(header.fileInfoLength, SeekOrigin.Begin);

            return(this);
        }
Exemple #2
0
        public void CheckWorkstations()
        {
            int n = 0;

            Thread.Sleep(5000);
            MessagePacket messagepack;

            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            IPAddress broadcast = IPAddress.Parse("192.168.1.255");


            IPEndPoint ep = new IPEndPoint(broadcast, 11000);

            // s.SendTo(sendbuf, ep);



            StructBytes sf = new StructBytes(typeof(MessagePacket));

            try
            {
                while (true)
                {
                    foreach (tblController tblcontroller in tblSolution.m_tblSolution().m_tblControllerCollection)
                    {
                        messagepack.Index     = 1;
                        messagepack.res       = 1;
                        messagepack.CAT       = 1;
                        messagepack.ID        = 1;
                        messagepack.StationNo = (byte)tblcontroller.NodeNumber;
                        //udpServer.Send(sf.StructToByteArray(messagepack), sf.SizeofStructure(messagepack));
                        n = s.SendTo(sf.StructToByteArray(messagepack), ep);
                        //udpServer.Send(new byte[] { 1 }, 1);
                        Thread.Sleep(100);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Exemple #3
0
        public File Create()
        {
            // 初始化
            p     = 0;
            index = new List <long>();

            // FileMode.Create : 如果存在同名文件则覆盖
            fs = new FileStream(pathWithName + "_" + partNum + ".lcl", FileMode.Create, FileAccess.Write);

            // 跳过 header 部分
            // 因为部分数据需要之后才能确定
            fs.Seek(header.GetSize(), SeekOrigin.Begin);

            // 写入 listenPoints
            foreach (var point in listenPoints)
            {
                fs.Write(point.Address.GetAddressBytes(), 0, 4);
                fs.Write(BitConverter.GetBytes((ushort)point.Port), 0, 2);
            }

            // 为 header 中部分数据赋值
            header.listenPointsAsBytesCount = listenPoints.Count * (4 + 2);

            // 写入 notes
            var bytes = Encoding.UTF8.GetBytes(notes, 0, notes.Length);

            fs.Write(bytes, 0, bytes.Length);

            // 为 header 中部分数据赋值
            header.notesAsBytesCount = bytes.Length;
            header.fileInfoLength    = fs.Position;

            // 回到文件开头
            fs.Seek(0, SeekOrigin.Begin);
            fs.Write(StructBytes.ConvertStructToBytes(header), 0, header.GetSize());

            // 返回文件数据开头
            fs.Seek(header.fileInfoLength, SeekOrigin.Begin);

            return(this);
        }
Exemple #4
0
        public void ReadData()
        {
            try
            {
                IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, BroadcastPortNo);
                //udpServer.Connect(remoteEP);
                while (true)
                {
                    Trace.Write("receive data from " + remoteEP.ToString());



                    StructBytes sf   = new StructBytes(typeof(MessagePacket));
                    var         data = udpSocket.Receive(ref remoteEP); // listen on port 11000


                    //udpServer.Send(new byte[] { 1 }, 1, remoteEP); // reply back
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }