Ejemplo n.º 1
0
    private void HandleWebsocketMessage(object sender, MessageEventArgs eventArgs)
    {
        var jsonPayload = eventArgs.Data;

        ExecuteOnMainThread(() => { RawMessageReceived?.Invoke(jsonPayload); });

        try
        {
            var message = SerializationUtils.DeserializeMessage(jsonPayload);

            if (message is ErrorMessage)
            {
                var cause = (message as ErrorMessage).Cause;
                ExecuteOnMainThread(() => { Error?.Invoke(new ErrorMessageException(cause)); });
            }
            else
            {
                ExecuteOnMainThread(() => { MessageReceived?.Invoke(message); });
            }
        }
        catch (Exception exception)
        {
            ExecuteOnMainThread(() => { Error?.Invoke(exception); });
        }
    }
Ejemplo n.º 2
0
        public void SendSystemMessage(string message, bool important = false)
        {
            var msg = new ChatMessage[] { LiveSplitMessage.Create(message, important) };

            MessageReceived?.Invoke(this, msg);
            RawMessageReceived?.Invoke(this, msg.First().Posted.ToString());
        }
Ejemplo n.º 3
0
        void Client_RawMessageReceived(object sender, IrcRawMessageEventArgs e)
        {
            if (e.Message.Command == "010")
            {
                Client.Disconnect();
                Connect("irc2.speedrunslive.com", Username, Password);
                return;
            }

            if (e.Message.Command == "433")
            {
                NicknameInUse?.Invoke(this, null);
            }

            if (e.Message.Source != null &&
                e.Message.Source.Name == "NickServ" &&
                e.Message.Command == "NOTICE")
            {
                if (e.Message.Parameters[1] == "Password accepted - you are now recognized.")
                {
                    Task.Factory.StartNew(() =>
                    {
                        foreach (var channel in ChannelsToJoin)
                        {
                            Client.Channels.Join(channel);
                        }
                    });
                }
                else if (e.Message.Parameters[1] == "Password incorrect.")
                {
                    PasswordIncorrect?.Invoke(this, null);
                }
            }

            if (e.Message.Command == "NOTICE" &&
                e.Message.Source != null &&
                e.Message.Source.Name == "RaceBot" &&
                e.Message.Parameters.Count > 1)
            {
                var text = e.Message.Parameters[1];
                if (text != null && !text.Contains("#srl"))
                {
                    MessageReceived?.Invoke(this, new Tuple <string, SRLIRCUser, string>(RaceChannelName, new SRLIRCUser("RaceBot", SRLIRCRights.Operator), text));
                }
            }

            RawMessageReceived?.Invoke(this, $"{e.Message.Command} - {e.Message.Parameters.Where(x => x != null).Aggregate((a, b) => a + " " + b)}");
        }
Ejemplo n.º 4
0
        public async void SendChannelMessage(string message)
        {
            message = message.Trim();
            message = message.Replace("\"", "\\\"");
            string data = TryCreateCommand(ref message) ? message : "{ \"action\": \"message\", \"data\": { \"message\":\"" + message + "\", \"guid\":\"" + Guid.NewGuid().ToString() + "\" } }";

            RawMessageReceived?.Invoke(this, data);

            ArraySegment <byte> bytesToSend = new ArraySegment <byte>(Encoding.UTF8.GetBytes(data));

            if (IsConnected)
            {
                try
                {
                    await ws.SendAsync(bytesToSend, WebSocketMessageType.Text, true, websocket_cts.Token);
                }
                catch
                {
                }
            }
        }
Ejemplo n.º 5
0
        void Client_RawMessageReceived(object sender, IrcRawMessageEventArgs e)
        {
            if (e.Message.Command == "010")
            {
                Client.Disconnect();
                Connect("irc2.speedrunslive.com", Username, Password);
                return;
            }

            if (e.Message.Command == "433")
            {
                NicknameInUse?.Invoke(this, null);
            }

            if (e.Message.Source != null &&
                e.Message.Source.Name == "NickServ" &&
                e.Message.Command == "NOTICE")
            {
                if (e.Message.Parameters[1] == "Password accepted - you are now recognized.")
                {
                    Task.Factory.StartNew(() =>
                    {
                        foreach (var channel in ChannelsToJoin)
                        {
                            Client.Channels.Join(channel);
                        }
                    });
                }
                else if (e.Message.Parameters[1] == "Password incorrect.")
                {
                    PasswordIncorrect?.Invoke(this, null);
                }
            }

            RawMessageReceived?.Invoke(this, $"{e.Message.Command} - {e.Message.Parameters.Where(x => x != null).Aggregate((a, b) => a + " " + b)}");
        }
Ejemplo n.º 6
0
 protected virtual void OnRawMessageReceived(ITwitchAccount account, TwitchChatMessage tcm)
 {
     RawMessageReceived?.Invoke(this, new ChatConnectionMessageReceivedEventArgs(account, tcm));
 }
Ejemplo n.º 7
0
        private void DataRecieved(IAsyncResult result)
        {
            if (!IsConnected || !Connection.Client.Connected)
            {
                return;
            }

            SocketError error;
            int         length = Connection.Client.EndReceive(result, out error) + _readBufferIndex;

            if (error != SocketError.Success)
            {
                Debug.WriteLine("ERROR: {0}", error.ToString());
                return;
            }

            _readBufferIndex = 0;
            while (length > 0)
            {
                int messageLength = Array.IndexOf(_readBuffer, (byte)'\n', 0, length);
                if (messageLength == -1)
                {
                    _readBufferIndex = length;
                    break;
                }
                messageLength++;

                string rawMessage = Encoding.UTF8.GetString(_readBuffer, 0, messageLength - 2);
                if (RawMessageReceived != null)
                {
                    RawMessageReceived.Invoke(this, new RawMessageEventArgs(rawMessage));
                }

                Message message = new Message(rawMessage);
                if (_handlers.ContainsKey(message.Command.ToUpper()))
                {
                    _handlers[message.Command.ToUpper()](this, message);
                }
                else
                {
                    Debug.WriteLine(String.Format("Missing handler for command {0} ({1})", message.Command.ToUpper(), message.RawData));
                }

                if (MessageReceived != null)
                {
                    MessageReceived.Invoke(this, new ProcessedMessageEventArgs(message));
                }

                Array.Copy(_readBuffer, messageLength, _readBuffer, 0, length - messageLength);
                length -= messageLength;
            }

            try {
                Connection.Client.BeginReceive(_readBuffer, _readBufferIndex, _readBuffer.Length - _readBufferIndex, SocketFlags.None, DataRecieved, null);
            } catch (SocketException e) {
                if (e.SocketErrorCode != SocketError.NotConnected && e.SocketErrorCode != SocketError.Shutdown)
                {
                    if (Error != null)
                    {
                        Error.Invoke(this, new ErrorEventArgs(e));
                    }

                    throw;
                }
            }
        }
Ejemplo n.º 8
0
        private async Task <bool> ReceiveAndProcess()
        {
            WebSocketReceiveResult result;
            string msg = "";

            byte[] buf = new byte[bufferSize];

            try
            {
                int maxBufferSize = RacetimeChannel.maxBufferSize;
                int read          = 0;
                int free          = buf.Length;


                do
                {
                    if (free < 1)
                    {
                        var newSize = buf.Length + (bufferSize);
                        if (newSize > maxBufferSize)
                        {
                            throw new InternalBufferOverflowException();
                        }
                        var newBuf = new byte[newSize];
                        Array.Copy(buf, 0, newBuf, 0, read);
                        buf  = newBuf;
                        free = buf.Length - read;
                    }
                    result = await ws.ReceiveAsync(new ArraySegment <byte>(buf, read, free), websocket_cts.Token);

                    if (websocket_cts.IsCancellationRequested)
                    {
                        return(false);
                    }
                    read += result.Count;
                    free -= result.Count;
                }while (!result.EndOfMessage);


                msg = Encoding.UTF8.GetString(buf, 0, read);
                RawMessageReceived?.Invoke(this, msg);
            }
            catch (InternalBufferOverflowException)
            {
                //flush socket
                while (!(result = await ws.ReceiveAsync(new ArraySegment <byte>(buf, 0, buf.Length), websocket_cts.Token)).EndOfMessage)
                {
                    ;
                }

                SendSystemMessage("Content too large to load");
                return(false);
            }
            catch
            {
                return(false);
            }

            RawMessageReceived?.Invoke(this, msg);

            IEnumerable <ChatMessage> chatmessages = Parse(JSON.FromString(msg));


            ChatMessage racemessage = chatmessages.FirstOrDefault(x => x.Type == MessageType.Race);

            if (racemessage != null)
            {
                UpdateRaceData((RaceMessage)racemessage);
            }

            var errormsg = chatmessages.FirstOrDefault(x => x.Type == MessageType.Error)?.Message;

            if (errormsg != null && string.Join("", errormsg).Contains("Permission denied"))
            {
                ForceReload();
                return(true);
            }
            else if (errormsg != null)
            {
                StateChanged?.Invoke(this, Race.State);
                UserListRefreshed?.Invoke(this, new EventArgs());
            }
            MessageReceived?.Invoke(this, chatmessages);
            return(true);
        }
Ejemplo n.º 9
0
 /// <summary>
 ///     Raises the <see cref="RawMessageReceived"/> event.
 /// </summary>
 /// <param name="e">The <see cref="EventArgs"/> instance holding the event data.</param>
 internal void OnRawMessageReceived(IrcRawMessageEventArgs e)
 {
     RawMessageReceived?.Invoke(this, e);
 }
Ejemplo n.º 10
0
 protected void OnRawMessageReceived(string message)
 {
     RawMessageReceived?.Invoke(this, new EventArgs());
 }
Ejemplo n.º 11
0
 protected virtual void OnRawMessageReceived(TwitchChatMessage msg)
 {
     RawMessageReceived?.Invoke(this, new TwitchChatMessageReceivedEventArgs(_credentials, msg));
 }
Ejemplo n.º 12
0
 /// <summary>
 /// Invokes RawMessageReceived event.
 /// </summary>
 protected void OnRawMessageReceived(NetInMessage message)
 {
     RawMessageReceived?.Invoke(this, new MessageReceivedEventArgs(message, RemoteId));
 }