Esempio n. 1
0
 public static MessageDirector Instance()
 {
     if (singleton == null)
     {
         singleton = new MessageDirector();
     }
     return(singleton);
 }
Esempio n. 2
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();
         }
     });
 }