1 /* 2 * Hunt - A refined core library for D programming language. 3 * 4 * Copyright (C) 2018-2019 HuntLabs 5 * 6 * Website: https://www.huntlabs.net/ 7 * 8 * Licensed under the Apache-2.0 License. 9 * 10 */ 11 12 module hunt.io.channel.Common; 13 14 import hunt.io.IoError; 15 import hunt.io.ByteBuffer; 16 import hunt.io.SimpleQueue; 17 import hunt.util.TaskPool; 18 import hunt.Functions; 19 import hunt.system.Memory; 20 21 import core.atomic; 22 import std.socket; 23 24 enum DataHandleStatus { 25 Done, 26 Pending 27 } 28 29 30 alias DataReceivedHandler = DataHandleStatus delegate(ByteBuffer buffer); 31 alias AcceptHandler = void delegate(Socket socket); 32 alias ErrorEventHandler = Action1!(IoError); 33 34 alias ConnectionHandler = void delegate(bool isSucceeded); 35 alias UdpDataHandler = void delegate(const(ubyte)[] data, Address addr); 36 37 @property TaskPool workerPool() @trusted { 38 import std.concurrency : initOnce; 39 40 __gshared TaskPool pool; 41 return initOnce!pool({ 42 return new TaskPool(defaultPoolThreads, true); 43 }()); 44 } 45 46 // __gshared bool useWorkerThread = false; 47 48 private shared uint _defaultPoolThreads = 0; 49 50 /** 51 These properties get and set the number of worker threads in the `TaskPool` 52 instance returned by `taskPool`. The default value is `totalCPUs` - 1. 53 Calling the setter after the first call to `taskPool` does not changes 54 number of worker threads in the instance returned by `taskPool`. 55 */ 56 @property uint defaultPoolThreads() @trusted { 57 const local = atomicLoad(_defaultPoolThreads); 58 return local < uint.max ? local : totalCPUs - 1; 59 } 60 61 /// Ditto 62 @property void defaultPoolThreads(uint newVal) @trusted { 63 atomicStore(_defaultPoolThreads, newVal); 64 } 65 66 67 /** 68 */ 69 interface Channel { 70 71 } 72 73 74 75 enum ChannelType : ubyte { 76 Accept = 0, 77 TCP, 78 UDP, 79 Timer, 80 Event, 81 File, 82 None 83 } 84 85 enum ChannelFlag : ushort { 86 None = 0, 87 Read, 88 Write, 89 90 OneShot = 8, 91 ETMode = 16 92 } 93 94 final class UdpDataObject { 95 Address addr; 96 ubyte[] data; 97 } 98 99 final class BaseTypeObject(T) { 100 T data; 101 } 102 103 104 105 // version (HUNT_IO_WORKERPOOL) { 106 // alias WritingBufferQueue = MagedNonBlockingQueue!ByteBuffer; 107 // } else { 108 // alias WritingBufferQueue = SimpleQueue!ByteBuffer; 109 // } 110 111 // alias WritingBufferQueue = MagedNonBlockingQueue!ByteBuffer; 112 alias WritingBufferQueue = SimpleQueue!ByteBuffer; 113 // alias WritingBufferQueue = MagedBlockingQueue!ByteBuffer; 114 115 /** 116 */ 117 Address createAddress(AddressFamily family = AddressFamily.INET, 118 ushort port = InternetAddress.PORT_ANY) { 119 if (family == AddressFamily.INET6) { 120 // addr = new Internet6Address(port); // bug on windows 121 return new Internet6Address("::", port); 122 } else 123 return new InternetAddress(port); 124 }