コード例 #1
0
        /// <summary>
        /// 从套接字的接收网络消息
        /// </summary>
        protected void RecvMsg(Socket sock)
        {
            TcpSocketProcArg arg = new TcpSocketProcArg();
            // 使用new的方式将此buffer一路传递给应用层
            arg.Buf = new Byte[3*1024];
            arg.Sock = sock;
			arg.BufLen =3*1024;
            arg.Stage = SocketRecvMsgStage.RECV_BODY;

            try
            {
				One=true;
				arg.Sock.BeginReceive(arg.Buf, 0, arg.Buf.Length, SocketFlags.None, TcpRecvMsgEndCallBack, arg);
//				arg.ReceiveDone.WaitOne(1000); // 5秒超时
            }
            catch (SocketException se)
            {
                //CSysLog.Instance.Write(SysLogLevel.ERR, String.Format("错误码: {0}, 描述: {1}", se.SocketErrorCode, se.Message));
            }
            catch (Exception e)
            {
                //CSysLog.Instance.Write(SysLogLevel.ERR, e.ToString());
            }
        }
コード例 #2
0
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="sendbuf">发送缓存区</param>
        /// <param name="msglen">要发送的消息长度</param>
        protected void Send(byte[] sendbuf, Int32 msglen)
        {
            try
            {
                // 检查上一个数据是否发送完毕,如果没有的话将buffer放入发送队列中排队
                if (false == mSendBufferQueue.AddSocketBuffer(sendbuf, msglen))
                {
                    return;
                }

                TcpSocketProcArg arg = new TcpSocketProcArg();
                arg.Buf = sendbuf;
                arg.Sock = Sock;
                arg.BufLen = msglen;

                Sock.BeginSend(arg.Buf, 0, arg.BufLen, SocketFlags.None, SendMsgEndCallBack, arg);
            }
            catch (SocketException se)
            {
                //CSysLog.Instance.Write(SysLogLevel.ERR, String.Format("错误码: {0}, 描述: {1}", se.SocketErrorCode, se.Message));
            }
            catch (Exception e)
            {
                //CSysLog.Instance.Write(SysLogLevel.ERR, e.ToString());
            }
        }
コード例 #3
0
        /// <summary>
        /// 处理消息体
        /// </summary>
        /// <param name="client"></param>
        private void HandleMsgBody(TcpSocketProcArg client)
        {
			byte[] byteRead = client.Stream.ToArray ();
			byte[] head1 = new byte[4];
			byte[] head2 = new byte[4];
			byte[] head3 = new byte[4];
			//TODO: 向应用层投递消息
			Buffer.BlockCopy (byteRead, 0, head1, 0, 4);
			Int32 msgnumber = BitConverter.ToInt32 (head1, 0);
			Buffer.BlockCopy (byteRead, 4, head2, 0, 4);
			Int32 msglen = BitConverter.ToInt32 (head2, 0);
			Buffer.BlockCopy (byteRead, 8, head3, 0, 4);
			Int32 reqid = BitConverter.ToInt32 (head3, 0);
			Byte [] msgbuf = new Byte[msglen]; 
			Buffer.BlockCopy (byteRead, 12, msgbuf, 0, msglen);
			if(msglen>2000)
			{
				Debug.LogError (msgbuf.Length.ToString()+"   aaaaa");
			}
			NetRecvMsg recvmsg = new NetRecvMsg (msgbuf, msglen, msgnumber, reqid);
			ModelToControlQueue.Instance.AddNetMsg (recvmsg);
			
            // 重新接收头部,进行循环接收消息
            RecvMsg(client.Sock);

            client = null;
        }
コード例 #4
0
        /// <summary>
        /// 处理消息头部
        /// </summary>
        /// <param name="client">socket参数</param>
        private void HandleMsgHead(TcpSocketProcArg client)
        {
            // 打印头部消息
            //UtilNet.PrintMsg(client.Buf, "接收到Head");

            // 获取头部信息
            MsgHead head = UtilNet.BytesToStruct<MsgHead>(client.Buf);

            // 完整接收到了头部信息,开始验证消息
//            if (false == CheckMsgValidity(ref head))
//            {
//                // 断开连接
//                CloseSocket("协议头部信息无效,客户端断开连接");
//
//                return;
//            }

            // 保存消息头部, C#中的struct为值类型
            client.Head = head;
            // 消息体的长度信息
            client.BufLen = (Int32)(head.msglen);
            // 接收数据
            client.Stage = SocketRecvMsgStage.RECV_BODY;

            // 开始接收数据载荷
            client.Sock.BeginReceive(client.Buf, 0, client.BufLen, SocketFlags.None, TcpRecvMsgEndCallBack, client);
        }