private bool ConnectToServer(Socket sendSocket, int id)
        {
            IPEndPoint rpcServerPoint = null;

            if (CreateAddress(ref rpcServerPoint, id) == false)
            {
                LogNotify?.Invoke($"Client error: Connection {id}: Cannot connect to server. Invalid server address.");
                return(false);
            }

            LogNotify?.Invoke($"Client: Connection {id}: Server address created.");

            try
            {
                sendSocket.Connect(rpcServerPoint);
            }
            catch (SocketException)
            {
                LogNotify?.Invoke($"Client error: Connection {id}: Cannot connect to server. Socket unavailable.");
                return(false);
            }
            catch (Exception)
            {
                LogNotify?.Invoke($"Client error: Connection {id}: Cannot connect to server.");
                return(false);
            }

            return(true);
        }
        private void ConnectionHandler(Socket connectedSocket, int id)
        {
            LogNotify?.Invoke($"Server: Connection {id}: Waiting request.");

            while (connectedSocket.Available == 0)
            {
            }

            LogNotify?.Invoke($"Server: Connection {id}: Receiving request.");

            StringBuilder builder = new StringBuilder();
            int           bytes   = 0;

            byte[] data = new byte[connectedSocket.Available];

            do
            {
                bytes = connectedSocket.Receive(data, data.Length, 0);
                builder.Append(Encoding.Unicode.GetString(data, 0, bytes));
            }while (connectedSocket.Available > 0);

            LogNotify?.Invoke($"Server: Connection {id}: Request received: {builder.ToString()}");

            bool   isNotification = false;
            string responce       = RequestHandler.Handle(builder.ToString(), out isNotification);


            if (!isNotification)
            {
                LogNotify?.Invoke($"Server: Connection {id}: Sending responce.");

                data = new byte[responce.Length];
                data = Encoding.Unicode.GetBytes(responce);
                connectedSocket.Send(data);

                LogNotify?.Invoke($"Server: Connection {id}: Responce sent: {responce}");
            }
            else
            {
                responce = " ";

                data = new byte[responce.Length];
                data = Encoding.Unicode.GetBytes(responce);
                connectedSocket.Send(data);

                LogNotify?.Invoke($"Server: Notification accepted.");
            }

            LogNotify?.Invoke($"Server: Connection {id}: Closing connection.");

            connectedSocket.Shutdown(SocketShutdown.Both);
            connectedSocket.Close();

            LogNotify?.Invoke($"Client: Server: Connection {id}: Connection closed.");
        }
        private void Listen()
        {
            LogNotify?.Invoke($"Server: Server running.");

            while (true)
            {
                LogNotify?.Invoke($"Server: Waiting for connection.");

                Socket connectedSocket = listenSocket.Accept();

                LogNotify?.Invoke($"Server: Connection accepted.");

                Task.Run(() => ConnectionHandler(connectedSocket, connectionId++));
            }
        }
        private string SendRequest(string jsonRpcRequest, int id)
        {
            LogNotify?.Invoke($"Client: Connection {id}: Start connection.");

            Socket sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            if (ConnectToServer(sendSocket, id) == false)
            {
                LogNotify?.Invoke($"Client error: Connection {id}: No connection  with server.");
                return(null);
            }

            LogNotify?.Invoke($"Client: Connection {id}: Server connection established.\nClient: Connection {id}: Sending request: {jsonRpcRequest}");

            byte[] data = Encoding.Unicode.GetBytes(jsonRpcRequest);
            sendSocket.Send(data);

            LogNotify?.Invoke($"Client: Connection {id}: Request sent.\nClient: Connection {id}: Waiting responce.");

            while (sendSocket.Available == 0)
            {
            }


            StringBuilder builder = new StringBuilder();
            int           bytes   = 0;

            data = new byte[sendSocket.Available];

            do
            {
                bytes = sendSocket.Receive(data, data.Length, 0);
                builder.Append(Encoding.Unicode.GetString(data, 0, bytes));
            }while (sendSocket.Available > 0);

            LogNotify?.Invoke($"Client: Connection {id}: Responce accepted: {builder.ToString()}\nClient: Connection {id}: Closing connection.");

            sendSocket.Shutdown(SocketShutdown.Both);
            sendSocket.Close();

            LogNotify?.Invoke($"Client: Connection {id}: Connection  closed.");

            return(builder.ToString());
        }
Example #5
0
        internal static void LogIt(string message)
        {
            if (LogNotify == null)
            {
                return;
            }

            foreach (Delegate del in LogNotify.GetInvocationList())
            {
                if (del.Target == null || ((ISynchronizeInvoke)del.Target).InvokeRequired == false)
                {
                    del.DynamicInvoke(new object[] { message, new EventArgs() });
                }
                else
                {
                    ((ISynchronizeInvoke)del.Target).Invoke(del, new object[] { message, new EventArgs() });
                }
            }
        }
Example #6
0
        public void LoadConfig()
        {
            ID          = iniRead(ConfigSection, "ID", "", ConfigFile);
            Token       = iniRead(ConfigSection, "Token", "", ConfigFile);
            Domain      = iniRead(ConfigSection, "Domain", "", ConfigFile);
            ServiceName = iniRead(ConfigSection, "ServiceName", "DDNS", ConfigFile);
            LogFile     = iniRead(ConfigSection, "LogFile", LogFile, ConfigFile);
            LogNotify   = Convert.ToBoolean(iniRead(ConfigSection, "LogNotify", LogNotify.ToString(), ConfigFile));
            LogIPChange = Convert.ToBoolean(iniRead(ConfigSection, "LogIPChange", LogIPChange.ToString(), ConfigFile));

            TimeLoop = Convert.ToInt32(iniRead(ConfigSection, "TimeLoop", "30", ConfigFile));
            if (TimeLoop < 5)
            {
                TimeLoop = 5;
            }

            string cHost = iniRead(ConfigSection, "Host", "", ConfigFile);

            Hosts = cHost.Split(',');
        }
Example #7
0
        public void SaveConfig()
        {
            iniWrite(ConfigSection, "ID", ID, ConfigFile);
            iniWrite(ConfigSection, "Token", Token, ConfigFile);
            iniWrite(ConfigSection, "Domain", Domain, ConfigFile);
            iniWrite(ConfigSection, "TimeLoop", TimeLoop.ToString(), ConfigFile);
            iniWrite(ConfigSection, "ServiceName", ServiceName, ConfigFile);
            iniWrite(ConfigSection, "LogFile", LogFile, ConfigFile);
            iniWrite(ConfigSection, "LogIPChange", LogIPChange.ToString(), ConfigFile);
            iniWrite(ConfigSection, "LogNotify", LogNotify.ToString(), ConfigFile);

            string subdomain = "";

            foreach (string cHost in Hosts)
            {
                if (cHost.Trim().Length > 0)
                {
                    subdomain += cHost + ",";
                }
            }
            iniWrite(ConfigSection, "Host", subdomain.Trim(','), ConfigFile);
        }
        private bool CreateAddress(ref IPEndPoint rpcPoint)
        {
            IPAddress iPAddress;

            if (IPAddress.TryParse(RpcIPAddress, out iPAddress) == false)
            {
                LogNotify?.Invoke($"Server error: Server address cannot be created. Invalid IP.");
                return(false);
            }

            try
            {
                rpcPoint = new IPEndPoint(iPAddress, RpcPort);
            }
            catch (ArgumentOutOfRangeException)
            {
                LogNotify?.Invoke($"Server error: Server address cannot be created. Invalid Port.");
                return(false);
            }

            return(true);
        }
        public async void ListenAsync()
        {
            LogNotify?.Invoke($"Server: Start.");

            if (Procedures == null)
            {
                LogNotify?.Invoke($"Server error: Server cannot be started. Procedures not set.");
                return;
            }

            listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            IPEndPoint rpcPoint = null;

            if (CreateAddress(ref rpcPoint) == false)
            {
                LogNotify?.Invoke($"Server error: Server cannot be started. Invalid Address.");
                return;
            }

            try
            {
                listenSocket.Bind(rpcPoint);
                listenSocket.Listen(10);
            }
            catch (SocketException)
            {
                LogNotify?.Invoke($"Server error: Server cannot be started. Socket unavailable.");
                return;
            }
            catch (Exception)
            {
                LogNotify?.Invoke($"Server error: Server cannot be started.");
                return;
            }

            await Task.Run(() => Listen());
        }