/// <summary> /// Create a new udp connection to handle communication to a single endpoint from the specified socket. /// </summary> public UdpConnection(UdpServer server, Socket socket, IPEndPoint remoteEndpoint) { // persist the server Server = server; RemoteEndPoint = remoteEndpoint; _timeoutTimer = new Timer(_timeoutMilliseconds, ActionSet.New(ProcessError, new TimeoutException("UdpConnection timeout."))); _header = new UdpHeader(); _header.Compression = _defaultCompression = DecompressionMethods.None; // create the encoder _encoder = Encoding.UTF8.GetEncoder(); _lock = new Lock(); _message = new UdpMessage(this); _messages = new ArrayRig <UdpMessage>(); _onMessage = new ActionPop <UdpMessage>(); _errors = new ArrayRig <Exception>(); _onError = new ActionPop <Exception>(); // create an async socket to manage the socket AsyncSocket = new AsyncUdpSocket(socket, (IPEndPoint)socket.LocalEndPoint, OnReceived, ProcessError); AsyncSocket.TargetEndPoint = RemoteEndPoint; }
/// <summary> /// Create a new incoming udp connection to handle communication to a single endpoint. /// </summary> public UdpConnection(UdpServer server, IPEndPoint remoteEndpoint, IPEndPoint localEndpoint, byte[] buffer, int count) { // persist the server Server = server; RemoteEndPoint = remoteEndpoint; _timeoutTimer = new Timer(_timeoutMilliseconds, ActionSet.New(ProcessError, new TimeoutException("UdpConnection timeout."))); _header = new UdpHeader(); _header.Compression = _defaultCompression = DecompressionMethods.None; // create the encoder _encoder = Encoding.UTF8.GetEncoder(); _lock = new Lock(); _message = new UdpMessage(this); _messages = new ArrayRig <UdpMessage>(); _onMessage = new ActionPop <UdpMessage>(); _errors = new ArrayRig <Exception>(); _onError = new ActionPop <Exception>(); // call on received for the first buffer OnReceived(RemoteEndPoint, buffer, count); // create a socket instance for the udp connection var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); // create an async socket to manage the socket AsyncSocket = new AsyncUdpSocket(socket, localEndpoint, OnReceived, ProcessError); AsyncSocket.TargetEndPoint = RemoteEndPoint; }
//----------------------------------// /// <summary> /// Create a new server listening to the specified endpoint. /// </summary> public UdpServer(IPEndPoint localEndpoint, string name = "Efz") { _connections = new Shared <Dictionary <IPEndPoint, UdpConnection> >(new Dictionary <IPEndPoint, UdpConnection>()); _onConnection = new ActionPop <UdpConnection>(); _name = name; _stopped = true; _flags = SocketFlags.None; LocalEndpoint = localEndpoint; // create the server socket _socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); AsyncSocket = new AsyncUdpSocket(_socket, LocalEndpoint, OnReceive, OnError); }