Ejemplo n.º 1
0
 private void ReceiveCallback(IAsyncResult ar)
 {
     try
     {
         if (_receiveSocket.EndReceive(ar) > 1)
         {
             _buffer = new byte[BitConverter.ToInt32(_buffer, 0)];
             _receiveSocket.Receive(_buffer, _buffer.Length, SocketFlags.None);
             //everything is received, now we convert the data:
             string data = Encoding.Default.GetString(_buffer);
             // raise the received package data with appropriate context information
             PackageReceivedEventArgs eventArgs = new PackageReceivedEventArgs {
                 Data = data, Id = Id, Socket = _receiveSocket
             };
             PackageReceivedHandler.OnReceivePackage(this, eventArgs);
         }
         else
         {
             Disconnect();
         }
     }
     catch
     {
         if (!_receiveSocket.Connected)
         {
             Disconnect();
         }
         else
         {
             StartReceiving();
         }
     }
     StartReceiving();
 }
        public static void ReceivePackage(Object o, PackageReceivedEventArgs args)
        {
            Dictionary <string, object> pdict = JsonConvert.DeserializeObject <Dictionary <string, object> >(args.Data);

            if ((string)pdict["type"] == "REQUEST_STATUS")
            {
                //if the client is requesting the current state of the whiteboard
                Client       c      = ClientController.ClientList[0]; //the "host" client
                PacketSender sender = new PacketSender(c.Socket);
                sender.Send(args.Data);
            }
            else if ((string)pdict["type"] == "RESTORE")
            {
                //transfer the current state of the whiteboard to the client who
                //requested it
                int          id     = int.Parse(pdict["client_id"].ToString());
                Client       c      = ClientController.ClientList[id];
                PacketSender sender = new PacketSender(c.Socket);
                sender.Send(args.Data);
            }
            else
            {
                //if a new shape is drawn, we simply need to transfer it to the clients
                foreach (Client c in ClientController.ClientList)
                {
                    if (c.Id != args.Id)
                    {
                        PacketSender sender = new PacketSender(c.Socket);
                        sender.Send(args.Data);
                    }
                    else
                    {
                        continue;
                    }
                }
            }
        }