//TODO: This source should be modified what "try ~ catch" syntax .
 public MessageNode GetNodeFromJson(String2 json)
 {
     try
     {
         IDictionary <String, String> buffer = JsonConvert.DeserializeObject <Dictionary <String, String> >(json.ToString());
         MessageNode node = new MessageNode();
         if (buffer.ContainsKey("TYPE"))
         {
             node.MessageType = (MessageType)Int32.Parse(buffer["TYPE"]);
         }
         if (buffer.ContainsKey("MESSAGE"))
         {
             node.Message = buffer["MESSAGE"];
         }
         if (buffer.ContainsKey("WORKTITLE"))
         {
             node.WorkTitle = buffer["WORKTITLE"];
         }
         return(node);
     }
     catch (Exception)
     {
         return(null);
     }
 }
        public String2 GetJsonFromNode(MessageNode node)
        {
            IDictionary <String, Object> buffer = new Dictionary <String, Object>();

            buffer.Add("TYPE", (int)node.MessageType);
            buffer.Add("WORKTITLE", node.WorkTitle);
            buffer.Add("MESSAGE", node.Message);
            buffer.Add("LIST", node.Files);
            return(new String2(JsonConvert.SerializeObject(buffer), Encoding.UTF8));
        }
Beispiel #3
0
 public Action <IWorkSocketClient, byte, String2> SetReceive()
 {
     return((client, opcode, data) =>
     {
         if (file.Open && opcode != (int)Opcode.BINARY)
         {
             file.Init();
         }
         if (opcode == (int)Opcode.MESSAGE)
         {
             MessageNode message = MessageDirector.Instance().GetNodeFromJson(data);
             if (message.MessageType == MessageType.MESSAGE)
             {
                 MessageNode sendMessage = MessageDirector.Instance().CreateNode();
                 sendMessage.MessageType = MessageType.MESSAGE;
                 sendMessage.Message = client.SocketClient.RemoteEndPoint + "-" + message.Message;
                 String2 json = MessageDirector.Instance().GetJsonFromNode(sendMessage);
                 SendBroadcast(MessageType.MESSAGE, json);
                 Console.WriteLine(sendMessage.Message);
             }
             else if (message.MessageType == MessageType.WORKTEMP)
             {
                 FileInfo info = new FileInfo(Program.WORK_PATH + Path.DirectorySeparatorChar + message.WorkTitle);
                 String2 buffer = new String2(message.Message, Encoding.UTF8);
                 using (FileStream stream = new FileStream(info.FullName, FileMode.Create, FileAccess.Write))
                 {
                     stream.Write(buffer.ToBytes(), 0, buffer.Length);
                 }
                 SendWorkList(client, WorkType.WorkListNotice);
             }
             else if (message.MessageType == MessageType.WORKNOTICE)
             {
                 String buffer = message.Message;
                 SendWorkTemp(client, buffer.Trim(), buffer.Trim());
             }
         }
         if (opcode == (int)Opcode.BINARY)
         {
             if (data.Length < 1)
             {
                 //logger.Error("It is being have downloading.but because what the data is nothing is stopped.");
                 //continue;
             }
             byte type = data[0];
             if (type == (byte)FileMessageType.FileOpen)
             {
                 file.Length = BitConverter.ToInt32(data.ToBytes(), 1);
                 String2 filename = data.SubString(5, data.Length - 5);
                 filename.Encode = Encoding.UTF8;
                 //logger.Info("filename - " + filename);
                 file.SetStream(new FileStream(Program.FILE_STORE_PATH + filename.Trim().ToString(), FileMode.Create, FileAccess.Write), file.Length);
                 return;
             }
             if (type == (byte)FileMessageType.FileWrite)
             {
                 if (!file.Open)
                 {
                     //logger.Error("It is being have downloading.but because what file's connection is closed.");
                     file.Init();
                     return;
                 }
                 String2 binary = data.SubString(1, data.Length - 1);
                 file.StreamBuffer.Write(binary.ToBytes(), 0, binary.Length);
                 file.Peek += binary.Length;
                 //logger.Info(file.Peek);
                 if (file.Peek >= file.Length)
                 {
                     file.Complete();
                     client.Send((int)Opcode.BINARY, new String2("File upload Success!!", Encoding.UTF8));
                 }
                 return;
             }
             if (type == (byte)FileMessageType.FileSearch || type == (byte)FileMessageType.FileListNotice)
             {
                 SendFileList(client, (FileMessageType)type);
                 return;
             }
             if (type == (byte)WorkType.WorkSearch || type == (byte)WorkType.WorkListNotice)
             {
                 SendWorkList(client, (WorkType)type);
             }
             //logger.Error("FileMessage type is wrong.");
             file.Init();
         }
     });
 }