Example #1
0
 public static extern void HP_Set_FN_Client_OnClose(IntPtr pListener, OnClose fn);
Example #2
0
 /// <summary>
 /// ē‚¹å‡»å–ę¶ˆęŒ‰é’®å›žč°ƒę­¤ę–¹ę³•
 /// </summary>
 private Task OnClickClose()
 {
     CurrentTime = Value;
     OnClose?.Invoke();
     return(Task.CompletedTask);
 }
Example #3
0
        private async Task receiveLoop()
        {
            byte[] buffer = new byte[websocket_package_size];
            while (true)
            {
                string json = string.Empty;
                System.Net.WebSockets.WebSocketReceiveResult result;
                try
                {
                    if (ws == null)
                    {
                        return;
                    }
                    if (ws.State != System.Net.WebSockets.WebSocketState.Open)
                    {
                        OnClose?.Invoke("");
                        return;
                    }
                    result = await ws.ReceiveAsync(new ArraySegment <byte>(buffer), src.Token);

                    json = Encoding.UTF8.GetString(buffer.Take(result.Count).ToArray());
                    if (TryParseJSON(json))
                    {
                        var message = JsonConvert.DeserializeObject <SocketMessage>(json);
                        if (message != null)
                        {
                            _receiveQueue.Add(message);
                        }
                    }
                    else
                    {
                        if (TryParseJSON(tempbuffer + json))
                        {
                            var message = JsonConvert.DeserializeObject <SocketMessage>(tempbuffer + json);
                            if (message != null)
                            {
                                _receiveQueue.Add(message);
                            }
                            tempbuffer = null;
                        }
                        else
                        {
                            if (!string.IsNullOrEmpty(tempbuffer))
                            {
                                Log.Debug("FAILED: " + json);
                            }
                            tempbuffer += json;
                        }
                        if (!string.IsNullOrEmpty(tempbuffer) && tempbuffer.Length > (websocket_package_size * 2))
                        {
                            tempbuffer = null;
                        }
                    }
                    await ProcessQueue();
                }
                catch (System.Net.WebSockets.WebSocketException ex)
                {
                    if (ws.State != System.Net.WebSockets.WebSocketState.Open)
                    {
                        OnClose?.Invoke(ex.Message);
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(json))
                        {
                            Log.Error(json);
                        }
                        Log.Error(ex, "");
                        await Task.Delay(3000);

                        await this.Close();
                    }
                }
                catch (Exception ex)
                {
                    if (ws.State != System.Net.WebSockets.WebSocketState.Open)
                    {
                        OnClose?.Invoke(ex.Message);
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(json))
                        {
                            Log.Error(json);
                        }
                        Log.Error(ex, "");
                        await Task.Delay(3000);

                        //await this.Close();
                    }
                }
            }
        }
Example #4
0
        private void Listen()
        {
            while (!dispose)
            {
                var buffers = new List <byte[]>();
                WebSocketReceiveResult msgInfo = null;

                try
                {
                    while (!msgInfo?.EndOfMessage ?? true)
                    {
                        var b = new ArraySegment <byte>(new byte[bufferSize]);

                        msgInfo = socket.ReceiveAsync(b, socketTokenSource.Token).Result;

                        var bArray = b.Array;

                        Array.Resize(ref bArray, msgInfo.Count);

                        buffers.Add(bArray);
                    }
                }
                catch (AggregateException ex)
                    when(ex.InnerException?.GetType() == typeof(TaskCanceledException))
                    {
                        _ = OnClose.InvokeAsync();

                        return;
                    }
                catch (Exception e1)
                {
                    OnError?.Invoke(e1);

                    if (!AutoReconnect)
                    {
                        return;
                    }

                    try
                    {
                        _ = socketTokenSource.Token.WaitHandle.WaitOne(1000);

                        ConnectAsync().Wait();
                    }
                    catch (Exception e2)
                    {
                        _ = OnReconnectFailed.InvokeAsync();
                        _ = OnError.InvokeAsync(e2);
                        _ = OnClose.InvokeAsync();
                    }

                    return;
                }

                var buffer = new List <byte>();

                foreach (var b in buffers)
                {
                    buffer.AddRange(b);
                }

                _ = Task.Run(() => HandleNewMessage(msgInfo, buffer.ToArray()));
            }

            _ = OnClose.InvokeAsync();
        }
Example #5
0
 public void Close()
 {
     OnClose?.Invoke(this, true);
     ws.Dispose();
 }
Example #6
0
 private void FormLogin_FormClosed(object sender, FormClosedEventArgs e)
 {
     OnClose?.Invoke();
 }
Example #7
0
 private void CloseError(Socket socket, Exception exception)
 {
     OnError.Invoke(this, exception);
     OnClose.Invoke(this, null);
     Console.WriteLine($"TCPClient: Unexpectadely disconnected. {exception.Message}");
 }
 /// <summary>
 /// Closes the modal and invokes the <see cref="OnClose"/> event.
 /// </summary>
 public void Cancel()
 {
     CloseModal?.Invoke();
     OnClose?.Invoke(ModalResult.Cancel(_modalType));
 }
 /// <summary>
 /// Closes the modal and invokes the <see cref="OnClose"/> event with the specified <paramref name="modalResult"/>.
 /// </summary>
 /// <param name="modalResult"></param>
 public void Close(ModalResult modalResult)
 {
     modalResult.ModalType = _modalType;
     CloseModal?.Invoke();
     OnClose?.Invoke(modalResult);
 }
Example #10
0
 public void SetAsLoaded()
 {
     // loading = false;
     OnClose?.Invoke();
 }
 private void cmdClose_Click(object sender, EventArgs e)
 {
     OnClose?.Invoke(this, EventArgs.Empty);
 }
Example #12
0
 public void InvokeClose()
 {
     Connected = false;
     OnClose?.Invoke();
 }
Example #13
0
 private void ClientDisconnectError(Socket socket, Exception exception)
 {
     OnError.Invoke(this, exception);
     OnClose.Invoke(this, socket);
     Console.WriteLine($"TCPServer: Client unexpectadely disconnected. {exception.Message}");
 }
Example #14
0
 /// <summary>
 /// ē‚¹å‡»å–ę¶ˆęŒ‰é’®å›žč°ƒę­¤ę–¹ę³•
 /// </summary>
 protected void OnClickClose()
 {
     CurrentTime = Value;
     OnClose?.Invoke();
 }
Example #15
0
 public virtual void Close(CloseReason reason)
 {
     OnClose?.Invoke(reason);
     CyberFuck.netPlay?.SendMessage(MessageContentType.CloseConnection, world.NetType == NetType.Server ? -1 : world.MyPlayerId, null);
 }
Example #16
0
 protected virtual void Close()
 {
     OnClose?.Invoke(this, EventArgs.Empty);
 }
Example #17
0
 public void Close()
 {
     OnClose?.Invoke();
 }
Example #18
0
 protected override void OnClosed(EventArgs e)
 {
     OnClose?.Invoke(this, e);
     base.OnClosed(e);
 }
Example #19
0
 public void Closed(IWebSocket webSocket, WebSocketClosedEventArgs args)
 {
     // OnClose.Invoke(args.Reason);
     OnClose?.Invoke(true); // TODO: detect if disconnect was clean
 }
Example #20
0
 public void Close()
 {
     isOpen = false;
     OnClose?.Invoke();
 }
Example #21
0
 protected void RaiseOnClose(bool result)
 {
     OnClose.SafeInvoke(result);
 }
Example #22
0
 public void ListenClose(OnClose onMultiCastMsg)
 {
     this.onClose = onMultiCastMsg;
 }
Example #23
0
 internal void InvokeOnClose()
 {
     OnClose?.Invoke();
 }
Example #24
0
        /// <summary>
        /// Dequeues all the messages from Discord and invokes appropriate methods. This will process the message and update the internal state before invoking the events. Returns the messages that were invoked and in the order they were invoked.
        /// </summary>
        /// <returns>Returns the messages that were invoked and in the order they were invoked.</returns>
        public IMessage[] Invoke()
        {
            //Dequeue all the messages and process them
            IMessage[] messages = connection.DequeueMessages();
            for (int i = 0; i < messages.Length; i++)
            {
                //Do a bit of pre-processing
                var message = messages[i];
                HandleMessage(message);

                //Invoke the appropriate methods
                switch (message.Type)
                {
                case MessageType.Ready:
                    if (OnReady != null)
                    {
                        OnReady.Invoke(this, message as ReadyMessage);
                    }
                    break;

                case MessageType.Close:
                    if (OnClose != null)
                    {
                        OnClose.Invoke(this, message as CloseMessage);
                    }
                    break;

                case MessageType.Error:
                    if (OnError != null)
                    {
                        OnError.Invoke(this, message as ErrorMessage);
                    }
                    break;

                case MessageType.PresenceUpdate:
                    if (OnPresenceUpdate != null)
                    {
                        OnPresenceUpdate.Invoke(this, message as PresenceMessage);
                    }
                    break;

                case MessageType.Subscribe:
                    if (OnSubscribe != null)
                    {
                        OnSubscribe.Invoke(this, message as SubscribeMessage);
                    }
                    break;

                case MessageType.Unsubscribe:
                    if (OnUnsubscribe != null)
                    {
                        OnUnsubscribe.Invoke(this, message as UnsubscribeMessage);
                    }
                    break;

                case MessageType.Join:
                    if (OnJoin != null)
                    {
                        OnJoin.Invoke(this, message as JoinMessage);
                    }
                    break;

                case MessageType.Spectate:
                    if (OnSpectate != null)
                    {
                        OnSpectate.Invoke(this, message as SpectateMessage);
                    }
                    break;

                case MessageType.JoinRequest:
                    if (OnJoinRequested != null)
                    {
                        OnJoinRequested.Invoke(this, message as JoinRequestMessage);
                    }
                    break;

                case MessageType.ConnectionEstablished:
                    if (OnConnectionEstablished != null)
                    {
                        OnConnectionEstablished.Invoke(this, message as ConnectionEstablishedMessage);
                    }
                    break;

                case MessageType.ConnectionFailed:
                    if (OnConnectionFailed != null)
                    {
                        OnConnectionFailed.Invoke(this, message as ConnectionFailedMessage);
                    }
                    break;

                default:
                    //This in theory can never happen, but its a good idea as a reminder to update this part of the library if any new messages are implemented.
                    Logger.Error("Message was queued with no appropriate handle! {0}", message.Type);
                    break;
                }
            }

            //Finally, return the messages
            return(messages);
        }
Example #25
0
 public void NetworkClose(ISocket socket)
 {
     Disconencted();
     OnClose?.Invoke(this);
 }
Example #26
0
 protected override void OnDisable()
 {
     base.OnDisable();
     OnClose?.Invoke();
 }
Example #27
0
 void ClearOnClose()
 {
     onClose = null;
 }
Example #28
0
 public void Cancel()
 {
     OnClose?.Invoke(ModalResult.Cancel());
 }
Example #29
0
 private void _onClose()
 {
     // stop the timer while normally closing the connection
     _timer.Stop();
     OnClose?.Invoke();
 }
Example #30
0
 public void Close(ModalResult modalResult)
 {
     OnClose?.Invoke(modalResult);
 }
Example #31
0
 public static extern void HP_Set_FN_Server_OnClose(IntPtr pListener, OnClose fn);
Example #32
0
 public override async Task CloseAsync()
 {
     client.Close();
     OnClose?.Invoke(this, new ChannelCloseEventArgs(Id));
     await Task.CompletedTask;
 }