public void ServerStart() { ThreadPool.QueueUserWorkItem((c) => { while (true) { Socket client = null; try { client = Accept(); ThreadPool.QueueUserWorkItem((temp_client) => { var _client = temp_client as Socket; String2 buffer = new String2(Define.BUFFER_SIZE); _client.Receive(buffer.ToBytes(), buffer.Length, SocketFlags.None); if (buffer.IsEmpty()) { logger.Debug("not Byte data.."); //TODO : Bug?? _client.Close(); return; } Request req = new Request(buffer); Console.WriteLine(req.Uri); if (req.IsWebSocket()) { socketlist.Add(new WebSocket(client, this, req, _websocket_method_list)); return; } Response res = new Response(); Console.WriteLine(req.Uri); if (_method_list.ContainsKey(req.Uri)) { _method_list[req.Uri](req, res); } else if (_rootpath != null) { string filepath = _rootpath + req.Uri.ToString(); if (File.Exists(filepath)) { res.ReadFile(filepath); } } String2 sendbuffer = TransResponse(res); client.Send(sendbuffer.ToBytes(), sendbuffer.Length, SocketFlags.None); _client.Close(); }, client); } catch (Exception e) { if (client != null) { client.Dispose(); } throw e; } } }); }
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); } } }); }