Ejemplo n.º 1
0
        //sends connect message
        public void SendConnectMessage()
        {
            ConnectMessagePacket connectPacket = new ConnectMessagePacket("user");
            MemoryStream         msgStream     = new MemoryStream();

            formatter.Serialize(msgStream, connectPacket);
            byte[] buffer = msgStream.GetBuffer();
            writer.Write(buffer.Length);
            writer.Write(buffer);
            writer.Flush();
            isConnected = true;
        }
Ejemplo n.º 2
0
        //processes server responce
        private void ProcessServerResponse()
        {
            int numberOfBytes;

            //if connected to server
            while (isConnected == true)
            {
                //read data from server and deserialize
                if ((numberOfBytes = reader.ReadInt32()) != 0)
                {
                    byte[]       buffer  = reader.ReadBytes(numberOfBytes);
                    MemoryStream _stream = new MemoryStream(buffer);
                    Packet       packet  = formatter.Deserialize(_stream) as Packet;

                    //if packet type is...
                    switch (packet.mPacketType)
                    {
                    //send message to all
                    case PacketType.chatMessage:
                        ChatMessagePacket chatPacket = (ChatMessagePacket)packet;
                        mClientForm.UpdateChatWindow(chatPacket.mSender + ": " + chatPacket.mMessage);
                        break;

                    //send disconnect message to all
                    case PacketType.disconnectMessage:
                        DisconnectMessagePacket disconnectPacket = (DisconnectMessagePacket)packet;
                        mClientForm.UpdateChatWindow(disconnectPacket.mSender + ": has disconnected.");
                        break;

                    //send connect message to all
                    case PacketType.connectMessage:
                        ConnectMessagePacket connectPacket = (ConnectMessagePacket)packet;
                        mClientForm.UpdateChatWindow(connectPacket.mSender + ": has connected.");
                        break;

                    //send private message to user
                    case PacketType.privateMessage:
                        PrivateMessagePacket privateMessagePacket = (PrivateMessagePacket)packet;
                        mClientForm.UpdateChatWindow(privateMessagePacket.mSender + "(" + privateMessagePacket.mReceiver + ")" + " Wispers: " + privateMessagePacket.mMessage);
                        break;
                    }
                }
            }

            reader.Close();
            writer.Close();
            tcpClient.Close();
        }
Ejemplo n.º 3
0
        //client method sends data from server program to client program
        private void ClientMethod(Client client)
        {
            Packet packet;

            try
            {
                //while client is reading data
                while ((packet = client.Read()) != null)
                {
                    switch (packet.mPacketType)
                    {
                    //send message to everyone
                    case PacketType.chatMessage:
                        ChatMessagePacket chatMessagePacket = (ChatMessagePacket)packet;
                        foreach (Client onlineClient in clients.Values)
                        {
                            onlineClient.Send(packet);
                        }
                        break;

                    //send disconnect message to everyone
                    case PacketType.disconnectMessage:
                        DisconnectMessagePacket disconnectPacket = (DisconnectMessagePacket)packet;
                        foreach (Client onlineClient in clients.Values)
                        {
                            onlineClient.Send(packet);
                        }
                        break;

                    //send connect message
                    case PacketType.connectMessage:
                        ConnectMessagePacket connectMessage = (ConnectMessagePacket)packet;
                        foreach (Client onlineClient in clients.Values)
                        {
                            onlineClient.Send(packet);
                        }
                        break;

                    //send private message to one user
                    case PacketType.privateMessage:
                        PrivateMessagePacket privateMessage = (PrivateMessagePacket)packet;
                        for (int i = 0; i < clients.Count(); i++)
                        {
                            if (i == privateMessage.mReceiver)
                            {
                                clients[i].Send(packet);
                            }
                        }
                        break;

                    default:
                        break;
                    }
                }
            }
            //if try catch fails send error message
            catch (Exception e)
            {
                Console.WriteLine("Error:" + e.Message);
            }
            //close client and remove from concurrent bag
            finally
            {
                for (int i = 0; i < clients.Count(); i++)
                {
                    //clients[i].Close();
                    clients.TryRemove(i, out client);
                }
            }
        }