public void ServerStart()
        {
            ThreadPool.QueueUserWorkItem((c) =>
            {
                while (true)
                {
                    Socket client = null;
                    try
                    {
                        client = Accept();
                        ThreadPool.QueueUserWorkItem((temp_client) =>
                        {
                            var _client = temp_client as Socket;
                            try
                            {
                                String2 buffer = new String2(Define.BUFFER_SIZE);
                                _client.Receive(buffer.ToBytes(), buffer.Length, SocketFlags.None);
                                if (buffer.IsEmpty())
                                {
                                    _client.Close();
                                    return;
                                }
                                Request req = new Request(this, buffer);
                                if (req.IsWebSocket())
                                {
                                    socketlist.Add(new WebSocket(client, this, req, _websocket_method_list));
                                    return;
                                }
                                Response res = new Response(this, req);
                                String2 url  = req.Uri;
                                if (_default != null && url.ToString().Equals("/"))
                                {
                                    url += _default;
                                }

                                if (_method_list.ContainsKey(url))
                                {
                                    _method_list[url](req, res);
                                }
                                else if (zipmap.ContainsKey(url))
                                {
                                    String extension = url.SubString(url.Length - 4, 4).ToUpper().ToString();
                                    if (".JS".Equals(extension.Substring(1)))
                                    {
                                        res.ContextType = "text/javascript; charset=UTF-8";
                                    }
                                    else if (".CSS".Equals(extension))
                                    {
                                        res.ContextType = "text/css; charset=UTF-8";
                                    }
                                    res.Body = zipmap[url];
                                }
                                else if (_rootpath != null)
                                {
                                    string filepath = _rootpath + url.ToString();
                                    if (File.Exists(filepath))
                                    {
                                        res.ReadFile(filepath);
                                    }
                                }
                                String2 sendbuffer = res.View();
                                client.Send(sendbuffer.ToBytes(), sendbuffer.Length, SocketFlags.None);
                                ThreadPool.QueueUserWorkItem((_) =>
                                {
                                    Thread.Sleep(5000);
                                    _client.Close();
                                });
                            }
                            catch (Exception e)
                            {
                                if (_client != null)
                                {
                                    _client.Dispose();
                                }
                                Console.WriteLine(e);
                            }
                        }, client);
                    }
                    catch (Exception e)
                    {
                        if (client != null)
                        {
                            client.Dispose();
                        }
                        Console.WriteLine(e);
                    }
                }
            });
        }
Esempio n. 2
0
        public static void Run(IWorkSocketClient client, byte opcode, String2 data)
        {
            SendWorkTemp(client, "default", DateTime.Now.ToString("yyyy_MM_dd") + "_業務報告");
            SendFileList(client, FileMessageType.FileSearch);
            SendWorkList(client, WorkType.WorkSearch);

            if (file.Open && opcode != (int)Opcode.BINARY)
            {
                //logger.Error("It's error what transfer the file.");
                file.Init();
            }
            if (opcode == (int)Opcode.MESSAGE)
            {
                IDictionary <String, String> messageBuffer = JsonConvert.DeserializeObject <Dictionary <String, String> >(data.ToString());
                if (String.Equals(messageBuffer["TYPE"], "1"))
                {
                    WebSocketMessageBuilder builder = WebSocketMessageBuilder.GetMessage(MessageType.MESSAGE);
                    String chatMessage = client.SocketClient.RemoteEndPoint + "-" + messageBuffer["MESSAGE"];
                    builder.SetMessage(chatMessage);
                    String2 message = builder.Build();
                    Console.WriteLine(message);

                    /*foreach (WebSocketServer client1 in clientlist)
                     * {
                     *  client1.Send((int)OPCODE.MESSAGE, message);
                     * }*/
                    //logger.Info(message);
                }
                else if (String.Equals(messageBuffer["TYPE"], "4"))
                {
                    FileInfo info  = new FileInfo(Program.WORK_PATH + Path.DirectorySeparatorChar + messageBuffer["WORKTITLE"]);
                    String2  data1 = new String2(messageBuffer["MESSAGE"], Encoding.UTF8);
                    using (FileStream stream = new FileStream(info.FullName, FileMode.Create, FileAccess.Write))
                    {
                        //data1.WriteStream(stream);
                        stream.Write(data1.ToBytes(), 0, data1.Length);
                    }
                    SendWorkList(client, WorkType.WorkListNotice);
                }
                else if (String.Equals(messageBuffer["TYPE"], "5"))
                {
                    String data1 = messageBuffer["MESSAGE"];
                    SendWorkTemp(client, data1.Trim(), data1.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);
                    //continue;
                }
                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();
                        //continue;
                    }
                    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));
                    }
                    //continue;
                }
                if (type == (byte)FileMessageType.FileSearch || type == (byte)FileMessageType.FileListNotice)
                {
                    SendFileList(client, (FileMessageType)type);
                    //continue;
                }
                if (type == (byte)WorkType.WorkSearch || type == (byte)WorkType.WorkListNotice)
                {
                    SendWorkList(client, (WorkType)type);
                }
                //logger.Error("FileMessage type is wrong.");
                file.Init();
            }
        }