Beispiel #1
0
    public void HandleRegisterAccount(byte[] bytes, PlazaSession client)
    {
        string     id   = "";
        string     pw   = "";
        string     name = "";
        ByteBuffer read = new ByteBuffer(bytes);

        id   = read.ReadString();
        pw   = read.ReadString();
        name = read.ReadString();
        bool registerSucceed = false;

        if (id != string.Empty && pw != string.Empty && name == string.Empty)
        {
            registerSucceed = dataMgr.Register(id, pw, name);
        }
        string msg = "注册成功!";

        if (!registerSucceed)
        {
            msg = "注册失败!";
        }
        sessionCode.MainCmdId = 0;
        sessionCode.SubCmdId  = 1001;
        ByteBuffer buff = new ByteBuffer();

        buff.WriteString(msg);
        sessionCode.SetBytes(buff.ToBytes());
        byte[] byteCode = serial.Encode(sessionCode);
        SeverNet.instance.Send(client, byteCode);
    }
Beispiel #2
0
 public void SubPackHanlder(int op, byte[] bytes, PlazaSession client)
 {
     if (subPack.ContainsKey(op))
     {
         subPack[op](bytes, client);
     }
 }
Beispiel #3
0
 public void MainPackHanlder(int op, PlazaSessionCode code, PlazaSession client)
 {
     if (mainPack.ContainsKey(op))
     {
         mainPack[op](code.SubCmdId, code.GetBytes, client);
     }
 }
Beispiel #4
0
 //private DataMgr dataMgr = new DataMgr();
 public void HandleConnectSuccess(byte[] bytes, PlazaSession client)
 {
     //ByteBuffer read = new ByteBuffer(bytes);
     //string readMsg = read.ReadString();
     //string msg = "收到[" + client.GetAddress()+ "]发来消息内容:" + readMsg;
     //ByteBuffer buff = new ByteBuffer();
     //buff.WriteString(msg);
     ////PlazaSessionCode sessionCode = new PlazaSessionCode(0, 11, buff.ToBytes());
     ////byte[] b = serial.Encode(sessionCode);
     //SeverNet.instance.Broadcast(client, buff.ToBytes());
 }
Beispiel #5
0
    public void HandleRecevieMsg(byte[] bytes, PlazaSession client)
    {
        ByteBuffer read    = new ByteBuffer(bytes);
        string     readMsg = read.ReadString();
        string     msg     = "收到[" + client.GetAddress() + "]发来消息内容:" + readMsg;

        sessionCode.MainCmdId = 0;
        sessionCode.SubCmdId  = 1002;
        ByteBuffer buff = new ByteBuffer();

        buff.WriteString(msg);
        sessionCode.SetBytes(buff.ToBytes());
        byte[] byteCode = serial.Encode(sessionCode);
        SeverNet.instance.Broadcast(client, byteCode);
    }
Beispiel #6
0
 public void Start(string host, int port)
 {
     //定时器
     //timer.Elapsed += new System.Timers.ElapsedEventHandler(HandleMainTimer);
     //timer.AutoReset = false;
     //timer.Enabled = true;
     //链接池
     clients = new PlazaSession[maxConn];
     for (int i = 0; i < maxConn; i++)
     {
         clients[i] = new PlazaSession();
     }
     tcpListener = new TcpListener(new IPEndPoint(IPAddress.Any, 6379));
     tcpListener.Start();
     tcpListener.BeginAcceptTcpClient(AcceptCb, tcpListener);
     Console.WriteLine("[服务器]启动成功");
 }
Beispiel #7
0
 public int NewIndex()
 {
     if (clients == null)
     {
         return(-1);
     }
     for (int i = 0; i < clients.Length; i++)
     {
         if (clients[i] == null)
         {
             clients[i] = new PlazaSession();
             return(i);
         }
         else if (clients[i].isUsed == false)
         {
             return(i);
         }
     }
     return(-1);
 }
Beispiel #8
0
        private void ReceiveCb(IAsyncResult ar)
        {
            PlazaSession plazaSession = (PlazaSession)ar.AsyncState;
            int          bytesRead    = 0;

            try
            {
                lock (plazaSession.client.GetStream())
                {         //读取字节流到缓冲区
                    bytesRead = plazaSession.client.GetStream().EndRead(ar);
                }
                if (bytesRead < 1)
                {                //包尺寸有问题,断线处理
                    //OnDisconnected(DisType.Disconnect, "bytesRead < 1");
                    return;
                }
                byte[] lenBytes = new byte[bytesRead - sizeof(ushort)];
                Array.Copy(plazaSession.byteBuffer, sizeof(ushort), lenBytes, 0, bytesRead - sizeof(ushort));
                PlazaSessionCode sessionCode = (PlazaSessionCode)serial.Decode(lenBytes, 0, lenBytes.Length);
                MessageOperate   msgOperate  = new MessageOperate();
                msgOperate.MainPackHanlder(sessionCode.MainCmdId, sessionCode, plazaSession);
                //ByteBuffer buff = new ByteBuffer(sessionCode.message.byteBuffer);
                //Console.WriteLine(sessionCode.message.mainCmdId);
                //Console.WriteLine(sessionCode.message.subCmdId);
                //string w = buff.ReadString();
                //string q = buff.ReadString();
                //Console.WriteLine(w);
                //Console.WriteLine(q);
                //OnReceive(byteBuffer, bytesRead);   //分析数据包内容,抛给逻辑层
                lock (plazaSession.client.GetStream())
                {                                                                            //分析完,再次监听服务器发过来的新消息
                    Array.Clear(plazaSession.byteBuffer, 0, plazaSession.byteBuffer.Length); //清空数组
                    plazaSession.client.GetStream().BeginRead(plazaSession.byteBuffer, 0, plazaSession.GetMaxRead(), new AsyncCallback(ReceiveCb), plazaSession);
                }
            }
            catch (Exception ex)
            {
                //PrintBytes();
                //OnDisconnected(DisType.Exception, ex.Message);
            }
        }
Beispiel #9
0
        public void Send(PlazaSession pla, byte[] bytes)
        {
            MemoryStream ms = null;

            using (ms = new MemoryStream())
            {
                ms.Position = 0;
                BinaryWriter writer = new BinaryWriter(ms);
                ushort       msglen = (ushort)bytes.Length;
                writer.Write(msglen);
                writer.Write(bytes);
                writer.Flush();
                if (pla.client != null && pla.client.Connected)
                {
                    outStream = pla.client.GetStream();
                    //NetworkStream stream = client.GetStream();
                    byte[] payload = ms.ToArray();
                    outStream.BeginWrite(payload, 0, payload.Length, null, null);
                }
            }
        }
Beispiel #10
0
 public void Broadcast(PlazaSession client, byte[] bytes, bool isTalkSelf = false)
 {
     for (int i = 0; i < clients.Length; i++)
     {
         if (clients[i] == null)
         {
             continue;
         }
         if (!clients[i].isUsed)
         {
             continue;
         }
         if (clients[i] == client && !isTalkSelf)
         {
             continue;
         }
         lock (clients[i])
         {
             Send(clients[i], bytes);
         }
     }
 }
Beispiel #11
0
        private void AcceptCb(IAsyncResult ar)
        {
            try
            {
                TcpListener listener = (TcpListener)ar.AsyncState;
                TcpClient   client   = (TcpClient)listener.EndAcceptTcpClient(ar);
                int         index    = NewIndex();

                if (index < 0)
                {
                    client.Close();
                    Console.Write("[警告]链接已满");
                }
                else
                {
                    PlazaSession plazaSession = clients[index];
                    plazaSession.Init(client);
                    string adr = plazaSession.GetAddress();
                    Console.WriteLine("客户端连接 [" + adr + "] conn池ID:" + index);
                    MessageData messageData = new MessageData();
                    string      msg         = " [" + plazaSession.GetAddress() + "] 加入聊天房间,当前房间人数[" + GetOnlineCount() + "]";
                    plazaSession.client.GetStream().BeginRead(plazaSession.byteBuffer, 0, plazaSession.GetMaxRead(), new AsyncCallback(ReceiveCb), plazaSession);
                    //plazaSession.client
                    //AddMessage(conn, ref messageData, 0, 3, msg);
                    //Broadcast(conn, messageData,true);
                    //conn.socket.BeginReceive(conn.readBuffer,
                    //                         conn.bufferCount, conn.Buffremain(),
                    //                         SocketFlags.None, ReceiveCb, conn);
                }
                tcpListener.BeginAcceptTcpClient(AcceptCb, tcpListener);
            }
            catch (Exception e)
            {
                Console.WriteLine("AcceptCb失败:" + e.Message);
            }
        }