Exemplo n.º 1
0
        public void SendMessage(string recipient, string messageHead, string messageBody)
        {
            List <string> fileNameVec = FileSystem.ListFilesInDirectory(Globals.IpcPath);

            bool found     = true;
            int  fileCount = 0;

            while (found)
            {
                found = false;
                foreach (string fileNameCurrent in fileNameVec)
                {
                    if (fileNameCurrent == recipient + fileCount.ToString())
                    {
                        found = true;
                        ++fileCount;
                        break;
                    }
                }
            }

            string pathOld = Globals.IpcPath + "\\s" + selfName + IPC.SentCount.ToString();
            string pathNew = Globals.IpcPath + "\\" + recipient + fileCount.ToString();

            FileSystem.WriteStringToFile(pathOld, messageHead + "!" + messageBody);
            FileSystem.RenameFile(pathOld, pathNew);

            ++IPC.SentCount;

            Console.WriteLine("message sent: " + recipient + " " + messageHead + " " + messageBody);
        }
Exemplo n.º 2
0
        public int Assign(int portIn = -1)
        {
            //Setup the socket and message buffer
            udpSock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            bool fileFound = false;

            List <string> fileNameVec = FileSystem.ListFilesInDirectory(Globals.IpcPath);

            foreach (string fileNameCurrent in fileNameVec)
            {
                if (fileNameCurrent == "udp_port")
                {
                    fileFound = true;
                    break;
                }
            }

            int portCurrent;

            if (fileFound)
            {
                string udpPortStr = FileSystem.ReadTextFile(Globals.IpcPath + "\\udp_port")[0];
                int.TryParse(udpPortStr, out portCurrent);
                endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), portCurrent);
            }
            else
            {
                if (portIn == -1)
                {
                    endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 0);
                }
                else
                {
                    endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), portIn);
                }
            }

            udpSock.Bind(endPoint);
            int.TryParse(udpSock.LocalEndPoint.ToString().Split(':')[1], out portCurrent);
            FileSystem.WriteStringToFile(Globals.IpcPath + "\\" + "udp_port", portCurrent.ToString());

            buffer = new byte[1024];

            //Start listening for a new message.
            udpSock.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref endPoint, DoReceiveFrom, udpSock);

            return(portCurrent);
        }
Exemplo n.º 3
0
        public void Update()
        {
            if (!IPC.Updated)
            {
                return;
            }

            IPC.Updated = false;

            List <string> fileNameVec = FileSystem.ListFilesInDirectory(Globals.IpcPath);

            foreach (string fileNameCurrent in fileNameVec)
            {
                string fileNameEveryone = "";
                if (fileNameCurrent.Length >= 8)
                {
                    fileNameEveryone = fileNameCurrent.Substring(0, 8);
                }

                if (fileNameCurrent.Length > selfName.Length || fileNameEveryone == "everyone")
                {
                    if (IPC.FileNameProcessedMap.ContainsKey(fileNameCurrent) && IPC.FileNameProcessedMap[fileNameCurrent] == true)
                    {
                        continue;
                    }
                    else
                    {
                        IPC.FileNameProcessedMap[fileNameCurrent] = true;
                    }

                    string fileName   = "";
                    string fileNameID = "";
                    if (fileNameEveryone != "everyone")
                    {
                        fileName   = fileNameCurrent.Substring(0, selfName.Length);
                        fileNameID = fileNameCurrent.Substring(selfName.Length, fileNameCurrent.Length - selfName.Length);
                    }

                    if (fileName == selfName || fileNameEveryone == "everyone")
                    {
                        Thread.Sleep(20);

                        List <string> lines = FileSystem.ReadTextFile(Globals.IpcPath + "\\" + fileNameCurrent);
                        // FileSystem.DeleteFile(Globals.IpcPath + "\\" + fileNameCurrent);

                        string[] messageVec  = lines[0].Split('!');
                        string   messageHead = messageVec[0];
                        string   messageBody = messageVec[1];

                        Console.WriteLine("message received " + " " + messageHead + " " + messageBody + " " + fileNameCurrent);

                        if (!responseMap.ContainsKey(messageHead))
                        {
                            if (commandMap.ContainsKey(messageHead))
                            {
                                commandMap[messageHead](messageBody);
                            }
                        }
                        else
                        {
                            Func <string, int> func = responseMap[messageHead];
                            responseMap.Remove(messageHead);
                            func(messageBody);
                        }
                    }
                }
            }

            IPC.Updated = true;
        }