Esempio n. 1
0
 private void tree_AfterSelect(object sender, TreeViewEventArgs e)
 {
     if (tree.SelectedNode.ImageKey == "folder" && sendPath != null)
     {
         sendPath.Invoke(tree.SelectedNode.FullPath);
         treeNode     = tree.SelectedNode;
         tree.Enabled = false;
     }
 }
Esempio n. 2
0
        private void button1_Click(object sender, EventArgs e)
        {
            string command = this.textBox1.Text;

            if (sendCommand != null)
            {
                sendCommand.Invoke(command);
            }
            receiveOuput(command);
        }
Esempio n. 3
0
        public void Start()
        {
            Listener.Bind("tcp://*:" + ListenPort, out var connectError);
            if (connectError != null)
            {
                Console.WriteLine($"Connection error: {connectError.Name} - {connectError.Number} - {connectError.Text}");
            }

            runningThread = new Thread(new ThreadStart(() =>
            {
                Running = true;
                while (Active)
                {
                    try
                    {
                        var message = Listener.ReceiveMessage(out var error);
                        if (error == null)
                        {
                            if (message != null && message.Count > 0)
                            {
                                try
                                {
                                    if (HandleMessage != null)
                                    {
                                        var responseMessage = HandleMessage?.Invoke(message[0].ReadString());
                                        using (ZMessage reponse = new ZMessage())
                                        {
                                            reponse.Add(new ZFrame(responseMessage));
                                            Listener.Send(reponse);
                                        }
                                    }
                                    else
                                    {
                                        Console.WriteLine("ZeroMQP2PListener: MessageHandler not defined");
                                    }
                                }
                                catch (Exception err)
                                {
                                    Console.WriteLine("Failed to Receive P2P Message -> " + err.ToString());
                                }
                            }
                        }
                    }
                    catch (Exception err)
                    {
                        Console.WriteLine("MessageListener failed -> " + err.ToString());
                    }

                    Thread.Sleep(10);
                }
                Running = false;
            }));
            runningThread.Start();
        }
        override protected void HandleReceivedMessage(Message message)
        {
            base.HandleReceivedMessage(message);

            Message response = null;

            switch (message.Type)
            {
            case MessageType.TRACE:
                _tracing2Client = true;
                HandleMessage?.Invoke(this, message);
                break;

            case MessageType.PING:
                response = CreatePingResponse(message);
                SendMessage(response);
                break;

            case MessageType.STATUS_REQUEST:
                response = CreateStatusResponse(message);
                SendMessage(response);
                break;

            case MessageType.SUBSCRIBE:
                if (message.HasValue("Subscriber"))
                {
                    try
                    {
                        AddSubscriber(ConnectionManager.Subscriber.Parse(message.GetString("Subscriber")));
                    }
                    catch (Exception e)
                    {
                        Tracing?.TraceEvent(TraceEventType.Error, 2000, e.Message);
                    }
                }
                break;

            case MessageType.UNSUBSCRIBE:
                if (message.HasValue("Subscriber"))
                {
                    RemoveSubscriber(message.GetString("Subscriber"));
                }
                break;

            default:
                int priorSize = ThreadExecutionManager.MaxQueueSize;
                ThreadExecutionManager.MaxQueueSize = 256;
                ThreadExecutionManager.Execute <Message>("HandleMessage-" + ID, HandleMessageDelegateWrapper, message);
                ThreadExecutionManager.MaxQueueSize = priorSize;
                break;
            }
        }
Esempio n. 5
0
        private void NewClientAccept(object oClientSocket)
        {
            Socket clientSocket = oClientSocket as Socket;

            try
            {
                UserToken userToken = UserToken.Pop();
                if (ClientInfosDic.TryAdd(clientSocket, userToken) && ClientSocketsDic.TryAdd(clientSocket.RemoteEndPoint + "", clientSocket))
                {
                    eShowMsg?.Invoke($"客户端上线,RemoteEndPoint:{clientSocket.RemoteEndPoint}");
                    userToken.socket          = clientSocket;
                    userToken.LastWorkingTime = DateTime.Now;
                    if (userToken.ReciveArgs == null)
                    {
                        userToken.ReciveArgs            = new SocketAsyncEventArgs();
                        userToken.ReciveArgs.Completed += ReciveArgs_Completed;
                        userToken.ReciveHandler.server  = this;
                        if (_HandlePackage == null)
                        {
                            _HandlePackage = new Action <TcpServerSocket, string, byte[]>((sender, remoteEndPoint, msg) =>
                            {
                                HandleMessage.Invoke(sender, remoteEndPoint, msg);
                            });
                        }
                        userToken.ReciveHandler.HandlePackage      = _HandlePackage;
                        userToken.ReciveHandler.dicSendedMessages  = dicSendedPackages;
                        userToken.ReciveHandler.dicRevivedMessages = dicRevivedPackages;
                        userToken.ReciveHandler.ClientSocketDic    = ClientInfosDic;
                        userToken.ReciveArgs.UserToken             = userToken;
                    }

                    userToken.ReciveHandler.clientSocket = clientSocket;
                    userToken.ReciveHandler.Start();
                    byte[] data = new byte[ReciveBufferSize];
                    userToken.ReciveArgs.SetBuffer(data, 0, data.Length);
                    userToken.ReciveArgs.AcceptSocket = clientSocket;
                    clientSocket.ReceiveAsync(userToken.ReciveArgs);
                }
                else
                {
                    DisposeClient(clientSocket, "加入客户端队列失败!");
                }
            }
            catch (Exception err)
            {
                DisposeClient(clientSocket, err.Message);
                OnError?.Invoke(this, err);
            }
        }
Esempio n. 6
0
        public void NotifyNewStep(int currentStep, string description, int stepsCount, int rank = 1)
        {
            var percent = 100 * currentStep * rank / stepsCount;

            HandleMessage?.Invoke(this, new JobStatusEventArgs(currentStep, description, percent, stepsCount));
        }
Esempio n. 7
0
        public void NotifyInitialStep(string description)
        {
            var percent = 1;

            HandleMessage?.Invoke(this, new JobStatusEventArgs(0, description, percent, 0));
        }
 private void HandleMessageDelegateWrapper(Message message)
 {
     HandleMessage?.Invoke(this, message);
 }
Esempio n. 9
0
        public void Run()
        {
            while (isRun)
            {
                try
                {
                    byte[] type = new byte[1];
                    client.Receive(type);
                    byte[] data = ReceiveVarData(client);

                    switch (type[0])
                    {
                    case 0:
                        string message = Encoding.UTF8.GetString(data);
                        if (transfer != null)
                        {
                            transfer.Invoke(message);
                        }
                        save2File(message);
                        break;

                    case 1:
                        MemoryStream ms  = new MemoryStream(data);
                        Image        bmp = Image.FromStream(ms);
                        if (setPicture != null)
                        {
                            setPicture.Invoke(bmp);
                        }
                        save2File(bmp);
                        break;

                    case 2:
                        string output = Encoding.UTF8.GetString(data);
                        if (setOutput != null)
                        {
                            setOutput.Invoke(output);
                        }
                        break;

                    case 3:
                        string dirName = Encoding.UTF8.GetString(data);
                        if (addDir != null)
                        {
                            addDir.Invoke(dirName);
                        }
                        break;

                    case 4:
                        string fileName = Encoding.UTF8.GetString(data);
                        if (addFile != null)
                        {
                            addFile.Invoke(fileName);
                        }
                        break;

                    case 9:
                        if (completeTransInfo != null)
                        {
                            completeTransInfo.Invoke();
                        }
                        break;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.StackTrace);
                    if (isRun)
                    {
                        if (closeConnect != null)
                        {
                            closeConnect.Invoke();
                        }
                        removeWorker(this);
                        isRun = false;
                    }
                }
            }
        }
Esempio n. 10
0
 public void LogMessage(string message)
 {
     HandleMessage?.Invoke(message);
 }