Ejemplo n.º 1
0
 public byte[] Serialize(Message msg)
 {
     if(msg == null)
     {
         throw new ArgumentNullException("msg");
     }
     MessageEncoder encoder;
     if(!m_encoders.TryGetValue(msg.Type, out encoder))
     {
         throw new NotImplementedException("Message type not supported: " + msg.Type);
     }
     MemoryStream s = new MemoryStream();
     BinaryWriter writer = new BinaryWriter(s);
     // write placeholder for message size
     writer.Write((int)0);
     // write message type
     writer.Write((int)msg.Type);
     // write message body
     encoder(writer, msg);
     writer.Flush();
     // write message size
     writer.Seek(0, SeekOrigin.Begin);
     writer.Write((int)s.Length);
     writer.Flush();
     return s.ToArray();
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Send a message to the server, changing its sender. It will be handled in the server's thread.
 /// </summary>
 public void EnqueueMessage(Message message, object sender)
 {
     if(message != null && sender != null)
     {
         message.Sender = sender;
     }
     m_queue.Enqueue(message);
 }
Ejemplo n.º 3
0
        protected virtual void OnMessageReceived(Message msg)
        {
            if(m_receiveQueue != null)
            {
                m_receiveQueue.Enqueue(msg);
            }

            EventHandler<MessageEventArgs> handler = MessageReceived;
            if (handler != null)
            {
                handler(this, new MessageEventArgs(msg));
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Send a message to the client (blocking).
 /// </summary>
 public void SendMessage(Message message)
 {
     m_socket.Send(m_serializer.Serialize(message));
 }
Ejemplo n.º 5
0
 private void SerializePopBalloon(BinaryWriter writer, Message msg)
 {
     PopBalloonMessage pbm = (PopBalloonMessage)msg;
     writer.Write(pbm.BalloonID);
 }
 private void SerializeChangeScreen(BinaryWriter writer, Message msg)
 {
     ChangeScreenMessage csm = (ChangeScreenMessage)msg;
     writer.Write(csm.BalloonID);
     writer.Write((int)csm.Direction);
     writer.Write(csm.Y);
     writer.Write(csm.Velocity.X);
     writer.Write(csm.Velocity.Y);
 }
 private void SerializeBalloonContentUpdate(BinaryWriter writer, Message msg)
 {
     BalloonContentUpdateMessage bcm = (BalloonContentUpdateMessage)msg;
     writer.Write(bcm.BalloonID);
     writer.Write((int)bcm.BalloonType);
     writer.Write(bcm.Label == null ? "" : bcm.Label);
     writer.Write(bcm.Content == null ? "" : bcm.Content);
     writer.Write(bcm.Url == null ? "" : bcm.Url);
     writer.Write(bcm.ImageUrl == null ? "" : bcm.ImageUrl);
 }
 private void LogMessage(Message msg, string direction)
 {
     if((m_logFormatter != null) && (msg != null))
     {
         try
         {
             string line = m_logFormatter.Format(msg);
             Trace.WriteLine(String.Format("{0} {1}", direction, line));
         }
         catch(Exception)
         {
         }
     }
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Send a message to the server. It will be handled in the server's thread.
 /// </summary>
 public void EnqueueMessage(Message message)
 {
     m_queue.Enqueue(message);
 }
 private void FormatBalloonContentUpdate(JArray args, Message m)
 {
     BalloonContentUpdateMessage bcm = (BalloonContentUpdateMessage)m;
     args.Add(JValue.FromObject(bcm.BalloonID));
     args.Add(JValue.FromObject((int)bcm.BalloonType));
     args.Add(JValue.CreateString(bcm.Label));
     args.Add(JValue.CreateString(bcm.Content));
     args.Add(JValue.CreateString(bcm.Url));
     args.Add(JValue.CreateString(bcm.ImageUrl));
 }
 private void FormatBalloon(JArray args, Message m)
 {
     BalloonMessage bm = (BalloonMessage)m;
     args.Add(JValue.FromObject(bm.BalloonID));
 }
 public byte[] Serialize(Message msg)
 {
     string line = Format(msg);
     if(Configuration.LogNetworkMessages)
     {
         Trace.WriteLine(String.Format(">> {0}", line));
     }
     return m_encoding.GetBytes(line + "\n");
 }
Ejemplo n.º 13
0
 /// <summary>
 /// Handle a message. Must be called from the server's thread.
 /// </summary>
 /// <returns>
 /// True if the message has been handled, false if messages should stop being processed.
 /// </returns>
 private bool HandleMessage(Message msg)
 {
     if(msg == null)
     {
         return false;
     }
     switch(msg.Type)
     {
     case MessageType.Connected:
         return HandleScreenConnected((ConnectedMessage)msg);
     case MessageType.Disconnected:
         return HandleScreenDisconnected((DisconnectedMessage)msg);
     case MessageType.ChangeScreen:
         return HandleChangeScreen((ChangeScreenMessage)msg);
     case MessageType.NewBalloon:
         return HandleNewBalloon((NewBalloonMessage)msg);
     case MessageType.NewPlane:
         return HandleNewPlane((NewPlaneMessage)msg);
     case MessageType.PopObject:
         return HandlePopObject((PopObjectMessage)msg);
     case MessageType.GetBalloonContent:
         return HandleGetBalloonContent((GetBalloonContentMessage)msg);
     case MessageType.GetBalloonState:
         return HandleGetBalloonState((GetBalloonStateMessage)msg);
     case MessageType.BalloonContentUpdate:
         return HandleBalloonContentUpdate((BalloonContentUpdateMessage)msg);
     case MessageType.BalloonStateUpdate:
         return HandleBalloonStateUpdate((BalloonStateUpdateMessage)msg);
     case MessageType.FeedUpdated:
         return HandleFeedUpdated((FeedUpdatedMessage)msg);
     default:
         // warn about unknown messages
         Trace.WriteLine(String.Format("Warning: message type not handled by server: {0}",
                                       msg.Type));
         return true;
     }
 }
Ejemplo n.º 14
0
 private void SerializeNewPlane(BinaryWriter writer, Message msg)
 {
     NewPlaneMessage npm = (NewPlaneMessage)msg;
     writer.Write(npm.ObjectID);
     writer.Write((int)npm.PlaneType);
     writer.Write((int)npm.Direction);
     writer.Write(npm.Y);
     writer.Write(npm.Velocity.X);
     writer.Write(npm.Velocity.Y);
     writer.Write(npm.Time);
 }
Ejemplo n.º 15
0
 private void SerializeBalloon(BinaryWriter writer, Message msg)
 {
     ObjectMessage bm = (ObjectMessage)msg;
     writer.Write(bm.ObjectID);
 }
Ejemplo n.º 16
0
 /// <summary>
 /// Handles a message. Must be called from the screen's thread.
 /// </summary>
 /// <returns>
 /// True if the message has been handled, false if messages should stop being processed.
 /// </returns>
 private bool HandleMessage(Message msg)
 {
     if(msg == null)
     {
         return false;
     }
     switch(msg.Type)
     {
     case MessageType.NewBalloon:
         NewBalloonMessage am = (NewBalloonMessage)msg;
         m_bubbles[am.BalloonID] = m_server.GetBubble(am.BalloonID);
         m_bubbles[am.BalloonID].Screen = this;
         m_connection.SendMessage(am);
         return true;
     case MessageType.ChangeScreen:
         ChangeScreenMessage csm = (ChangeScreenMessage)msg;
         m_bubbles.Remove(csm.BalloonID);
         m_server.EnqueueMessage(csm, this);
         return true;
     case MessageType.PopBalloon:
         PopBalloonMessage pbm = (PopBalloonMessage)msg;
         if(pbm.Sender is ScreenConnection)
         {
             m_server.EnqueueMessage(pbm);   // Notify server
         }
         else
         {
             m_connection.SendMessage(pbm);  // Notify physical screen
         }
         return true;
     default:
         // Disconnect when receiving unknown messages
         return false;
     }
 }
Ejemplo n.º 17
0
 /// <summary>
 /// Handle a message. Must be called from the server's thread.
 /// </summary>
 /// <returns>
 /// True if the message has been handled, false if messages should stop being processed.
 /// </returns>
 private bool HandleMessage(Message msg)
 {
     if(msg == null)
     {
         return false;
     }
     switch(msg.Type)
     {
     case MessageType.Connected:
         return HandleScreenConnected((ConnectedMessage)msg);
     case MessageType.Disconnected:
         return HandleScreenDisconnected((DisconnectedMessage)msg);
     case MessageType.ChangeScreen:
         return HandleChangeScreen((ChangeScreenMessage)msg);
     case MessageType.NewBalloon:
         return HandleNewBalloon((NewBalloonMessage)msg);
     case MessageType.PopBalloon:
         return HandlePopBalloon((PopBalloonMessage)msg);
     default:
         // Disconnect when receiving unknown messages
         return false;
     }
 }
 private void FormatBalloonStateUpdate(JArray args, Message m)
 {
     BalloonStateUpdateMessage bdm = (BalloonStateUpdateMessage)m;
     args.Add(JValue.FromObject(bdm.BalloonID));
     args.Add(JValue.FromObject((int)bdm.OverlayType));
     args.Add(JValue.FromObject(bdm.BackgroundColor.Red));
     args.Add(JValue.FromObject(bdm.BackgroundColor.Green));
     args.Add(JValue.FromObject(bdm.BackgroundColor.Blue));
     args.Add(JValue.FromObject(bdm.BackgroundColor.Alpha));
     args.Add(JValue.FromObject(bdm.Votes));
 }
 private void FormatChangeScreen(JArray args, Message m)
 {
     ChangeScreenMessage csm = (ChangeScreenMessage)m;
     args.Add(JValue.FromObject(csm.BalloonID));
     args.Add(JValue.CreateString(Balloon.FormatDirection(csm.Direction)));
     args.Add(JValue.FromObject(csm.Y));
     args.Add(JValue.FromObject(csm.Velocity.X));
     args.Add(JValue.FromObject(csm.Velocity.Y));
 }
 private void FormatNewBalloon(JArray args, Message m)
 {
     NewBalloonMessage nbm = (NewBalloonMessage)m;
     args.Add(JValue.FromObject(nbm.BalloonID));
     args.Add(JValue.CreateString(Balloon.FormatDirection(nbm.Direction)));
     args.Add(JValue.FromObject(nbm.Y));
     args.Add(JValue.FromObject(nbm.Velocity.X));
     args.Add(JValue.FromObject(nbm.Velocity.Y));
 }
 private void SerializeBalloon(BinaryWriter writer, Message msg)
 {
     BalloonMessage bm = (BalloonMessage)msg;
     writer.Write(bm.BalloonID);
 }
 public string Format(Message msg)
 {
     MessageFormatter formatter;
     if(!m_formatters.TryGetValue(msg.TypeTag, out formatter))
     {
         throw new Exception("Unsupported type: " + msg.TypeTag);
     }
     JArray args = new JArray();
     args.Add(JValue.CreateString(msg.TypeTag));
     formatter(args, msg);
     return args.ToString(Formatting.None);
 }
 private void SerializeBalloonStateUpdate(BinaryWriter writer, Message msg)
 {
     BalloonStateUpdateMessage bdm = (BalloonStateUpdateMessage)msg;
     writer.Write(bdm.BalloonID);
     writer.Write((int)bdm.OverlayType);
     writer.Write(bdm.BackgroundColor.Red);
     writer.Write(bdm.BackgroundColor.Green);
     writer.Write(bdm.BackgroundColor.Blue);
     writer.Write(bdm.BackgroundColor.Alpha);
     writer.Write(bdm.Votes);
 }
Ejemplo n.º 24
0
 public MessageEventArgs(Message message)
 {
     m_message = message;
 }
 private void SerializeNewBalloon(BinaryWriter writer, Message msg)
 {
     NewBalloonMessage nbm = (NewBalloonMessage)msg;
     writer.Write(nbm.BalloonID);
     writer.Write((int)nbm.Direction);
     writer.Write(nbm.Y);
     writer.Write(nbm.Velocity.X);
     writer.Write(nbm.Velocity.Y);
 }
Ejemplo n.º 26
0
 /// <summary>
 /// Handles a message. Must be called from the screen reader's thread.
 /// </summary>
 /// <returns>
 /// True if the message has been handled, false if messages should stop being processed.
 /// </returns>
 private bool HandleMessage(Message msg)
 {
     if(msg == null)
     {
         return false;
     }
     else if(msg.Type == MessageType.RefreshFeed)
     {
         DoRefresh();
     }
     else
     {
         // warn about unknown messages
         Trace.WriteLine(String.Format("Warning: message type not handled by feed: {0}",
                                       msg.Type));
     }
     return true;
 }