Ejemplo n.º 1
0
        public void ReadCallback(IAsyncResult ar)
        {
            if (modeNetwork == Mode.indeterminately)
            {
                return;
            }

            try
            {
                NodeData      node = (NodeData)ar.AsyncState;
                NetworkStream ns   = node.newNode.GetStream();

                int r = ns.EndRead(ar);

                if (r > 0)
                {
                    string header  = Encoding.Default.GetString(node.buffer);
                    int    leninfo = int.Parse(header);

                    MemoryStream ms   = new MemoryStream(leninfo);
                    byte[]       temp = new byte[leninfo];
                    r = ns.Read(temp, 0, temp.Length);
                    ms.Write(temp, 0, r);
                    XmlSerializer formatter = new XmlSerializer(typeof(MessageData));
                    ms.Position = 0;
                    MessageData sc = (MessageData)formatter.Deserialize(ms);
                    ms.Close();

                    if (sc.fileSize > 0)
                    {
                        if (sc.text == "nd_res")
                        {
                            sc.fileName += ".dd";
                        }
                        FileStream fs = new FileStream(sc.fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite, sc.fileSize);
                        do
                        {
                            if (sc.type == MessageData.TypeData.CODE)
                            {
                                Global.nameOfBasicFiles[0] = sc.fileName;
                            }
                            else
                            {
                                Global.nameOfBasicFiles[1] = sc.fileName;
                            }
                            temp = new byte[ConstField.BUFFER];
                            r    = ns.Read(temp, 0, temp.Length);

                            fs.Write(temp, 0, r);

                            if (fs.Length == sc.fileSize)
                            {
                                fs.Close();
                                fs = null;
                                break;
                            }
                        }while (r > 0);

                        node.isBusy = false;
                        temp        = null;
                        GC.Collect();
                        GC.WaitForPendingFinalizers();
                    }
                    else
                    {
                        if (sc.text[0] == 'i')
                        {
                            string[] bufArr = sc.text.Split(',');
                            NewNode.BeginInvoke(bufArr[1], Convert.ToInt32(bufArr[2]), null, null); ///NEW
                        }
                        else if (sc.text[0] == 'd')
                        {
                            string[] bufArr = sc.text.Split(',');
                            DeleteNodeItem.BeginInvoke(Convert.ToInt32(bufArr[1]), null, null);
                        }
                        else if (sc.text == "client")
                        {
                            _client          = Global.nodes.Last();
                            _client.typeNode = NodeData.TypeNode.CLIENT;
                            Global.nodes.Remove(Global.nodes.Last());
                        }
                        else if (sc.text[0] == 'n')
                        {
                            string[] bufArr = sc.text.Split(',');
                            bufNodes = Convert.ToInt32(bufArr[1]);
                            All.BeginInvoke(Convert.ToInt32(bufArr[1]), null, null);
                        }
                    }

                    if (Receive != null)
                    {
                        Receive(this, new ReceiveEventArgs(sc));
                    }

                    node.buffer = new byte[ConstField.LENGTHHEADER];
                    ns.BeginRead(node.buffer, 0, node.buffer.Length, new AsyncCallback(ReadCallback), node);


                    if (sc.text == "nd_res")
                    {
                        var dirInfo = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.dd");
                        if (dirInfo.Length == bufNodes)
                        {
                            GetFinalResult.BeginInvoke(null, null);
                        }
                    }

                    if (modeNetwork == Mode.Client)
                    {
                        count--;
                        if (count <= 0)
                        {
                            All.BeginInvoke(this, null, null);
                            Global.count++;
                        }
                    }
                }
                else
                {
                    string buf = node.identification.ToString();
                    DeleteNode(node);
                    SendData($"d,{bufID}");

                    if (Disconnected != null)
                    {
                        Disconnected.BeginInvoke(this, "Узел отключился!", bufID.ToString(), null, null);
                    }
                }
            }
            catch (Exception e)
            {
                if (Disconnected != null)
                {
                    Disconnected.BeginInvoke(this, "Узел отключился аварийно!", "", null, null);
                }

                globalState = "ReadCallback | " + e;
            }
        }