Ejemplo n.º 1
0
        public static void Serialize(MemoryStream output, params object[] objs)
        {
            long initialPosition = output.Position;

            WriteSize(output, 0);   // empty bytes

            foreach (object m in objs)
            {
                long initialPos = output.Position;
                WriteSize(output, 0);   // empty bytes
                SerializeOne(output, m);
                long finalPos = output.Position;

                long objectSize = finalPos - initialPos - SizeSize;
                MyAssert.Assert(objectSize > 0);

                output.Position = initialPos;
                WriteSize(output, (int)objectSize);
                output.Position = finalPos;
            }

            long finalPosition = output.Position;
            long totalSize     = finalPosition - initialPosition - SizeSize;

            output.Position = initialPosition;
            MyAssert.Assert(totalSize >= 0);
            if (objs.Length > 0)
            {
                MyAssert.Assert(totalSize > 0);
            }
            WriteSize(output, (int)totalSize);

            output.Position = finalPosition;
        }
Ejemplo n.º 2
0
        // questionable design choices for sake of smoother application

        public void Respond(ref RemoteAction ra, Action <Response, Stream> followUp_)
        {
            followUp = followUp_;

            MyAssert.Assert(ra == null);
            ra = this;
        }
Ejemplo n.º 3
0
        public void TriggerOnEmpty(Action a)
        {
            MyAssert.Assert(onClear == null);
            onClear = a;

            EmptyCheck();
        }
Ejemplo n.º 4
0
        protected void MessageServer(MessageType mt, params object[] arg)
        {
            MyAssert.Assert(serverHost != null);

            Host.TryConnectAsync(serverHost, ProcessServerDisconnect);
            Host.SendMessage(serverHost, mt, arg);
        }
Ejemplo n.º 5
0
        public Node ConnectAsync(OverlayEndpoint theirInfo, Node.DisconnectProcessor dp)
        {
            dp = ProcessDisconnectWrap(dp);
            Node targetNode = FindNode(theirInfo);

            Handshake info = new Handshake(Address, theirInfo, extraHandshakeInfo);

            bool newConnection = (targetNode == null);

            if (newConnection)
            {
                targetNode = new Node(info, processQueue, dp);
                AddNode(targetNode);
            }

            MyAssert.Assert(targetNode.readerStatus != Node.ReadStatus.DISCONNECTED);
            MyAssert.Assert(targetNode.writerStatus != Node.WriteStatus.DISCONNECTED);
            MyAssert.Assert(!targetNode.IsClosed);

            if (targetNode.writerStatus == Node.WriteStatus.WRITING)
            {
                throw new NodeException("Already connected/connecting to " + targetNode.Address);
            }
            else
            {
                targetNode.ConnectAsync();
            }

            if (newConnection)
            {
                onNewConnectionHook.Invoke(targetNode);
            }

            return(targetNode);
        }
Ejemplo n.º 6
0
        public void SoftDisconnect(OverlayEndpoint theirInfo)
        {
            Node targetNode = FindNode(theirInfo);

            MyAssert.Assert(targetNode != null);

            targetNode.SoftDisconnect();
        }
Ejemplo n.º 7
0
        public void Remove(Guid id)
        {
            MyAssert.Assert(remotes.ContainsKey(id));

            remotes.Remove(id);

            EmptyCheck();
        }
Ejemplo n.º 8
0
        public void SendMessage(OverlayEndpoint remote, NetworkMessage nm)
        {
            Node n = FindNode(remote);

            MyAssert.Assert(n != null);

            n.SendMessage(nm);
        }
Ejemplo n.º 9
0
        public TimedAction(Action a, TimeSpan period)
        {
            MyAssert.Assert(period > TimeSpan.Zero);

            this.a        = a;
            this.period   = period;
            this.timeLeft = period;
        }
Ejemplo n.º 10
0
    public override void NET_RemoveBlock(Point pos)
    {
        ITile t = this[pos];

        MyAssert.Assert(t.IsEmpty());
        MyAssert.Assert(walls[pos] != null);

        UnityEngine.Object.Destroy(walls[pos]);
    }
Ejemplo n.º 11
0
        public void Close()
        {
            foreach (var n in GetAllNodes().ToArray())
            {
                DisconnectNode(n);
            }

            MyAssert.Assert(!nodes.Any());
        }
Ejemplo n.º 12
0
        void OnStopValidating(Node n)
        {
            IPEndPoint addr = n.info.remote.addr;

            MyAssert.Assert(validatorPool.Contains(addr));
            validatorPool.Remove(addr);

            Log.Dump(n.info.remote);
        }
Ejemplo n.º 13
0
        public void Untrack()
        {
            --tracker;
            MyAssert.Assert(tracker >= 0);

            if (tracker == 0)
            {
                Unsubscribe();
            }
        }
Ejemplo n.º 14
0
        public void Add(Action a)
        {
            lock (syncLock)
                if (a != null && executeThreadId != null)
                {
                    MyAssert.Assert(executeThreadId != Thread.CurrentThread.ManagedThreadId);
                }

            msgs.Add(a);
        }
Ejemplo n.º 15
0
        public void FinalizeVerifier()
        {
            MyAssert.Assert(finalizing == false);
            finalizing = true;

            if (locked == null)
            {
                OnFinalizedVerifier();
            }
        }
Ejemplo n.º 16
0
        static public void Process(ref RemoteAction ra, Node n, Stream st)
        {
            Response r  = Serializer.Deserialize <Response>(st);
            Guid     id = Serializer.Deserialize <Guid>(st);

            MyAssert.Assert(ra.Id == id);

            ra.FollowUp(n, st, r);

            ra = null;
        }
Ejemplo n.º 17
0
        void FollowUp(Node n, Stream st, Response r)
        {
            MyAssert.Assert(n.info.remote == remoteHost);

            if (l != null)
            {
                l.Unlock();
            }

            followUp.Invoke(r, st);
        }
Ejemplo n.º 18
0
        public void WorldAction(ForwardFunctionCall ffc, WorldInfo info)
        {
            if (!ValidateWorldInfo(info))
            {
                return;
            }

            MyAssert.Assert(world != null);
            ffc.Apply(world);
            ffc.Apply(worldHook);
        }
Ejemplo n.º 19
0
        public void Tick(TimeSpan tickPeriod)
        {
            MyAssert.Assert(a != null);

            timeLeft -= tickPeriod;

            if (timeLeft <= TimeSpan.Zero)
            {
                a.Invoke();
                timeLeft = period;
            }
        }
Ejemplo n.º 20
0
        static void WriteSize(Stream output, int size)
        {
            byte[] intBytes = BitConverter.GetBytes(size);

            MyAssert.Assert(intBytes.Length == sizeof(int));

            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(intBytes);
            }

            output.Write(intBytes, 0, intBytes.Length);
        }
Ejemplo n.º 21
0
        public bool TryCloseNode(OverlayEndpoint remote)
        {
            Node n = FindNode(remote);

            if (n != null)
            {
                MyAssert.Assert(!n.IsClosed);
                n.Disconnect();
                return(true);
            }

            return(false);
        }
Ejemplo n.º 22
0
        public void Remove(Guid player)
        {
            MyAssert.Assert(playerPositions.ContainsKey(player));

            Point prevPos = playerPositions[player];

            foreach (Point p in Point.SymmetricRange(Point.One))
            {
                untrack.Invoke(prevPos + p);
            }

            playerPositions.Remove(player);
        }
Ejemplo n.º 23
0
        void OnFinalizedVerifier()
        {
            MyAssert.Assert(finalizing == true);
            MyAssert.Assert(locked == null);

            if (playerData.IsConnected)
            {
                MessageWorld(playerData.world.Value, MessageType.PLAYER_DISCONNECT);
                OnDisconnect();
            }

            MessageServer(MessageType.PLAYER_HOST_DISCONNECT, playerData);
        }
Ejemplo n.º 24
0
        internal void NewIncomingConnection(Handshake info, Socket sck)
        {
            try
            {
                MyAssert.Assert(info.local.hostname == hostName);

                MemoryStream remoteExtraInfo = info.ExtraInfo;
                info.ExtraInfo = extraHandshakeInfo;

                Node targetNode = FindNode(info.remote);

                bool newConnection = (targetNode == null);
                if (newConnection)
                {
                    targetNode = new Node(info, processQueue, null);
                    AddNode(targetNode);
                }

                NodeProcessors proc = processorAssigner(targetNode, remoteExtraInfo);
                proc.Message    = ProcessMessageWrap(proc.Message);
                proc.Disconnect = ProcessDisconnectWrap(proc.Disconnect);

                targetNode.notifyDisonnect = proc.Disconnect;

                MyAssert.Assert(targetNode.readerStatus != Node.ReadStatus.DISCONNECTED);
                MyAssert.Assert(targetNode.writerStatus != Node.WriteStatus.DISCONNECTED);
                MyAssert.Assert(!targetNode.IsClosed);

                if (targetNode.readerStatus != Node.ReadStatus.READY)
                {
                    Log.Console("New connection {0} rejected: node already connected", info.remote);
                    sck.Close();
                    return;
                }

                targetNode.AcceptReaderConnection(sck, proc.Message);

                if (newConnection)
                {
                    onNewConnectionHook.Invoke(targetNode);
                }

                //if (targetNode.writerStatus == Node.WriteStatus.READY)
                //    targetNode.ConnectAsync();
            }
            catch (NodeException) // FIXME
            {
                sck.Close();
                throw;
            }
        }
Ejemplo n.º 25
0
        static internal int ReadSize(Stream input)
        {
            byte[] intBytes = new byte[sizeof(int)];
            int    c        = input.Read(intBytes, 0, intBytes.Length);

            MyAssert.Assert(c == intBytes.Length);

            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(intBytes);
            }

            return(BitConverter.ToInt32(intBytes, 0));
        }
Ejemplo n.º 26
0
        static private void Tick(Aggregator all)
        {
            foreach (var id in timings.Keys.ToArray())
            {
                timings[id]--;

                if (timings[id] <= 0)
                {
                    int timeout = PlayerAiMove(all, id);
                    MyAssert.Assert(timeout > 0 && timeout < 10);
                    timings[id] = timeout;
                }
            }
        }
Ejemplo n.º 27
0
        public OverlayHost NewHost(OverlayHostName hostName, OverlayHost.ProcessorAssigner messageProcessor, MemoryStream extraHandshakeInfo,
                                   TimeSpan inactivityPeriod)
        {
            MyAssert.Assert(!hosts.ContainsKey(hostName));

            OverlayHost host = new OverlayHost(hostName, MyAddress, processQueue, messageProcessor, extraHandshakeInfo,
                                               tt, inactivityPeriod);

            hosts.Add(hostName, host);

            Log.EntryConsole(log, "New host: " + hostName);

            return(host);
        }
Ejemplo n.º 28
0
        public void WorldInit(WorldInitializer init)
        {
            if (!ValidateWorldInfo(init.info))
            {
                return;
            }

            MyAssert.Assert(world == null);

            world = data.generateWorld(init);
            foreach (WorldInfo inf in world.GetKnownNeighbors())
            {
                data.recordWorldInfo(inf);
            }
        }
Ejemplo n.º 29
0
        private void Unsubscribe()
        {
            if (isSubscribed)
            {
                isSubscribed = false;
                MyAssert.Assert(Info != null);
                data.unsubscribe(Info.Value);
                //data.host.ConnectSendMessage(Info.Value.host, MessageType.UNSUBSCRIBE);

                if (world != null)
                {
                    world.Dispose();
                    world = null;
                }
            }
        }
Ejemplo n.º 30
0
    public override void NET_RemovePlayer(Guid player, bool teleporting)
    {
        base.NET_RemovePlayer(player, teleporting);

        MyAssert.Assert(playerAvatars.ContainsKey(player));

        GameObject avatar = playerAvatars.GetValue(player);

        if (teleporting)
        {
            NewTeleportAnimation(avatar.transform.position, avatar.renderer.material.color);
        }

        UnityEngine.Object.Destroy(avatar);
        playerAvatars.Remove(player);
    }