/// <summary> /// Adds a delegate method for a given Command type, e.g. EchoCommand -> OnEcho(..). /// </summary> /// <param name='type'> /// Type of the class, should implement ICommand, but not yet strict. /// </param> /// <param name='e'> /// E. is the OnReceiveDelegate method pointer /// </param> public virtual void AddReceiveEventHandler(Type type, OnReceiveDelegate e) { if (!typeof(ICommand).IsAssignableFrom(type)) { throw new ArgumentException(String.Format( "Illegal command type specified {0}, must be of type {1}", type, typeof(ICommand)) ); } HandlerCommandEvent co = null; if (EventHandlerCommands.ContainsKey(type)) { co = (HandlerCommandEvent)EventHandlerCommands [type]; } else { co = new HandlerCommandEvent(); EventHandlerCommands.Add(type, e); } co.ReceiveEvent += e; co.Type = type; }
public AsynchSocketManager(OnConnectDelegate OnConnect, OnDisconnectDelegate OnDisconnect, OnReceiveDelegate OnReceive) { // copy the delegates this.OnReceive = OnReceive; this.OnConnect = OnConnect; this.OnDisconnect = OnDisconnect; Id++; // increment our "global" id - could use 'real' quid's here... MyId = Id; // save the value locally StartTime = DateTime.Now.Ticks; }
public UDPSocket(IPEndPoint localEndPoint, int packetSize, bool dontFragment, short ttl, OnReceiveDelegate onReceive, OnStopDelegate onStop) { m_PacketSize = packetSize; m_Disposed = false; m_OnReceive = onReceive; m_OnStop = onStop; m_SendFifo = new Queue <PacketBuffer>(); m_ReceiveFifo = new AutoPumpQueue <PacketBuffer>( (sender, data) => { bool isDisposed = false; lock (m_Sync) { isDisposed = m_Disposed; } if (!isDisposed) { m_OnReceive(this, (IPEndPoint)data.EndPoint, data.Data); } } ); m_SendPending = false; m_ReceivePending = 0; m_IPv6 = (localEndPoint.AddressFamily == AddressFamily.InterNetworkV6); m_Socket = new Socket(localEndPoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp); m_Socket.EnableBroadcast = true; m_Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1); m_Socket.SendBufferSize = 65536; m_Socket.ReceiveBufferSize = 65536; if (!m_IPv6) { m_Socket.DontFragment = dontFragment; } if (ttl >= 0) { m_Socket.Ttl = ttl; } m_Socket.Bind(localEndPoint); m_LocalEndPoint = m_Socket.LocalEndPoint; m_Socket.IOControl((IOControlCode)SIO_UDP_CONNRESET, new byte[] { 0, 0, 0, 0 }, null); BeginReceive(); }
/// <summary> /// Attempts to execute the delegate method, either asynchronously, or synchronous. /// /// Will pass the command, and client socket instances to the delegate method. /// /// </summary> /// <returns> /// The receive event handler. /// </returns> /// <param name='client'> /// An instnace of the IClientSocket interface. /// </param> /// <param name='command'> /// The command object, should correspond to the type of delegate method handled, e.g. EchoCommand. /// </param> /// <param name='async'> /// If set to <c>true</c> then execute asynchronously. /// </param> protected virtual bool AttemptReceiveEventHandler(IClientSocket client, ICommand command, bool async) { if (command == null) { Debug.WriteLine(String.Format( "Invalid command was received, null pointer for client {0}", client ) ); // TODO error on socket?? return(false); } Type type = command.GetType(); // Object serial = client.BufferCodec.BuildSerial (type); if (!EventHandlerCommands.ContainsKey(type)) { Debug.WriteLine(String.Format( "Command {0} with serial {1} received for Socket {2} on " + "Server type {3} but no OnReceiveDelegate event handler found for command type", command, type, client, this) ); return(false); } OnReceiveDelegate deleg = (OnReceiveDelegate)EventHandlerCommands [type]; if (async) { deleg.BeginInvoke( client, command, BeginInvokeReceiveEventHandlerCallback, deleg ); } else { deleg.Invoke(client, command); } return(true); }
/// <summary> /// Sets the on receive event. /// </summary> /// <param name="aDelegate">The Event Delegate.</param> public void SetOnReceive(OnReceiveDelegate aDelegate) { OnReceiveDelegate += aDelegate; }
/// <summary> /// Begins the invoke receive event handler callback, ending the invocation. /// </summary> /// <param name='result'> /// Result. /// </param> protected virtual void BeginInvokeReceiveEventHandlerCallback(IAsyncResult result) { OnReceiveDelegate deleg = (OnReceiveDelegate)result.AsyncState; deleg.EndInvoke(result); }