Exemplo n.º 1
0
        public void OnConnect(Client c)
        {
            Console.WriteLine("{0}[{1}, {2}] connected!", c.ID, c.tcpAdress, c.udpAdress);
            clientList.Add(c);

            MessageBuffer msg = new MessageBuffer();
            msg.WriteInt(cProto.CONNECT);
            msg.WriteInt(roleId);
            c.Send(msg);
            roleId++;
        }
Exemplo n.º 2
0
        void ClientConnected(Socket s)
        {
            Client c = new Client(nmbrOfClients, s, this);
            clientList.Add(c);
            c.SendAcceptPoll();

            nmbrOfClients++;
        }
Exemplo n.º 3
0
 public void Send(MessageBuffer msg, Client c)
 {
     if (c != null && c.udpAdress != null)
         outMessages.Add(new MessageInfo(msg, c.udpAdress, this));
 }
Exemplo n.º 4
0
 public void PingResult(Client c, int millis)
 {
     if (OnPing != null) OnPing(c, millis);
 }
Exemplo n.º 5
0
 public void ClientDisconnected(Client c)
 {
     clientList.Remove(c);
     disconnectedList.Add(c);
 }
Exemplo n.º 6
0
        public void OnMessage(Client c, MessageBuffer msg)
        {
            int cproto = msg.ReadInt();
            switch(cproto)
            {
                case cProto.CONNECT:
                    break;
                case cProto.READY:
                    if (!userList.ContainsKey(c))
                    {
                        int id = msg.ReadInt();
                        userList.Add(c, id);
                    }
                    //所有的玩家都准备好了,可以开始同步
                    if(userList.Count >= clientList.Count)
                    {
                        frameCount = 1;
                        keyDic = new Dictionary<int, Dictionary<int, List<string>>>();
                        string playStr = "";
                        List<string> playList = new List<string>();
                        foreach(var play in userList)
                        {
                            CharData charData = new CharData(play.Value, play.Value+ "_" + play.Value);
                            playList.Add(charData.ToString());
                        }
                        playStr = string.Join(";", playList.ToArray());
                        MessageBuffer buff = new MessageBuffer();
                        buff.WriteInt(cProto.START);
                        buff.WriteString(playStr);

                        for (int i = 0; i < clientList.Count; ++i)
                        {
                            clientList[i].Send(buff);
                        }
                    }
                    break;
                case cProto.SYNC_POS:
                    for (int i = 0; i < clientList.Count; ++i)
                    {
                        if(c == clientList[i])
                        {
                            continue;
                        }
                        clientList[i].Send(msg);
                    }
                        break;
                case cProto.SYNC_KEY:
                    int clientCurFrameCount = msg.ReadInt();
                    string keyStr = msg.ReadString();
                    if(keyDic.ContainsKey(clientCurFrameCount))
                    {
                        if(keyDic[clientCurFrameCount].ContainsKey(userList[c]))
                        {
                            keyDic[clientCurFrameCount][userList[c]].Add(keyStr);
                        }
                        else
                        {
                            keyDic[clientCurFrameCount][userList[c]] = new List<string>();
                            keyDic[clientCurFrameCount][userList[c]].Add(keyStr);
                        }
                    }
                    else
                    {
                        keyDic[clientCurFrameCount] = new Dictionary<int,List<string>>();
                        keyDic[clientCurFrameCount][userList[c]] = new List<string>();
                        keyDic[clientCurFrameCount][userList[c]].Add(keyStr);
                    }
                    if(clientCurFrameCount == frameCount)
                    {
                        if(keyDic[clientCurFrameCount].Count == clientList.Count)
                        {
                            List<string> keyDataList = new List<string>();
                            foreach(var dataList in keyDic[clientCurFrameCount].Values)
                            {
                                keyDataList.AddRange(dataList);
                            }

                            string keyData = string.Join(";", keyDataList.ToArray());
                            MessageBuffer buff = new MessageBuffer();
                            buff.WriteInt(cProto.SYNC_KEY);
                            buff.WriteInt(frameCount);
                            buff.WriteString(keyData);
                            for (int i = 0; i < clientList.Count; ++i)
                            {
                                clientList[i].Send(buff);
                            }
                            frameCount += 1;
                        }
                    }
                    break;
                case cProto.START:
                    break;
            }
        }
Exemplo n.º 7
0
 public void OnDisconnect(Client c)
 {
     Console.WriteLine("{0}[{1}, {2}] disconnected!", c.ID, c.tcpAdress, c.udpAdress);
     clientList.Remove(c);
     if(userList.ContainsKey(c))
     {
         userList.Remove(c);
     }
 }