Esempio n. 1
0
 /// <summary>
 /// Trigger the OnException event.
 /// </summary>
 /// <param name="socket">Socket the event originated from.</param>
 /// <param name="exception">Exception that triggered the event.</param>
 public virtual ExceptionHandlerResponse OnException(EzSocket socket, Exception exception)
 {
     if (OnExceptionHandler == null)
     {
         return(ExceptionHandlerResponse.CloseSocket);
     }
     return(OnExceptionHandler(socket, exception));
 }
Esempio n. 2
0
        /// <summary>
        /// Listen on port and accept connections.
        /// </summary>
        /// <param name="port">Port to listen to.</param>
        public void Listen(int port)
        {
            // create listener and start
            Port      = port;
            _listener = new TcpListener(IPAddress.Any, port);
            _listener.Start();

            // accept connections in endless loop until set to false
            IsListening = true;
            while (IsListening)
            {
                var client = _listener.AcceptTcpClient();
                var sock   = new EzSocket(client, EventsListener);
                // ^ while we don't do anything with 'sock' here, it will trigger event internally.
                // the user should use it from there.
            }

            // stop listening and delete listener
            Port = 0;
            _listener.Stop();
            _listener.Server.Close();
            _listener = null;
        }
Esempio n. 3
0
 /// <summary>
 /// Trigger the OnConnectionClosed event.
 /// </summary>
 /// <param name="socket">Socket the event originated from.</param>
 public virtual void OnConnectionClosed(EzSocket socket)
 {
     OnConnectionClosedHandler?.Invoke(socket);
 }
Esempio n. 4
0
 /// <summary>
 /// Trigger the OnNewConnection event.
 /// </summary>
 /// <param name="socket">Socket the event originated from.</param>
 public virtual void OnNewConnection(EzSocket socket)
 {
     OnNewConnectionHandler?.Invoke(socket);
 }
Esempio n. 5
0
 /// <summary>
 /// Trigger the OnMessageRead event.
 /// </summary>
 /// <param name="socket">Socket the event originated from.</param>
 /// <param name="data">Data read.</param>
 public virtual void OnMessageRead(EzSocket socket, byte[] data)
 {
     OnMessageReadHandler?.Invoke(socket, data);
 }
Esempio n. 6
0
 /// <summary>
 /// Trigger the OnDataSend event.
 /// </summary>
 /// <param name="socket">Socket the event originated from.</param>
 /// <param name="data">Data sent.</param>
 public virtual void OnDataSend(EzSocket socket, byte[] data)
 {
     OnDataSendHandler?.Invoke(socket, data);
 }