예제 #1
0
        /// <summary>
        /// When User Give Buzz to abuddy.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBuZZ_Click(object sender, EventArgs e)
        {
            ClientMsg m = new ClientMsg();

            m.Type = (int)ClientMsgType.Buzz;
            m.Info = client;
            m.From = Program.app.myInfo.ClientID;
            Program.app.client.L.Send(Program.app.client.serverIP, 12345, m.Serialize());
        }
예제 #2
0
        /// <summary>
        /// It will send a alive message within 3 seconds.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void timerForAlive_Elapsed(object sender, ElapsedEventArgs e)
        {
            timerForAlive.Stop();
            //throw new NotImplementedException();
            ClientMsg m = new ClientMsg();

            m.Type = (int)ClientMsgType.Alive;
            m.Info = Program.app.myInfo;
            l.Send(Program.app.ServerIP, 12345, m.Serialize());
            timerForAlive.Start();
        }
예제 #3
0
        /// <summary>
        /// This button send Public Message/Status to all other clients.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSendStatus_Click(object sender, EventArgs e)
        {
            ClientMsg msg = new ClientMsg();

            msg.Type          = (int)ClientMsgType.Status;
            msg.Status        = txtStatus.Text;
            msg.Info.ClientID = Program.app.myInfo.ClientID;
            msg.Info.Name     = Program.app.myInfo.Name;
            Program.app.client.L.Send(Program.app.ServerIP, 12345, msg.Serialize());
            txtStatus.Font      = new Font(txtStatus.Font, FontStyle.Italic);
            txtStatus.ForeColor = Color.LightGray;
            txtStatus.Text      = "";
        }
예제 #4
0
        /// <summary>
        /// This is the menu at the top of Client Window. If client  click Leave option, there will send a leave
        /// message to server and close all window
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void leaveToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            ClientMsg m = new ClientMsg();

            m.Type          = (int)ClientMsgType.Disconnect;
            m.Info.ClientID = Program.app.myInfo.ClientID;
            m.Info.Name     = Program.app.myInfo.Name;
            m.From          = Program.app.myInfo.ClientID;
            Program.app.client.L.Send(Program.app.ServerIP, 12345, m.Serialize());
            if (Program.app.hasOwnServer)
            {
                Program.app.server.Dispose();
                Program.app.server = null;
            }
            Program.app.client.Dispose();
            Program.app.client = null;
            f1.Show();
            this.Dispose();
            this.Close();
        }
예제 #5
0
        /// <summary>
        /// Start listening in a port, send joining message to server,
        /// </summary>
        /// <param name="serverIP"></param>
        /// <param name="name"></param>
        public void Start(string serverIP, string name)
        {
            this.serverIP = serverIP;
            me            = new ClientInfo();
            me.IP         = Program.OwnIP;
            me.Name       = name;

            clientDic = new Dictionary <int, ClientInfo>();
            l         = new SocketListener(0, gotClientMsg);
            Program.app.myInfo.ListenPort = l.Port;
            me.ListenPort = l.Port;
            // Sending joining message to server
            ClientMsg msg = new ClientMsg();

            msg.Info = me;
            msg.Type = (int)ClientMsgType.Join;
            l.Send(serverIP, 12345, msg.Serialize());
            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
            timer.Start();
            timerForAlive.Elapsed += new ElapsedEventHandler(timerForAlive_Elapsed);
            timerForAlive.Start();
        }
예제 #6
0
        /// <summary>
        /// Server Checks Within 6 seconds weather every Clients send alive message or not.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void serverTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            serverTimer.Stop();
            DateTime now = DateTime.Now;
            TimeSpan diff;
            string   res;

            lock (Program.lockObject)
            {
                foreach (ClientInfo info in AliveList.Values)
                {
                    diff = now - info.lastAlivemsg;
                    res  = diff.Seconds.ToString();
                    int x = int.Parse(res);
                    if (x >= 6)
                    {
                        lock (Program.lockObject2)
                        {
                            myList.Remove(info.ClientID);
                            ClientMsg M = new ClientMsg();
                            M.Type = (int)ClientMsgType.Disconnect;
                            M.Info = info;
                            byte[] data3 = M.Serialize();
                            foreach (ClientInfo c in myList.Values)
                            {
                                listener.Send(c.IP, c.ListenPort, data3);
                            }
                            ClientMsg m = new ClientMsg();
                            m.Type = (int)ClientMsgType.disconnectedByServer;
                            listener.Send(info.IP, info.ListenPort, m.Serialize());
                        }
                    }
                    serverTimer.Start();
                }
                //throw new NotImplementedException();
            }
        }
예제 #7
0
        /// <summary>
        /// Checking Which Type of message Server got for further Processing
        /// </summary>
        /// <param name="data"></param>
        /// <param name="dataAvailable"></param>
        private void msgFromClient(byte[] data, int dataAvailable)
        {
            ClientMsg msg = ClientMsg.DeSerialize(data, 0, dataAvailable);

            switch ((ClientMsgType)msg.Type)
            {
            case ClientMsgType.Join:

                ClientMsg m = new ClientMsg();     // this m will sent to new comer.
                m.Type          = (int)ClientMsgType.ClientList;
                m.Info.ClientID = ++lastClientID;

                msg.Info.ClientID = m.Info.ClientID;     // this msg will sent to other clients for notify about new comer.
                msg.Type          = (int)ClientMsgType.clientListForALL;
                byte[] data2 = msg.Serialize();
                lock (Program.lockObject2)
                {
                    foreach (ClientInfo c in myList.Values)
                    {
                        m.CurrentClients.Add(c);
                        // Send all clients the join msg
                        listener.Send(c.IP, c.ListenPort, data2);
                    }

                    // Sending new client server's client list
                    listener.Send(msg.Info.IP, msg.Info.ListenPort, m.Serialize());
                    //// Add this client to server's own client list
                    myList.Add(msg.Info.ClientID, msg.Info);
                }
                break;



            case ClientMsgType.Msg:

                listener.Send(msg.Info.IP, msg.Info.ListenPort, data);
                break;


            case ClientMsgType.Disconnect:
                lock (Program.lockObject2)
                {
                    myList.Remove(msg.Info.ClientID);
                    ClientMsg M = new ClientMsg();
                    M.Type = (int)ClientMsgType.Disconnect;
                    M.Info = msg.Info;
                    byte[] data3 = M.Serialize();
                    foreach (ClientInfo c in myList.Values)
                    {
                        listener.Send(c.IP, c.ListenPort, data3);
                    }
                }
                if (Program.app.myInfo.ClientID == msg.Info.ClientID)
                {
                    this.Dispose();
                }

                break;


            case ClientMsgType.Status:

                foreach (ClientInfo c in myList.Values)
                {
                    listener.Send(c.IP, c.ListenPort, msg.Serialize());
                }
                break;



            case ClientMsgType.Buzz:

                listener.Send(msg.Info.IP, msg.Info.ListenPort, data);
                break;

            case ClientMsgType.Alive:

                msg.Info.lastAlivemsg = DateTime.Now;

                lock (Program.lockObject)
                {
                    // AliveList[msg.Info.ClientID] = new DateTime();
                    AliveList[msg.Info.ClientID] = msg.Info;
                }
                break;
            }
        }
예제 #8
0
        /// <summary>
        /// When Client get any message. Here Message type will be checked.
        /// </summary>
        /// <param name="data"></param>
        /// <param name="dataAvailable"></param>
        private void gotClientMsg(byte[] data, int dataAvailable)
        {
            ClientMsg msg = ClientMsg.DeSerialize(data, 0, dataAvailable);

            //Checking Which Type of Message Client got.
            switch ((ClientMsgType)msg.Type)
            {
            case ClientMsgType.ClientList:

                timer.Stop();
                Program.app.myInfo.ClientID = msg.Info.ClientID;
                foreach (ClientInfo info in msg.CurrentClients)
                {
                    if (clientDic.ContainsKey(info.ClientID) == false)
                    {
                        clientDic.Add(info.ClientID, info);
                    }
                }
                if (ConnectionStatus != null)
                {
                    ConnectionStatus(serverIP, true);
                }
                break;



            case ClientMsgType.clientListForALL:

                msg.CurrentClients.Add(msg.Info);
                foreach (ClientInfo info in msg.CurrentClients)
                {
                    if (clientDic.ContainsKey(info.ClientID) == false)
                    {
                        clientDic.Add(info.ClientID, info);
                    }
                }
                if (NewList != null)
                {
                    NewList(msg.CurrentClients);
                }
                if (NewStatus != null)
                {
                    NewStatus(msg.Info, msg.Status, ClientMsgType.Join);
                }
                break;



            case ClientMsgType.Msg:

                if (NewMsg != null)
                {
                    NewMsg(msg.Info, msg.Msg, msg.From, msg.LineNumb);
                }
                break;



            case ClientMsgType.Disconnect:
                if (NewStatus != null)
                {
                    NewStatus(msg.Info, msg.Status, ClientMsgType.Disconnect);
                }
                if (clientDic.ContainsKey(msg.Info.ClientID) == true)
                {
                    clientDic.Remove(msg.Info.ClientID);
                }
                if (ClientLeaved != null)
                {
                    ClientLeaved(msg.Info);
                }
                break;

            case ClientMsgType.Status:

                if (NewStatus != null)
                {
                    NewStatus(msg.Info, msg.Status, ClientMsgType.Status);
                }
                break;

            case ClientMsgType.Buzz:

                if (NewBuzz != null)
                {
                    NewBuzz(msg.From);
                }
                break;

            case ClientMsgType.disconnectedByServer:
                if (DisconnectByServer != null)
                {
                    DisconnectByServer();
                }
                break;
            }
        }