예제 #1
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;
            }
        }
예제 #2
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;
            }
        }