Пример #1
0
        public override void Tick()
        {
            Message m;
            var     messageReference = new List <Message>();
            var     serverPayload    = new List <NetMessage>();

            while ((m = Anima.Instance.MailBoxes.GetMessage(this)) is not null)
            {
                try
                {
                    messageReference.Add(m);
                    var nm = Anima.Deserialize <NetMessage>(m.Value);
                    //Anima.Instance.WriteLine($"Sending from: {m.Sender}, {Anima.Serialize(nm)}");
                    serverPayload.Add(nm);
                }
                catch (Exception e)
                {
                    Anima.Instance.ErrorStream.WriteLine($"Could not deserialize a message: {m} {Anima.NewLineChar}because: {e.Message}");
                }
            }

            var GetRequest = new NetMessage(true);

            serverPayload.Add(GetRequest);

            var payload           = serverPayload.ToArray();
            var serializedPayload = Anima.Serialize(payload);
            //Anima.Instance.WriteLine($"About to try and send: {serializedPayload}");
            var tcpClient = Helper.TryConnectClient(serverAddress, port);
            //Anima.Instance.WriteLine($"Tried to connect: {tcpClient}");
            var t = Helper.TrySendMessage(tcpClient, serializedPayload);

            //Anima.Instance.WriteLine($"Sent off: {t.Status}");
            t.Wait();
            //Anima.Instance.WriteLine($"It's done: {t.Status}, {t.Result}");
            if (!t.Result.Item1)
            {
                Anima.Instance.WriteLine($"Couldn't send messages, saving them for later: {t.Result}, {t.Status}");
                //If we couldn't send them, we need to put them back for later
                foreach (var msg in messageReference)
                {
                    Anima.Instance.MailBoxes.PostMessage(msg);
                }
                return;
            }

            var replyReader = new StreamReader(tcpClient.GetStream());
            //Anima.Instance.WriteLine($"Waiting for reply: {replyReader}");
            var reply = Helper.ReadFromStreamUntilEnd(replyReader);
            //Anima.Instance.WriteLine($"Got: {reply} in reply");
            var messageQueue = Anima.Deserialize <Queue <NetMessage> >(reply);

            foreach (var message in messageQueue)
            {
                var msg = new Message(message.SendPlugin, message.ReceivePlugin, "Remote", message.Value);
                Anima.Instance.MailBoxes.PostMessage(msg);
            }

            tcpClient.Close();
        }
Пример #2
0
        static void Main(string[] args)
        {
            Anima anima = null;
            var   state = new FileInfo("AnimaState.json");

            if (state.Exists)
            {
                using (var fs = new StreamReader(state.OpenRead()))
                {
                    var contents = fs.ReadToEnd();
                    if (!String.IsNullOrWhiteSpace(contents))
                    {
                        anima = Anima.Deserialize <Anima>(contents);
                        if (anima is null)
                        {
                            Console.WriteLine("Loading failed");
                        }
                        Console.WriteLine("Loaded in from state file");
                    }
                }
            }
            else
            {
                Console.WriteLine($"Could not find: {state.FullName}");
            }
            anima ??= Anima.Instance;

            Console.CancelKeyPress += delegate(object sender, ConsoleCancelEventArgs e)
            {
                //Save state
                var newState = Anima.Serialize(anima);
                //Quick way to remake file fresh
                state.Delete();
                using (var fs = new StreamWriter(state.Create()))
                {
                    fs.Write(newState);
                }
            };

            try
            {
                anima.Run();
            }
            finally
            {
                //Save state
                var newState = Anima.Serialize(anima);
                //Quick way to remake file fresh
                state.Delete();
                using (var fs = new StreamWriter(state.Create()))
                {
                    fs.Write(newState);
                }
            }
        }
Пример #3
0
        private void HandleRequests()
        {
            while (true)
            {
                try
                {
                    var client = listener.AcceptTcpClient();

                    var strem = new StreamReader(client.GetStream());

                    string ReadContents = Helper.ReadFromStreamUntilEnd(strem);

                    var netMessages = Anima.Deserialize <NetMessage[]>(ReadContents);

                    bool AnyGetRequests = netMessages.Any(nm => nm.GetRequest);


                    if (AnyGetRequests)
                    {
                        var host = netMessages.First()?.SendHost;
                        if (host is null)
                        {
                            Anima.Instance.WriteLine($"Unable to get send host name: {netMessages.Length}");
                        }

                        if (relayBuffer.ContainsKey(netMessages.First().SendHost))
                        {
                            string Messages = Anima.Serialize(relayBuffer[host]);
                            var    t        = Helper.TrySendMessage(client, Messages);
                            t.Wait();
                            if (!t.Result.Item1)
                            {
                                Anima.Instance.ErrorStream.WriteLine("Was unable to send reply");
                            }
                            else
                            {
                                //Successfully sent reply. Can clear buffer
                                relayBuffer[host].Clear();
                            }
                        }
                        else
                        {
                            Anima.Instance.WriteLine($"No messages for: {netMessages.First().SendHost}");
                        }
                    }

                    foreach (var netMessage in netMessages)
                    {
                        if (string.IsNullOrWhiteSpace(netMessage.ReceiveHost))
                        {
                            continue;
                        }

                        netMessage.GetRequest = false;
                        if (relayBuffer.ContainsKey(netMessage.ReceiveHost) &&
                            (relayBuffer[netMessage.ReceiveHost] is not null))
                        {
                            relayBuffer[netMessage.ReceiveHost].Enqueue(netMessage);
                        }
                        else
                        {
                            relayBuffer.Add(netMessage.ReceiveHost, new Queue <NetMessage>());
                            relayBuffer[netMessage.ReceiveHost].Enqueue(netMessage);
                        }
                    }

                    client.Close();
                }
                catch (SocketException se)
                {
                    if (se.SocketErrorCode == SocketError.AddressFamilyNotSupported)
                    {
                        Anima.Instance.ErrorStream.WriteLine(se.Message);
                    }
                    else
                    {
                        Anima.Instance.ErrorStream.WriteLine(se.Message);
                    }
                }
                catch (Exception e)
                {
                    Anima.Instance.ErrorStream.WriteLine(e);
                }
            }
        }