コード例 #1
0
ファイル: Program.cs プロジェクト: caolaoyao/AsycServer
        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++;
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: zhu1987/AsycServer
        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;
            }
        }
コード例 #3
0
 public MessageInfo(MessageBuffer message, IPEndPoint endPoint, AyncServer s)
 {
     server  = s;
     Message = message;
     Adress  = endPoint;
 }
コード例 #4
0
 public void SendExternal(MessageBuffer msg, IPEndPoint ip)
 {
     outMessages.Add(new MessageInfo(msg, ip, this));
 }
コード例 #5
0
 public void Send(MessageBuffer msg, int id)
 {
     Send(msg, GetClient(id));
 }
コード例 #6
0
ファイル: AsyUdpServer.cs プロジェクト: caolaoyao/AsycServer
 public MessageInfo(MessageBuffer message, IPEndPoint endPoint, AsyUdpServer s)
 {
     server = s;
     Message = message;
     Adress = endPoint;
 }
コード例 #7
0
ファイル: AsyUdpServer.cs プロジェクト: caolaoyao/AsycServer
 public void SendExternal(MessageBuffer msg, IPEndPoint ip)
 {
     outMessages.Add(new MessageInfo(msg, ip, this));
 }
コード例 #8
0
ファイル: AsyUdpServer.cs プロジェクト: caolaoyao/AsycServer
 public void Send(MessageBuffer msg, Client c)
 {
     if (c != null && c.udpAdress != null)
         outMessages.Add(new MessageInfo(msg, c.udpAdress, this));
 }
コード例 #9
0
ファイル: AsyUdpServer.cs プロジェクト: caolaoyao/AsycServer
 public void Send(MessageBuffer msg, int id)
 {
     Send(msg, GetClient(id));
 }
コード例 #10
0
ファイル: Client.cs プロジェクト: hellowod/synchronize-frame
 public void Send(MessageBuffer msg)
 {
     server.Send(msg, this);
 }
コード例 #11
0
ファイル: Program.cs プロジェクト: caolaoyao/AsycServer
        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;
            }
        }
コード例 #12
0
ファイル: Client.cs プロジェクト: caolaoyao/AsycServer
 public void Send(MessageBuffer msg)
 {
     server.Send(msg, this);
 }