Ejemplo n.º 1
0
        public async void Start()
        {
            if (!CanStart())
            {
                return;
            }

            WrapperProcess = new Process();

            var processStartInfo = new ProcessStartInfo(ExecutablePath);

            processStartInfo.UseShellExecute        = false;
            processStartInfo.RedirectStandardOutput = true;
            processStartInfo.RedirectStandardInput  = true;
            processStartInfo.RedirectStandardError  = true;
            processStartInfo.CreateNoWindow         = true;
            WrapperProcess.StartInfo   = processStartInfo;
            processStartInfo.Arguments = "--config-file configs/BloodDonationCoin.conf";
            WrapperProcess.Start();

            await Task.Factory.StartNew(() => ReadNextLine(false));

            await Task.Factory.StartNew(() => ReadNextLine(true));

            PingTimer.Start();
            ConnectionTimer.Start();
        }
Ejemplo n.º 2
0
        private void ConnectComplete(IAsyncResult result)
        {
            Socket.EndConnect(result);

            NetworkStream = new NetworkStream(Socket);
            if (UseSSL)
            {
                NetworkStream = new SslStream(NetworkStream, false, new RemoteCertificateValidationCallback(CheckCertificate));
                try
                {
                    ((SslStream)NetworkStream).AuthenticateAsClient(ServerHostname);
                }
                catch (Exception)
                {
                    NetworkStream.Dispose();
                    Socket.Dispose();
                    if (OnDisconnected != null)
                    {
                        OnDisconnected(this, new EventArgs());
                    }
                    return;
                }
            }

            Read();

            if (!string.IsNullOrEmpty(User.Password))
            {
                SendRawMessage("PASS {0}", User.Password);
            }
            SendRawMessage("NICK {0}", User.Nick);
            // hostname, servername are ignored by most IRC servers
            SendRawMessage("USER {0} hostname servername :{1}", User.User, User.RealName);
            PingTimer.Start();
        }
Ejemplo n.º 3
0
        private void ConnectComplete(IAsyncResult result)
        {
            Socket.EndConnect(result);

            NetworkStream = new NetworkStream(Socket);
            if (UseSSL)
            {
                if (IgnoreInvalidSSL)
                {
                    NetworkStream = new SslStream(NetworkStream, false, (sender, certificate, chain, policyErrors) => true);
                }
                else
                {
                    NetworkStream = new SslStream(NetworkStream);
                }
                ((SslStream)NetworkStream).AuthenticateAsClient(ServerHostname);
            }

            NetworkStream.BeginRead(ReadBuffer, ReadBufferIndex, ReadBuffer.Length, DataRecieved, null);
            // Write login info
            if (!string.IsNullOrEmpty(User.Password))
            {
                SendRawMessage("PASS {0}", User.Password);
            }
            SendRawMessage("NICK {0}", User.Nick);
            // hostname, servername are ignored by most IRC servers
            SendRawMessage("USER {0} hostname servername :{1}", User.User, User.RealName);
            PingTimer.Start();
        }
Ejemplo n.º 4
0
        private void catchbtn_Click(object sender, EventArgs e)
        {
            PingTimer.Stop();
            Catch catch1 = new Catch(plcIPAddressBox.IPAddress, DiscoverPLC);

            catch1.ShowDialog();
            //DiscoverPLC.DiscoverPLCs();
            PingTimer.Start();
        }
Ejemplo n.º 5
0
 private void ConnectComplete(IAsyncResult result)
 {
     Socket.EndConnect(result);
     Socket.BeginReceive(ReadBuffer, ReadBufferIndex, ReadBuffer.Length, SocketFlags.None, DataRecieved, null);
     // Write login info
     if (!string.IsNullOrEmpty(User.Password))
     {
         SendRawMessage("PASS {0}", User.Password);
     }
     SendRawMessage("NICK {0}", User.Nick);
     // hostname, servername are ignored by most IRC servers
     SendRawMessage("USER {0} hostname servername :{1}", User.User, User.RealName);
     PingTimer.Start();
 }
Ejemplo n.º 6
0
        private void PingNode(SwimNode node)
        {
            //  Ping node directly

            PingCorrelationId = Ulid.NewUlid();

            Debug.WriteLine($"Pinging node {node.Endpoint}.");
            Logger.LogInformation($"Pinging node {node.Endpoint}.");

            ProtocolProvider.SendMessage(node, new PingMessage(PingCorrelationId.Value)
            {
                SourceNode = ProtocolProvider.Node
            });

            PingTimer.Start();
        }
Ejemplo n.º 7
0
        private void ConnectComplete(IAsyncResult result)
        {
            if (this.Connected)
            {
                SocketError socketError = SocketError.Success;
                try
                {
                    Socket.EndConnect(result);

                    NetworkStream = new NetworkStream(Socket);
                    if (UseSSL)
                    {
                        if (IgnoreInvalidSSL)
                        {
                            NetworkStream = new SslStream(NetworkStream, false, (sender, certificate, chain, policyErrors) => true);
                        }
                        else
                        {
                            NetworkStream = new SslStream(NetworkStream);
                        }
                        (( SslStream )NetworkStream).AuthenticateAsClient(ServerHostname);
                    }

                    NetworkStream.BeginRead(ReadBuffer, ReadBufferIndex, ReadBuffer.Length, DataRecieved, null);
                }
                catch (Exception ex)
                {
                    if (this.SocketConnectionError == null)
                    {
                        throw ex;
                    }
                    this.OnSocketConnectionError(new SocketUnhandledExceptionEventArgs(ex));
                    return;
                }
                // Write login info
                if (!string.IsNullOrEmpty(User.Password))
                {
                    SendRawMessage("PASS {0}", User.Password);
                }
                SendRawMessage("NICK {0}", User.Nick);
                // hostname, servername are ignored by most IRC servers
                SendRawMessage("USER {0} hostname servername :{1}", User.User, User.RealName);
                PingTimer.Start();
            }
            this.ConnectCompleted = true;
        }
Ejemplo n.º 8
0
        private void ConnectComplete(IAsyncResult result)
        {
            try
            {
                Socket.EndConnect(result);
                NetworkStream = new NetworkStream(Socket);
                if (UseSSL)
                {
                    if (IgnoreInvalidSSL)
                    {
                        NetworkStream = new SslStream(NetworkStream, false, (sender, certificate, chain, policyErrors) => true);
                    }
                    else
                    {
                        NetworkStream = new SslStream(NetworkStream);
                    }
                    ((SslStream)NetworkStream).AuthenticateAsClient(ServerHostname);
                }

                NetworkStream.BeginRead(ReadBuffer, ReadBufferIndex, ReadBuffer.Length, DataRecieved, null);
                // Begin capability negotiation
                //SendRawMessage("CAP LS 302");
                // Write login info
                if (!string.IsNullOrEmpty(User.Password))
                {
                    SendRawMessage("PASS {0}", User.Password);
                }
                //SendRawMessage("NICK {0}", User.Nick);
                SendRawMessage($"USER MO.{Program.ID} 0 * :3.3.4 MO CnCNet");
                // hostname, servername are ignored by most IRC servers
                SendRawMessage($"NICK {Program.Name}");
                //SendRawMessage("USER {0} hostname servername :{1}", User.User, User.RealName);
                PingTimer.Start();
            }
            catch (SocketException e)
            {
                OnNetworkError(new SocketErrorEventArgs(e.SocketErrorCode));
            }
            catch (Exception e)
            {
                OnError(new Events.ErrorEventArgs(e));
            }
        }
Ejemplo n.º 9
0
        private void FindPLCs()
        {
            Thread t = new Thread(delegate()
            {
                this.BeginInvoke(new Action(delegate()
                {
                    PingTimer.Stop();
                    changeCursor(Cursors.WaitCursor);
                    EnableButtons(false);
                    DiscoverPLC.PLCbs.Clear();
                    DiscoverPLC.DiscoverPLCs();
                    changeCursor(Cursors.Default);
                    EnableButtons(true);
                    DiscoverPLC.PLCbs.ResetBindings(false);
                    PLCGrid.ClearSelection();
                    PingTimer.Start();
                }));
                lastSelectedRow = -1;
            });

            t.Start();
            this.ActiveControl = Discoverbtn;
            this.ActiveControl = null;
        }
Ejemplo n.º 10
0
        /// <summary>
        /// 打开连接
        /// </summary>
        /// <param name="host">服务器地址</param>
        /// <param name="port">服务器端口号</param>
        /// <param name="userType">用户类型</param>
        /// <param name="userName">用户显示的名称</param>
        /// <param name="userAgent">用户登陆的终端信息</param>
        /// <param name="accessToken">访问令牌</param>
        /// <param name="lastTick">收到最后一条消息的UNIX时间戳</param>
        /// <param name="callback">回调函数</param>
        /// <returns>连接是否成功</returns>
        public bool Connect(string host, int port, string userType, string userName, string userAgent, string accessToken, int lastTick, Action <RequestInfo, ResponseAckInfo> callback)
        {
            var requestInfo = new Message.RequestInfo()
            {
                MsgType = MessageType.Connect,
                //MessageId = TimeStamp.Create(),
                Data = new
                {
                    userType    = userType,
                    userName    = userName,
                    userAgent   = userAgent,
                    accessToken = accessToken,
                    lastTick    = lastTick
                },
                Version = Encrypt_Version
            };
            var preRequestInfo = new Message.RequestInfo()
            {
                MsgType = MessageType.Connect,
                Data    = requestInfo.Data,
                Version = Encrypt_Version
            };

            if (IsAvailable)
            {
                //已经连接成功
                if (callback != null)
                {
                    callback(requestInfo, new ResponseAckInfo()
                    {
                        MsgType = MessageType.Ack, Status = ResponseCode.OK
                    });
                }
                return(true);
            }
            bool result = false;

            //开始连接
            try
            {
                m_Client = new Socket(SocketType.Stream, ProtocolType.Tcp);

                if (EnableEncrypt)
                {
                    requestInfo.Data = EncryptAdapter.Encode(CommonExt.DynamicToJsonString(requestInfo.Data), requestInfo);
                }

                LogOpInfo("BeginConnect:", requestInfo.ToJsonString());
                m_Client.Connect(host, port);
                m_IpAddress = host;
                m_Port      = port;
                if (m_Client.Connected)
                {
                    byte[] sendData = requestInfo.ToByte <Message.RequestInfo>();

                    m_Client.Send(sendData);
                    byte[] bufferData   = new byte[BufferSize];
                    int    receiveLen   = m_Client.Receive(bufferData);
                    byte[] receivedData = bufferData.ToList().GetRange(0, receiveLen).ToArray();
                    var    responseInfo = receivedData.ToObject <Message.ResponseAckInfo>();
                    if (EnableEncrypt)
                    {
                        dynamic deString = CommonCryptoService.Decrypt_DES(responseInfo.Data.ToString(), responseInfo.MessageId);
                        responseInfo.Data = CommonExt.JsonStringToDynamic(deString.ToString());
                    }
                    //LogOpInfo("ConnectOver:", responseInfo.ToJsonString());
                    if (responseInfo.Status == ResponseCode.OK)
                    {
                        m_User.IsAuthenticated    = true;
                        m_User.Name               = userName;
                        m_User.Token              = accessToken;
                        m_User.UserAgent          = userAgent;
                        m_User.UserType           = userType;
                        m_User.AuthenticationType = responseInfo.Data.userPermission;
                        m_User.PermissionList     = Permission.GetUserPermission(m_User.AuthenticationType);
                        //连接使用同步
                        // _sendBuffer.Add(requestInfo);
                        m_LastPingTime = DateTime.Now;

                        //启动线程接收
                        AfterConnectedServer();
                        result            = true;
                        m_IsCanConnecting = true;

                        if (this.clientPingMode == PingMode.Initiative)
                        {
                            _lastSendTime = DateTime.Now;
                            PingTimer.Start();
                        }
                    }
                    else
                    {
                        m_Client.Disconnect(true);
                    }

                    RunUserCallback(callback, preRequestInfo, responseInfo);
                }
            }
            catch (Exception ex)
            {
                LogInfo("ConnectError", ex);
                RunUserCallback(callback, preRequestInfo, new ResponseAckInfo()
                {
                    MsgType = MessageType.Ack, Status = ResponseCode.CLINET_ERR, Data = ex.Message
                });
            }

            return(result);
        }