예제 #1
0
        protected void OnDataRecived(WebSocketSharp.MessageEventArgs e, string id)
        {
            string datasocketId = ToDataSocketID(id);

            TotalMessageRecivedEvents++;
            MemoryStream ms = new MemoryStream(e.RawData);

            WebsocketPipeMessageInfo[] msgs;

            System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
            watch.Start();
            try
            {
                msgs = DataSocket.ReadMessages(ms).ToArray();
            }
            catch (Exception ex)
            {
                string msg = "Error while reading messages from data socket: " + ex.Message;
                WriteLogMessage(id, msg);
                throw new Exception(msg, ex);
            }

            watch.Stop();
            WriteLogTimeMessage(id, "Read from datasocket", watch.Elapsed.TotalMilliseconds);

            ms.Close();
            ms.Dispose();
            ms = null;

            watch.Reset();
            watch.Start();
            int bytecount = 0;

            MessageEventArgs[] mes = msgs.Select(msg =>
            {
                bytecount = bytecount + msg.Data.Length;
                TMessage o;
                try
                {
                    o = Serializer.FromBytes(msg.Data);;
                }
                catch (Exception ex)
                {
                    var str = "Error desrializing message. " + ex.Message;
                    WriteLogMessage(id, str);
                    throw new Exception(str, ex);
                }

                return(new MessageEventArgs(o, msg.NeedsResponse, id));
            }).ToArray();

            WriteLogTimeMessage(id, "Deserialzed " + msgs.Length + " messages with " + bytecount + " [bytes] ", watch.Elapsed.TotalMilliseconds);

            watch.Stop();
            watch.Reset();
            watch.Start();
            foreach (var me in mes)
            {
                if (TriggerWaitHandle(datasocketId, me.Message)) // this message is a response.
                {
                    continue;
                }

                OnMessage(me);
            }
            watch.Stop();
            WriteLogTimeMessage(id, "Handled evnets for " + msgs.Length + " messages", watch.Elapsed.TotalMilliseconds);
        }