public void ListenToEveryDirectMsg(Action <MyIGCMessage> reader)
            {
                IMyUnicastListener l = p.IGC.UnicastListener;

                while (l.HasPendingMessage)
                {
                    reader(l.AcceptMessage());
                }
            }
            public Ship init()
            {
                Ship ship = new Ship();

                ship.id     = pb.EntityId;
                ship.type   = "0";
                ship.action = "NULL";
                igc.SendBroadcastMessage <String>(listenerId, ship.encode());
                if (unicastListener.HasPendingMessage)
                {
                    MyIGCMessage message = unicastListener.AcceptMessage();
                    Ship         s       = new Ship();
                    s.decode(message.Data.ToString());
                    return(s);
                }

                return(null);
            }
Exemple #3
0
            /*
             * 取出所有消息
             */
            public List <MyIGCMessage> getIGCMessages()
            {
                List <MyIGCMessage> messages = new List <MyIGCMessage>();

                while (unicastListener.HasPendingMessage)
                {
                    messages.Add(unicastListener.AcceptMessage());
                }
                return(messages);
            }
Exemple #4
0
            /// <summary>
            /// Process all pending IGC messages
            /// </summary>
            public void ProcessIGCMessages()
            {
                // TODO: make this a yield return thing if processing takes too long

                bool bFoundMessages = false;

                //            Echo(broadcastChanels.Count.ToString() + " broadcast channels");
                //            Echo(broadcastMessageHandlers.Count.ToString() + " broadcast message handlers");
                //            Echo(unicastMessageHandlers.Count.ToString() + " unicast message handlers");
                do
                {
                    bFoundMessages = false;
                    foreach (var channel in broadcastChanels)
                    {
                        if (channel.HasPendingMessage)
                        {
                            bFoundMessages = true;
                            var msg = channel.AcceptMessage();
                            foreach (var handler in broadcastMessageHandlers)
                            {
                                handler(msg);
                            }
                        }
                    }
                } while (bFoundMessages); // Process all pending messages

                if (myUnicastListener != null)
                {
                    do
                    {
                        // since there's only one channel, we could just use .HasPendingMessages directly.. but this keeps the code loops the same
                        bFoundMessages = false;

                        if (myUnicastListener.HasPendingMessage)
                        {
                            bFoundMessages = true;
                            var msg = myUnicastListener.AcceptMessage();
                            foreach (var handler in unicastMessageHandlers)
                            {
                                // Call each handler
                                handler(msg);
                            }
                        }
                    } while (bFoundMessages); // Process all pending messages
                }
            }
Exemple #5
0
            public void HandleMessage()
            {
                //handle broadcasts:
                while (_myBroadcastListener.HasPendingMessage)
                {
                    var myIGCMessage = _myBroadcastListener.AcceptMessage();
                    if (myIGCMessage.Tag == _recallRequestTag)
                    {
                        var data       = myIGCMessage.Data.ToString();
                        var data_parts = data.Split(';');
                        var arg        = data_parts[0];

                        long sourceGrid = 0;
                        long.TryParse(data_parts[1], out sourceGrid);

                        parent_program.shipIOHandler.Echo("Broadcast received. This ship has been ordered to dock.");

                        var arg_test = parent_program.FindHomeLocation(arg);
                        if (arg_test != null && sourceGrid != parent_program.Me.CubeGrid.EntityId)
                        {
                            parent_program.Main(arg, UpdateType.Script);
                        }
                    }
                }


                // handle unicasts:
                var bFoundMessages = false;

                do
                {
                    bFoundMessages = false;
                    if (_myUnicastListener.HasPendingMessage)
                    {
                        bFoundMessages = true;
                        var msg = _myUnicastListener.AcceptMessage();
                        if (msg.Tag == _responseTag)
                        {
                            ParsePositionalResponse(msg.Data.ToString());
                        }
                        //parent_program.shipIOHandler.Echo("Unicast received. Data: " + .ToString());
                        //parent_program.shipIOHandler.Echo("Tag: " + msg.Tag.ToString());
                    }
                } while (bFoundMessages);
            }
        public void Main(string argument, UpdateType updateSource)
        {
            Echo(string.Format("UPDATE CALLED FROM: {0}", updateSource.ToString()));
            if (argument == "DOCK_MESSAGE")
            {
                //script ran by anntenna receiving broadcast, with matching ID ensuring the broadcast is for this ship

                bool isAccepted = true;

                MyIGCMessage message = listener.AcceptMessage();
                Echo("RECEIVED FOLLOWING TRANSMISSION: " + message.Data.ToString());
                Dictionary <string, object> messageData = DecodeMessage((string)message.Data);
                Echo(string.Format("Is Accepted: {0}", messageData["accepted"].ToString()));
                isAccepted = (bool)messageData["accepted"];
                SetPanel(messageData["action"].ToString(), isAccepted, messageData["message"].ToString());

                return;
            }

            Echo("REQUESTING " + argument);
            SendMessage(argument);
        }
            private void ReadDirectMsgs()
            {
                // Listen direct messages
                IMyUnicastListener uniL = p.IGC.UnicastListener;
                int newRangeRequired    = -1;

                while (uniL.HasPendingMessage)
                {
                    MyIGCMessage msg = uniL.AcceptMessage();
                    LOGGER.Debug("Got direct msg");
                    if (msg.Tag == AutoAntennaRelayManager.TAG_ASK_CHANGE_RANGE)
                    {
                        newRangeRequired = Math.Max((int)msg.Data, newRangeRequired);
                    }
                }

                if (newRangeRequired > -1)
                {
                    SetAntennaRange(newRangeRequired);
                    SendMyInfo();
                }
            }
Exemple #8
0
 public void Main(string argument, UpdateType updateSource)
 {
     if ((updateSource & UpdateType.Terminal) > 0)
     {
         // отправляем сообщение
         count++;
         string message = "test message " + count;
         IGC.SendBroadcastMessage <string>(TAG, message, TransmissionDistance.TransmissionDistanceMax);
         Echo("send: " + message);
     }
     else if ((updateSource & UpdateType.IGC) > 0)
     {
         // получаем ответ
         Echo("argument: " + argument);
         if (listener.HasPendingMessage)
         {
             MyIGCMessage message = listener.AcceptMessage();
             if (message.Data is string)
             {
                 Echo("result: " + message.Data.ToString());
             }
         }
     }
 }
Exemple #9
0
            /// <summary>
            /// Process all pending IGC messages.
            /// </summary>
            public void ProcessIGCMessages()
            {
                bool bFoundMessages = false;

                if (_debug)
                {
                    _program.Echo(_broadcastChannels.Count.ToString() + " broadcast channels");
                }
                if (_debug)
                {
                    _program.Echo(_broadcastMessageHandlers.Count.ToString() + " broadcast message handlers");
                }
                if (_debug)
                {
                    _program.Echo(_unicastMessageHandlers.Count.ToString() + " unicast message handlers");
                }


                // TODO: make this a yield return thing if processing takes too long
                do
                {
                    bFoundMessages = false;
                    foreach (var channel in _broadcastChannels)
                    {
                        if (channel.HasPendingMessage)
                        {
                            bFoundMessages = true;
                            var msg = channel.AcceptMessage();
                            if (_debug)
                            {
                                _program.Echo("Broadcast received. TAG:" + msg.Tag);
                                _debugTextPanel?.WriteText("IGC:" + msg.Tag + " SRC:" + msg.Source.ToString("X") + "\n", true);
                            }
                            foreach (var handler in _broadcastMessageHandlers)
                            {
                                if (_debug)
                                {
                                    _program.Echo("Calling handler");
                                }
                                handler(msg);
                            }
                            if (_debug)
                            {
                                _program.Echo("Broadcast Handlers completed");
                            }
                        }
                    }
                } while (bFoundMessages); // Process all pending messages

                if (_unicastListener != null)
                {
                    if (_debug)
                    {
                        _program.Echo("Unicast check");
                    }

                    // TODO: make this a yield return thing if processing takes too long
                    do
                    {
                        // since there's only one channel, we could just use .HasPendingMessages directly.. but this keeps the code loops the same
                        bFoundMessages = false;

                        if (_unicastListener.HasPendingMessage)
                        {
                            bFoundMessages = true;
                            var msg = _unicastListener.AcceptMessage();
                            if (_debug)
                            {
                                _program.Echo("Unicast received. TAG:" + msg.Tag);
                            }
                            foreach (var handler in _unicastMessageHandlers)
                            {
                                if (_debug)
                                {
                                    _program.Echo(" Unicast Handler");
                                }
                                // Call each handler
                                handler(msg);
                            }
                            if (_debug)
                            {
                                _program.Echo("Broadcast Handlers completed");
                            }
                        }
                    } while (bFoundMessages); // Process all pending messages
                    if (_debug)
                    {
                        _program.Echo("Unicast check completed");
                    }
                }
            }
        // Handle the available messages
        bool  HandleMessages()
        {
            int incomingCount = 1; // keep a count of how many messages we've processed

            do
            {
                Echo("Message " + incomingCount.ToString());

                // check broadcast first, then unicast
                bool bBroadcast = _bListener.HasPendingMessage;
                if (bBroadcast)
                {
                    Echo("Broadcast");
                }
                else
                {
                    Echo("Unicast");
                }
                var msg = _bListener.HasPendingMessage ? _bListener.AcceptMessage() : _uListener.AcceptMessage();


                // information about the received message
                Echo("Received Message");
                Echo(msg.ToString());
                var src = msg.Source;
                Echo("Source=" + src.ToString());
                Echo("Data=" + msg.Data);
                Echo("Tag=" + msg.Tag);


                // we could check to see if the source of the message is still reachable (destroyed, out of range, etc)
                // if(IGC.IsEndpointReachable(msg.Source)) {}

                // If we got a brodcast message, reply with a unicast message to the sender
                if (bBroadcast)
                {
                    if (IGC.SendUnicastMessage <string>(msg.Source, UnicastTag, "Message received by:" + Me.EntityId.ToString()))
                    {
                        Echo("Response Sent");
                    }
                    else
                    {
                        Echo("Error sending response");
                    }
                }
                Echo("----");
                incomingCount++;
            } while (_bListener.HasPendingMessage || _uListener.HasPendingMessage); // Process all pending messages
            return(true);
        }