コード例 #1
0
        private void UserAddError(NetworkEngine.Client client, UserAcceptStates status)
        {
            switch (status)
            {
            case UserAcceptStates.unknown:
                client.Send("UN", "ERROR");
                break;

            case UserAcceptStates.badToken:
                client.Send("BT", "ERROR");
                break;

            default:
                break;
            }
        }
コード例 #2
0
        public static int packetSize = 32000; // 255 - default

        private void _SendFile(string filePath)
        {
            //string fileContent = File.ReadAllText(filePath);

            //client.Send(fileContent, "DLOAD");


            //byte[] fileContent = File.ReadAllBytes(filePath);

            /*string fileContentStr = "";
             *
             * for(int i = 0;i<fileContent.Length;i++)
             * {
             *  fileContentStr += (char)fileContent[i];
             * }*/

            //Convert.



            byte[] fileContent = new byte[packetSize];

            /*for(int i = 0;i<255;i++)
             * {
             *  fileContent[i] = 124;
             * }*/



            using (FileStream fs = File.OpenRead(filePath))
            {
                var binaryReader = new BinaryReader(fs);
                //fileContent = binaryReader.ReadBytes((int)fs.Length);
                int count = 0;

                while (fs.Position < fs.Length - packetSize)
                {
                    fileContent = binaryReader.ReadBytes(packetSize);



                    client.Send(fileContent, "", false);
                    count++;
                    if (count % 500 == 0)
                    {
                    }



                    //Thread.Sleep(50);
                }

                // DISABLED FOR DEBUGGING PURPOSES
                fileContent = binaryReader.ReadBytes((int)(fs.Length - fs.Position));

                client.Send(fileContent, "", false);

                Debug.Log("Sent " + fs.Position + " bytes");
            }

            working = false;
        }
コード例 #3
0
 private void AddUser(NetworkEngine.Client client, string token)
 {
     client.Send("OK", "DAUTH");
     DownloadEngine newDownloadClient = new DownloadEngine(client, port, token);
 }
コード例 #4
0
        public void DataReceived(string data, NetworkEngine.Client client)
        {
            string response     = "UK";
            string responseCode = "UNKNW";

            Console.WriteLine("DownloadEngine: Data received: " + data);
            string command = data;

            if (data.Length > 5)
            {
                command = data.Remove(5);
            }

            data = data.Remove(0, 5);

            switch (command)
            {
            case "OK":
                return;

            //Download
            case "DWNLD":
            {
                if (user == null)
                {
                    response     = "NA";
                    responseCode = "ERROR";
                    break;
                }
                if (data.Length == 0)
                {
                    response     = "NF";
                    responseCode = "ERROR";
                    break;
                }
                Int64 id = -1;
                if (!Int64.TryParse(data, out id))
                {
                    Debug.LogError("Cannot parse \"" + data + "\" to Int64");

                    response     = "NF";
                    responseCode = "ERROR";
                    break;
                }

                StoreServer.Game game = StoreServer.FindGame(id);

                if (game == null)
                {
                    response     = "NF";
                    responseCode = "ERROR";
                    break;
                }

                if (!StoreServer.CheckIfUserHaveGame(user, game))
                {
                    response     = "NA";
                    responseCode = "ERROR";
                    break;
                }



                UploadStatus status = SendFile(DownloadsManager.downloadFilesDirectory + game.filename, true, false);

                switch (status)
                {
                case UploadStatus.success:
                    Debug.Log("File sent successfully");
                    client.Send("OK", "DWNLD");
                    workingThread.Start();

                    return;

                case UploadStatus.alreadyWorking:
                    Debug.Log("Thread is busy");
                    break;

                case UploadStatus.fileDoesntExist:
                    response     = "NF";
                    responseCode = "ERROR";
                    break;

                case UploadStatus.unknownError:

                    break;

                default:
                    break;
                }
            }

            break;

            //Game icon
            case "GICON":
            {
                int resLevel = -1;
                if (data.Length == 0)
                {
                    response     = "NF";
                    responseCode = "ERROR";
                    break;
                }

                if (data[0] == 'H' || data[0] == 'h')
                {
                    resLevel = 1;
                }
                else if (data[0] == 'L' || data[0] == 'l')
                {
                    resLevel = 0;
                }
                else
                {
                    response     = "BS";
                    responseCode = "ERROR";
                    break;
                }

                data = data.Remove(0, 1);

                if (user == null)
                {
                    //Not authorised
                    response     = "NA";
                    responseCode = "ERROR";
                    break;
                }
                if (data.Length == 0)
                {
                    response     = "NF";
                    responseCode = "ERROR";
                    break;
                }
                Int64 id = -1;
                if (!Int64.TryParse(data, out id))
                {
                    Debug.LogError("Cannot parse \"" + data + "\" to Int64");

                    response     = "NF";
                    responseCode = "ERROR";
                    break;
                }

                StoreServer.Game game = StoreServer.FindGame(id);

                if (game == null)
                {
                    //Not found
                    response     = "NF";
                    responseCode = "ERROR";
                    break;
                }

                if (game.iconPath == "\0")
                {
                    //No icon
                    response     = "NI";
                    responseCode = "ERROR";
                    break;
                }



                UploadStatus status = SendFile(DownloadsManager.downloadIconsDirectory + game.iconPath + "_" + resLevel.ToString() + ".png", true, false);

                switch (status)
                {
                case UploadStatus.success:
                    Debug.Log("File sent successfully");
                    client.Send("OK", "DWNLD");
                    workingThread.Start();

                    return;

                case UploadStatus.alreadyWorking:
                    Debug.Log("Thread is busy");
                    break;

                case UploadStatus.fileDoesntExist:
                    response     = "NF";
                    responseCode = "ERROR";
                    break;

                case UploadStatus.unknownError:

                    break;

                default:
                    break;
                }
            }
            break;

            //Not found
            default:
                response     = "NF";
                responseCode = "ERROR";
                break;
            }
            Debug.LogWarning("Sending response ");
            client.Send(response, responseCode);
        }