Ejemplo n.º 1
0
        void acceptCompleted(Socket accepted)
        {
            // Send a ConnectionAccepted.

            try
            {
                ClientRepresentation client = new ClientRepresentation(this)
                {
                    ClientSocket = accepted
                };

                connectedClients.Add(client);

                client.MessageReceived += ProcessMessageBytes;
                client.SendBytes(SocketPipeHelper.BuildMessage(MessageType.ConnectionAccepted, null));

                ClientConnected?.Invoke(this);

                client.AwaitBytes();
            }
            finally
            {
                if (!isStopped)
                {
                    serverAccept();
                }
            }
        }
Ejemplo n.º 2
0
        public void PushMessage(BaseMessage msg)
        {
            logger.Info($"PushMessage({msg.GetType().Name})");

            IFormatter formatter = new BinaryFormatter();

            using (MemoryStream stream = new MemoryStream())
            {
                formatter.Serialize(stream, msg);

                byte[] arr = stream.ToArray();

                byte[] message = SocketPipeHelper.BuildMessage(msg is ClientToClientMessage ? MessageType.BroadcastMessage : MessageType.Message, arr);
                client.SendBytes(message);
            }
        }