Example #1
0
        public static TransferLogic instantiateTransfer(string[] parts)
        {
            if (parts [0] == String.Empty || parts [1] == String.Empty)
            {
                return(null);
            }
            string fileName   = parts [0];
            string gameFolder = parts [1];
            int    fileLength;

            if (int.TryParse(parts [2], out fileLength))
            {
                FileType type;
                if (fileName.IndexOf("SPH") > -1)
                {
                    type     = FileType.SPH_CRAFT;
                    fileName = fileName.Substring(fileName.IndexOf("/") + 1);
                }
                else if (fileName.IndexOf("persistent") > -1)
                {
                    type = FileType.PERSISTENT;
                }
                else if (fileName.IndexOf("quicksave") > -1)
                {
                    type = FileType.QUICKSAVE;
                }
                else
                {
                    type     = FileType.VAB_CRAFT;
                    fileName = fileName.Substring(fileName.IndexOf("/") + 1);
                }

                TransferLogic logic = new TransferLogic(fileName, gameFolder, type, fileLength);
                TransferLogic.createGameStructure(gameFolder);
                return(logic);
            }

            return(null);
        }
        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);
            }
        }