Esempio n. 1
0
        protected void SendStepAll(object sender, ElapsedEventArgs e)
        {
            if (ClientPool.ActiveCount <= 0)
            {
                if (FrameList.Count > 0)
                {
                    ServerLog.LogServer("所有客户端退出游戏 战斗结束!!!", 1);
                    FrameList.Clear();
                }
                return;
            }

            if (FrameList.Count == 0)
            {
                ServerLog.LogServer("玩家进入服务器 战斗开始!!!", 1);
            }
            ServerLog.LogServer("0[" + FrameList.Count + "]", 1);

            byte[][]     temp     = StepMessage;
            int          length   = temp.Length;
            ProtocolBase protocol = new ByteProtocol();

            protocol.push((byte)MessageType.Frame);
            protocol.push((byte)length);
            //ServerLog.LogServer("获取[" + FrameList.Count + "]", 1);
            for (int i = 0; i < length; i++)
            {
                protocol.push(temp[i] != null);
                protocol.push(temp[i]);
            }
            if (FrameList.Count == 0)
            {
                protocol.push((byte)MessageType.RandomSeed);
                Random rand = new Random();
                protocol.push(rand.Next(10000));
            }
            protocol.push((byte)MessageType.end);
            ServerLog.LogServer("生成帧信息[" + length + "]", 1);
            byte[] temp2 = protocol.GetByteStream();

            FrameList.Add(temp2);

            ClientPool.Foreach((con) => { SendToClient(con.clientId, temp2);
                                          if (!con.ActiveCheck())
                                          {
                                              ServerLog.LogClient("客户端断线 中止连接:" + ClientPool[con.clientId].socket.LocalEndPoint + "ClientID:" + con.clientId, 0, con.clientId);

                                              con.socket.Close();
                                              ClientPool.Recover(con.clientId);
                                          }
                               });

            ServerLog.LogServer("帧同步[" + FrameList.Count + "]", 2);
            //StepMessage = new byte[ClientPool.Count][];
            //for (int i = 0; i < StepMessage.Length; i++)
            //{
            //    StepMessage[i] = new byte[framSize];
            //}
        }
Esempio n. 2
0
 protected void SendToClientAllFrame(int clientId)
 {
     byte[][] list = FrameList.ToArray();
     ServerLog.LogClient("中途加入 发送历史帧:" + list.Length, 3, clientId);
     foreach (var item in list)
     {
         SendToClient(clientId, item);
     }
 }
Esempio n. 3
0
        protected void SendIninInfo(byte clientId)
        {
            ProtocolBase protocol = new ByteProtocol();

            protocol.push((byte)MessageType.Init);
            protocol.push(clientId);
            protocol.push((byte)MessageType.end);
            SendToClient(clientId, protocol.GetByteStream());
            ServerLog.LogClient("客户端连接成功:" + ClientPool[clientId].socket.LocalEndPoint + "ClientID:" + clientId, 0, clientId);
        }
Esempio n. 4
0
        protected void ReceiveCallBack(IAsyncResult ar)
        {
            Connection con = (Connection)ar.AsyncState;

            if (!con.ActiveCheck())
            {
                return;
            }
            try
            {
                lock (con)
                {
                    int length = con.socket.EndReceive(ar);
                    ServerLog.LogClient("receive:" + length, 1, con.clientId);
                    if (length <= 0)
                    {
                        ServerLog.LogClient("客户端断开连接:" + ClientPool[con.clientId].socket.LocalEndPoint + "ClientID:" + con.clientId, 0, con.clientId);

                        con.socket.Close();
                        ClientPool.Recover(con.clientId);

                        return;
                    }
                    con.length += length;
                    //Console.WriteLine("bool isNew = bufferList[con]:" + ClientPool[con.clientId].socket.LocalEndPoint + "ClientID:" + con.clientId);
                    //bool isNew = bufferList[con] == null;
                    ProcessData(con);

                    //{
                    //    //MessageList.Push(message);

                    //    bufferList[con] = null;
                    //}
                    //else
                    //{
                    //    bufferList[con] = message;
                    ////}
                    //Console.WriteLine("接收信息:" + message.Length + "ClientID:" + con.clientId);



                    con.socket.BeginReceive(con.readBuff, con.length, con.BuffRemain, SocketFlags.None, ReceiveCallBack, con);
                }
            }
            catch (Exception)
            {
                ServerLog.LogClient("客户端异常终止连接:" + ClientPool[con.clientId].socket.LocalEndPoint + "ClientID:" + con.clientId, 0, con.clientId);

                con.socket.Close();
                ClientPool.Recover(con.clientId);
                //throw;
            }
        }
Esempio n. 5
0
 protected void SendToClient(int clientId, byte[] bytes)
 {
     //int sendLength = bytes.Length;
     //int index=0;
     //int unitLength=0;
     byte[] length = BitConverter.GetBytes(bytes.Length);
     byte[] temp   = new byte[4 + bytes.Length];
     Array.Copy(length, temp, 4);
     Array.Copy(bytes, 0, temp, 4, bytes.Length);
     ServerLog.LogClient("send:" + temp.Length, 2, clientId);
     ClientPool[clientId].socket.BeginSend(temp, 0, temp.Length, SocketFlags.None, null, null);
     //Console.WriteLine(DateTime.Now.ToLongTimeString() + "clientId:" + clientId + " mesagge " + bytes[0]+" length "+bytes.Length);
 }
Esempio n. 6
0
 protected void SendToClient(int clientId, byte[] bytes)
 {
     byte[] length = BitConverter.GetBytes(bytes.Length);
     byte[] send   = new byte[4 + bytes.Length];
     Array.Copy(length, send, 4);
     Array.Copy(bytes, 0, send, 4, bytes.Length);
     ServerLog.LogClient("send:" + send.Length, 2, clientId);
     try
     {
         ClientPool[clientId].socket.BeginSend(send, 0, send.Length, SocketFlags.None, null, null);
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
     }
 }
Esempio n. 7
0
        protected void ReceiveCallBack(IAsyncResult ar)
        {
            Connection con = (Connection)ar.AsyncState;

            if (!con.ActiveCheck())
            {
                return;
            }

            try
            {
                lock (con)
                {
                    int length = con.socket.EndReceive(ar);
                    ServerLog.LogClient("receive:" + length, 1, con.clientId);
                    if (length <= 0)
                    {
                        ServerLog.LogClient("客户端断开连接:" + ClientPool[con.clientId].socket.LocalEndPoint + "ClientID:" + con.clientId, 0, con.clientId);
                        con.socket.Close();
                        ClientPool.Recover(con.clientId);
                        return;
                    }

                    con.length += length;

                    ProcessData(con);

                    con.socket.BeginReceive(con.readBuff, con.length, con.BuffRemain, SocketFlags.None, ReceiveCallBack, con);
                }
            }
            catch (Exception)
            {
                ServerLog.LogClient("客户端异常终止连接:" + ClientPool[con.clientId].socket.LocalEndPoint + "ClientID:" + con.clientId, 0, con.clientId);

                con.socket.Close();
                ClientPool.Recover(con.clientId);
            }
        }
Esempio n. 8
0
        protected void ParseMessage(Connection con, ProtocolBase protocol)
        {
            if (_battleEnd == true)
            {
                return;
            }

            MessageType messageType = (MessageType)protocol.getByte();

            switch (messageType)
            {
            case MessageType.Frame:
                byte   clientId = protocol.getByte();
                byte[] t2       = protocol.getLastBytes();
                StepMessage[con.clientId] = t2;
                ClientPool[clientId].SetActive();
                ServerLog.LogClient("Key:[" + t2.Length + "]", 3, clientId);
                break;

            case MessageType.ClientReady:
                break;

            case MessageType.Ping:
                byte id = protocol.getByte();
                SendPingToClient(id);
                break;

            default:
                Console.WriteLine("not handle messagetype " + messageType);
                return;
            }

            if (protocol.Length > 0)
            {
                ServerLog.LogServer("剩余未解析" + protocol.Length, 1);
            }
        }