Ejemplo n.º 1
0
 private bool isConnectionRequest(MultiplayerTask task)
 {
     if (task.peer != null && task.peer.connect)
     {
         return(true);
     }
     return(false);
 }
Ejemplo n.º 2
0
 private void addToLog(MultiplayerTask message)
 {
     lock (_chatLock) {
         while (chatMessages.Count >= MAX_CHAT_SIZE)
         {
             chatMessages.Dequeue();
         }
         //chatMessages.Enqueue(message);
     }
 }
Ejemplo n.º 3
0
        private NetOutgoingMessage createPeerInfo(IPEndPoint end, String pname)
        {
            MultiplayerTask task = new MultiplayerTask();

            task.peer            = new MultiplayerTask.Peer();
            task.peer.ip         = System.Text.Encoding.Unicode.GetString(end.Address.GetAddressBytes());
            task.peer.port       = end.Port;
            task.peer.playerName = pname;
            task.peer.connect    = true;
            String             serializedTask = JsonConvert.SerializeObject(task);
            NetOutgoingMessage msg            = peer.CreateMessage();

            msg.Write(serializedTask);
            return(msg);
        }
Ejemplo n.º 4
0
 private void readEngineQueue()
 {
     lock (_dataLock) {
         MultiplayerTask task = engineTasks.Dequeue();
     }
 }
Ejemplo n.º 5
0
        private void drawGUI(int windowID)
        {
            GUIStyle mySty = new GUIStyle(GUI.skin.button);

            mySty.normal.textColor   = mySty.focused.textColor = Color.white;
            mySty.hover.textColor    = mySty.active.textColor = Color.cyan;
            mySty.onNormal.textColor = mySty.onFocused.textColor = mySty.onHover.textColor = mySty.onActive.textColor = Color.green;
            mySty.padding            = new RectOffset(8, 8, 8, 8);
            GUILayout.BeginHorizontal();
            playerName = GUILayout.TextField(playerName, 30);
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            ip   = GUILayout.TextField(ip, GUILayout.Width(100));
            port = GUILayout.TextField(port, GUILayout.Width(30));
            pass = GUILayout.PasswordField(pass, '*', GUILayout.MinWidth(20));
            GUILayout.EndHorizontal();
            GUILayout.BeginVertical();
            hostSave = GUILayout.Toggle(hostSave, "Host Game");

            if (GUILayout.Button("Connect!", GUILayout.Height(30)))
            {
                int intport;
                if (int.TryParse(port, out intport))
                {
                    client = new MultiplayerPeer(15, playerName, this);
                    IPAddress  addr = new IPAddress(System.Text.Encoding.Unicode.GetBytes(ip));
                    IPEndPoint end  = new IPEndPoint(addr, intport);
                    client.sendDiscoveryRequest(end);
                    clientWorker = new Thread(client.messageStream);
                    clientWorker.Start();
                }
            }

            if (hostedSaveKey == null && hostSave && client != null && client.isConnected)
            {
                scrollpos = GUILayout.BeginScrollView(scrollpos);
                GUILayout.BeginVertical();
                IEnumerable <string> keys = saves.Names;
                int ii = 0;
                foreach (string key in keys)
                {
                    comboBox [ii] = GUILayout.Toggle(comboBox [ii], saves [key].Game.Title);
                    if (comboBox [ii] && hostSave)
                    {
                        MultiplayerTask task = new MultiplayerTask();

                        hostedSaveKey = key;
                        break;
                    }
                    ii++;
                }
                GUILayout.EndVertical();
                GUILayout.EndScrollView();
            }
            else
            {
                scrollpos = GUILayout.BeginScrollView(scrollpos);
                GUILayout.BeginVertical();
                lock (client._logLock) {
                    int len = client.logSize;
                    for (int ii = logStart; ii < len; ii++)
                    {
                        GUILayout.Label(client.streamLog[ii]);
                    }
                }
                foreach (string message in chatMessages)
                {
                    GUILayout.Label(message);
                }
                GUILayout.EndVertical();
                GUILayout.EndScrollView();

                GUILayout.BeginHorizontal();
                chatMessage = GUILayout.TextField(chatMessage, GUILayout.Width(180));
                GUILayout.FlexibleSpace();
                if (GUILayout.Button("Send", GUILayout.Width(30)))
                {
                    chatMessage = playerName + ": " + chatMessage;
                    lock (client._taskLock) {
                        MultiplayerTask task = new MultiplayerTask();
                        task.generalMessage = chatMessage;
                    }
                }
                GUILayout.EndHorizontal();
            }
            GUI.DragWindow(new Rect(0, 0, 10000, 40));
        }
Ejemplo n.º 6
0
        public void messageStream()
        {
            while (isActive)
            {
                if (watch == null)
                {
                    watch = Stopwatch.StartNew();
                }
                else
                {
                    watch.Reset();
                }

                NetIncomingMessage msg;
                while ((msg = peer.ReadMessage()) != null)
                {
                    switch (msg.MessageType)
                    {
                    case NetIncomingMessageType.DiscoveryRequest:
                        addToLog("Discovery Request from ip: " + msg.SenderEndPoint.Address.ToString());
                        NetOutgoingMessage resp = peer.CreateMessage();
                        resp.Write(playerName);
                        peer.SendDiscoveryResponse(resp, msg.SenderEndPoint);
                        break;

                    case NetIncomingMessageType.DiscoveryResponse:
                        addToLog("Discovery Response from player: " + msg.ReadString());
                        peer.Connect(msg.SenderEndPoint);
                        isConnected = true;
                        break;

                    case NetIncomingMessageType.ConnectionApproval:
                        msg.SenderConnection.Approve();
                        NetOutgoingMessage send = this.createPeerInfo(msg.SenderEndPoint, "");
                        break;

                    case NetIncomingMessageType.Data:
                        try{
                            int messageType = (int)msg.ReadByte();
                            if (((MessageType)messageType) == MessageType.File)
                            {
                                string[]      gameInfo = TransferLogic.getFileInfo(msg);                              //sets msg cursor at files contents
                                TransferLogic logic    = TransferLogic.instantiateTransfer(gameInfo);
                                TransferLogic.createGameStructure(gameInfo[1]);
                                user.CurrentGame = gameInfo[1];
                                if (logic != null)
                                {
                                    byte[] contents = msg.ReadBytes(logic.length);
                                    logic.writeFile(contents);
                                    logic.copyToGameFile();
                                }
                            }
                            else if (((MessageType)messageType) == MessageType.FileUpdate)
                            {
                                string length = TransferLogic.getNextPart(msg);
                                int    len;
                                if (int.TryParse(length, out len))
                                {
                                    TransferLogic logic = new TransferLogic("persistent.sfs", user.CurrentGame, FileType.PERSISTENT, len);
                                    logic.readSaveUpdate(msg);
                                }
                            }
                        }catch (Exception e) { Console.WriteLine(e.Message); }

                        break;

                    case NetIncomingMessageType.UnconnectedData:
                        break;

                    case NetIncomingMessageType.StatusChanged:
                        break;

                    default:
                        break;
                    }
                }

                lock (_taskLock) {
                    if (sendTasks.Count > 0)
                    {
                        MultiplayerTask send = sendTasks.Dequeue();
                        send.peer            = new MultiplayerTask.Peer();
                        send.peer.playerName = this.playerName;
                        send.peer.connect    = false;
                        NetOutgoingMessage snd = peer.CreateMessage();
                        snd.Write(JsonConvert.SerializeObject(send));
                    }
                }

                watch.Stop();
                long elapsed = watch.ElapsedMilliseconds;
                Thread.Sleep(1000 / networkRate - (int)elapsed);
            }
        }
Ejemplo n.º 7
0
        private MultiplayerTask createMultiplayerTask(String task)
        {
            MultiplayerTask clientTask = (MultiplayerTask)JsonConvert.DeserializeObject(task);

            return(clientTask);
        }