Exemplo n.º 1
0
        public async void ReadClientData(IAsyncResult ar)
        {
            try
            {
                if (!TcpClient.Connected)
                {
                    Disconnected();
                    return;
                }
                else
                {
                    int Recevied = NetStream.EndRead(ar);
                    if (Recevied > 0)
                    {
                        BytesRecevied += Recevied;
                        await ClientMS.WriteAsync(PacketBuffer, 0, Recevied);

                        if (!Identified && BytesRecevied >= 4)
                        {
                            int iRet = GetInteger();
                            Identified = true;
                            //One node notify to main UI
                            if (OnConnected != null)
                            {
                                OnConnected.Invoke(this);
                            }
                        }
                        else if (Identified)
                        {
                            Process();
                        }
                        NetStream.BeginRead(PacketBuffer, 0, PacketBuffer.Length, ReadClientData, null);
                    }
                    else
                    {
                        Disconnected();
                        return;
                    }
                }
            }
            catch
            {
                Disconnected();
                return;
            }
        }