示例#1
0
        public void sessionCreated(TCPSession session)
        {
            CAccount a = new CAccount();

            a.name     = "孙克宁";
            a.password = "******";
            TCPUtil.sendMessage(session, a);
            Debug.Log("和服务器建立连接");
        }
示例#2
0
 public override byte[] Encode(TCPSession session, object message)
 {
     // MessageContext messsageContext=(MessageContext)message;
     // byte[] bodyBytes = (byte[])message;
     // //MemoryStream memStreamBody = new MemoryStream();
     // MsgHead msghead = new MsgHead();
     //// msghead.msgType = (uint)messsageContext.msgtype;
     // //byte[] messagebody=messsageContext.action.toBytesArray(messsageContext.message);
     // msghead.msgLength = (uint)bodyBytes.Length;
     // MemoryStream memStreamHead = new MemoryStream();
     // Serializer.Serialize<MsgHead>(memStreamHead, msghead);
     // byte[] package = new byte[MessageManager.MsgHeadLength + msghead.msgLength];
     // Buffer.BlockCopy(memStreamHead.GetBuffer(), 0, package, 0, MessageManager.MsgHeadLength);
     // Buffer.BlockCopy(bodyBytes, 0, package, MessageManager.MsgHeadLength, bodyBytes.Length);
     return((byte[])message);
 }
示例#3
0
        public void ClientAccepted(IAsyncResult ar)
        {
            //这就是客户端的Socket实例,我们后续可以将其保存起来
            Socket     socketClient = socket.EndAccept(ar);
            TCPClient  tcpClient    = new TCPClient();
            TCPSession session      = new TCPSession(tcpClient);

            tcpClient.session = session;
            tcpClient.setProtocolCodecFilter((ProtocolCodecFilter)Activator.CreateInstance(protocolCodecFilter.GetType()));
            tcpClient.setHandler((IHandler)Activator.CreateInstance(handler.GetType()));
            session.id           = idCounter++;
            session.socket       = socketClient;
            sessions[session.id] = session;
            handler.sessionCreated(session);
            tcpClient.beginReceive();
            //准备接受下一个客户端请求
            socket.BeginAccept(new AsyncCallback(ClientAccepted), socket);
        }
示例#4
0
        public override bool Decode(TCPSession session, PackageBuffer buffer, ProtocolDecoderOutput output)
        {
            // 如果没有接收完Header部分(4字节),直接返回false
            if (buffer.remaining() < MessageManager.MsgHeadLength)
            {
                return(false);
            }
            else
            {
                // 标记开始位置,如果一条消息没传输完成则返回到这个位置
                buffer.mark();

                byte[] bytes = new byte[MessageManager.MsgHeadLength];
                // 读取消息头
                buffer.get(bytes);
                MemoryStream msgHeadStream = new MemoryStream(bytes);
                MsgHead      msgHead       = Serializer.Deserialize <MsgHead>(msgHeadStream);
                msgHeadStream.Close();
                // 读取消息体的长度,
                uint bodyLength = msgHead.msgLength;

                // 如果消息体没有接收完整,直接返回false
                if (buffer.remaining() < bodyLength)
                {
                    buffer.reset(); // IoBuffer position回到原来标记的地方
                    return(false);
                }
                else
                {
                    //获取消息类型
                    uint messageType = msgHead.msgType;
                    bytes = new byte[bodyLength];
                    // 读取消息体
                    buffer.get(bytes);
                    //MemoryStream msgbodyStream = new MemoryStream(bytes);
                    MessageContext messageContext = MessageManager.instance.getMessageContext((int)messageType);
                    messageContext.message = messageContext.action.parse(bytes);

                    output.Write(messageContext);
                    return(true);
                }
            }
        }
示例#5
0
        public bool Connect(string ip, int port)
        {
            //IPAddress address = IPAddress.Parse(ip);
            //IPEndPoint endP = new IPEndPoint(address, port);
            try
            {
                session        = new TCPSession(this);
                session.socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                //异步方法,连接上后调用ConnectionCallback
                session.socket.Connect(ip, port);

                handler.sessionCreated(session);
                beginReceive();
            }
            catch (System.Exception e)
            {
                System.Console.WriteLine(e);
                return(false);
            }
            return(true);
        }
示例#6
0
        public void messageReceived(TCPSession session, object message)
        {
            MessageContext messageContext = (MessageContext)message;

            messageContext.action.doAction(messageContext.message);
        }