コード例 #1
0
ファイル: SocketListener.cs プロジェクト: kcitwm/dova
        protected virtual void OnDataReceived(ReceiveArgs e)
        {
            if (null != DataReceived)
            {
                DataReceived(this, e); 
            } 

        }
コード例 #2
0
ファイル: SyncMessageService.cs プロジェクト: kcitwm/dova
 void Socket_DataReceived(object sender, ReceiveArgs e)
 {
     try
     {
         DataHoldingUserToken token = e.Token;
         int len = token.lengthOfCurrentIncomingMessage;
         string userToken = string.Empty;
         WQMessage msg = SerializeHelper.DeSerializeToJSon<WQMessage>(encode.GetString(token.theDataHolder.dataMessageReceived)) ;
         int status = -1; 
         if (Config.AuthoType == 1)
         {
             DACService dac = new DACService();
             status = dac.CheckLogin(userToken);
             if (status != 1)
             {
                 token.dataToSend = encode.GetBytes("-99");
                 return;
             }
         }
         object o = MessageHandler.ExecuteMessage(msg.ServiceName, msg, msg.Async);
         token.dataToSend = encode.GetBytes(SerializeHelper.SerializeToJSon(o));
         //using (MemoryStream ms = new MemoryStream())
         //{
         //    RuntimeTypeModel.Default.Serialize(ms, o);
         //    token.dataToSend=ms.GetBuffer(); 
         //} 
     }
     catch (Exception ex)
     {
         Log.Error(ex);
     }
 }
コード例 #3
0
ファイル: Service1.cs プロジェクト: kcitwm/dova
        void Socket_DataReceived(object sender, ReceiveArgs e)
        {
            try
            {

                DataHoldingUserToken token = e.Token;
                int len = token.theDataHolder.dataMessageReceived.Length;
                int headDataLen = BitConverter.ToInt32(token.theDataHolder.dataMessageReceived, 0);
                string methodName = Encoding.ASCII.GetString(token.theDataHolder.dataMessageReceived, headerLength, headDataLen);
                int dataStart = headerLength + headDataLen; ;
                len = len - dataStart;
                Log.Info("收到缓存请求:" + methodName);
                 
                switch (methodName)
                {
                    case "Get":
                           break;
                    case "Set":
                           break; 
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }

        }
コード例 #4
0
ファイル: DataAccessService.cs プロジェクト: kcitwm/dova
        void Socket_DataReceived(object sender, ReceiveArgs e)
        {
            long t = DateTime.Now.Ticks;
            try
            {
                Log.Info("Socket_DataReceived:开始触发接收事件处理数据的时间点:" + DateTime.Now.Ticks);
                DataHoldingUserToken token = e.Token;
                int len = token.lengthOfCurrentIncomingMessage;
                string userToken = string.Empty;
                int authoLen = 0;
                int headStart = 0;
                int headDataStart = headerLength;
                DACService dac = new DACService();
                int status = 1;
                if (Config.AuthoType == 1)
                {
                    Log.Info("Socket_DataReceived:开始认证:" + DateTime.Now.Ticks);

                    authoLen = BitConverter.ToInt32(token.theDataHolder.dataMessageReceived, 0);
                    userToken = Encoding.UTF8.GetString(token.theDataHolder.dataMessageReceived, headerLength, authoLen);
                    //检查是否登录 
                    status = dac.CheckLogin(userToken);

                    //结束检查登录
                    headStart = headerLength + authoLen;
                    headDataStart = headerLength + headerLength + authoLen;
                    Log.Info("Socket_DataReceived: 认证结束:" + DateTime.Now.Ticks);

                }
                int headDataLen = BitConverter.ToInt32(token.theDataHolder.dataMessageReceived, headStart);
                string methodName = Encoding.ASCII.GetString(token.theDataHolder.dataMessageReceived, headDataStart, headDataLen);
                if (status != 1 && methodName != "Login" && methodName != "Regist")
                {
                    e.DataToSend = encode.GetBytes("-99");
                    return;
                }
                int dataStart = headDataStart + headDataLen;
                len = len - dataStart;
                Log.Info("Socket_DataReceived:解析处理收到数据执行方法:" + methodName + " 时间点:" + DateTime.Now.Ticks);
                int fileNameLen = 0;
                string fileName = string.Empty;
                string data = "";
                switch (methodName)
                {
                    case "ExecutePagedDataList":
                        e.DataToSend = encode.GetBytes(dac.ExecutePagedDataList(JsonConvert.DeserializeObject<PagedRecordParameter>(encode.GetString(token.theDataHolder.dataMessageReceived, dataStart, len))));
                        break;
                    case "ExecuteDataList":
                        data = encode.GetString(token.theDataHolder.dataMessageReceived, dataStart, len);
                        Log.Write("收到数据:" + data);
                        e.DataToSend = encode.GetBytes(dac.ExecuteDataList(JsonConvert.DeserializeObject<WrapedDatabaseParameter>(data)));
                        break;
                    case "ExecuteNonQuery":
                        e.DataToSend = BitConverter.GetBytes(dac.ExecuteNonQuery(JsonConvert.DeserializeObject<WrapedDatabaseParameter>(encode.GetString(token.theDataHolder.dataMessageReceived, dataStart, len))));
                        break;
                    case "ExecuteScalar":
                        e.DataToSend = encode.GetBytes(dac.ExecuteScalar(JsonConvert.DeserializeObject<WrapedDatabaseParameter>(encode.GetString(token.theDataHolder.dataMessageReceived, dataStart, len))).ToString());
                        break;
                    case "ExecuteDataTable":
                        e.DataToSend = encode.GetBytes(dac.ExecuteDataTable(JsonConvert.DeserializeObject<WrapedDatabaseParameter>(encode.GetString(token.theDataHolder.dataMessageReceived, dataStart, len))));
                        break;
                    case "ExecuteDataSet":
                        e.DataToSend = encode.GetBytes(dac.ExecuteDataSet(JsonConvert.DeserializeObject<WrapedDatabaseParameter>(encode.GetString(token.theDataHolder.dataMessageReceived, dataStart, len))).GetXml());
                        break;
                    case "ExecutePagedDataSet":
                        e.DataToSend = encode.GetBytes(dac.ExecutePagedDataSet(JsonConvert.DeserializeObject<PagedRecordParameter>(encode.GetString(token.theDataHolder.dataMessageReceived, dataStart, len))).GetXml());
                        break;
                    case "Login":
                        e.DataToSend = encode.GetBytes(dac.Login(JsonConvert.DeserializeObject<LoginReq>(encode.GetString(token.theDataHolder.dataMessageReceived, dataStart, len))));
                        break;
                    case "Logout":
                        e.DataToSend = BitConverter.GetBytes(dac.Logout(encode.GetString(token.theDataHolder.dataMessageReceived, dataStart, len)));
                        break;
                    case "CheckLogin":
                        e.DataToSend = BitConverter.GetBytes(dac.CheckLogin(encode.GetString(token.theDataHolder.dataMessageReceived, dataStart, len)));
                        break;
                    case "Regist":
                        e.DataToSend = BitConverter.GetBytes(dac.Regist(JsonConvert.DeserializeObject<LoginReq>(encode.GetString(token.theDataHolder.dataMessageReceived, dataStart, len))));
                        break;
                    case "GetFile":
                        fileNameLen = BitConverter.ToInt32(token.theDataHolder.dataMessageReceived, dataStart);
                        fileName = Encoding.UTF8.GetString(token.theDataHolder.dataMessageReceived, headerLength + dataStart, fileNameLen);
                        e.DataToSend = dac.GetFile(fileName);
                        break;
                    case "SaveFile":
                        fileNameLen = BitConverter.ToInt32(token.theDataHolder.dataMessageReceived, dataStart);
                        fileName = Encoding.UTF8.GetString(token.theDataHolder.dataMessageReceived, headerLength + dataStart, fileNameLen);
                        int fileStart = dataStart + headerLength + fileNameLen;
                        len = len - headerLength - fileNameLen;
                        byte[] fileData = new byte[len];
                        Log.Write("收到文件长度:" + fileData.Length);
                        Buffer.BlockCopy(token.theDataHolder.dataMessageReceived, fileStart, fileData, 0, len);
                        e.DataToSend = encode.GetBytes(dac.SaveFile(fileName, fileData));

                        break;
                }
                Log.Write(LogAction.Info, className, "Socket_DataReceived", "end", "end", DateTime.Now.Ticks - t, "Socket_DataReceived:接口方法:" + methodName + "处理完成");
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }

        }
コード例 #5
0
ファイル: SocketListener.cs プロジェクト: kcitwm/dova
        //____________________________________________________________________________
        // This method is invoked by the IO_Completed method
        // when an asynchronous receive operation completes. 
        // If the remote host closed the connection, then the socket is closed.
        // Otherwise, we process the received data. And if a complete message was
        // received, then we do some additional processing, to 
        // respond to the client.
        private void ProcessReceive(SocketAsyncEventArgs receiveSendEventArgs)
        { 
            DataHoldingUserToken receiveSendToken = (DataHoldingUserToken)receiveSendEventArgs.UserToken;
            // If there was a socket error, close the connection. This is NOT a normal
            // situation, if you get an error here.
            // In the Microsoft example code they had this error situation handled
            // at the end of ProcessReceive. Putting it here improves readability
            // by reducing nesting some.
            if (receiveSendEventArgs.SocketError != SocketError.Success)
            { 
                receiveSendToken.Reset();
                CloseClientSocket(receiveSendEventArgs); 
                return;
            }

            // If no data was received, close the connection. This is a NORMAL
            // situation that shows when the client has finished sending data.
            if (receiveSendEventArgs.BytesTransferred == 0)
            {  
                receiveSendToken.Reset();
                CloseClientSocket(receiveSendEventArgs);  
                return;
            }

            //The BytesTransferred property tells us how many bytes 
            //we need to process.
            Int32 remainingBytesToProcess = receiveSendEventArgs.BytesTransferred;
 

            //If we have not got all of the prefix already, 
            //then we need to work on it here.                                
            if (receiveSendToken.receivedPrefixBytesDoneCount < this.socketListenerSettings.ReceivePrefixLength)
            { 
                remainingBytesToProcess = prefixHandler.HandlePrefix(receiveSendEventArgs, receiveSendToken, remainingBytesToProcess);
               
                if (remainingBytesToProcess == 0)
                {
                    // We need to do another receive op, since we do not have
                    // the message yet, but remainingBytesToProcess == 0. 
                    StartReceive(receiveSendEventArgs);
                    //Jump out of the method.
                    return;
                }
            }

            // If we have processed the prefix, we can work on the message now.
            // We'll arrive here when we have received enough bytes to read
            // the first byte after the prefix.  

            bool incomingTcpMessageIsReady = messageHandler.HandleMessage(receiveSendEventArgs, receiveSendToken, remainingBytesToProcess);
            

            if (incomingTcpMessageIsReady == true)
            {   

                // Pass the DataHolder object to the Mediator here. The data in
                // this DataHolder can be used for all kinds of things that an
                // intelligent and creative person like you might think of.  
                ReceiveArgs ra=new ReceiveArgs(receiveSendToken);
                OnDataReceived(ra); 
                receiveSendToken.theMediator.HandleData(receiveSendToken.theDataHolder); 

                // Create a new DataHolder for next message.
                receiveSendToken.CreateNewDataHolder();
                
                //Reset the variables in the UserToken, to be ready for the
                //next message that will be received on the socket in this
                //SAEA object.
                receiveSendToken.Reset();
               // Log.Info("ProcessReceive:10");
                if (null != ra.DataToSend)
                {
                    receiveSendToken.theMediator.PrepareOutgoingData(ra.DataToSend);
                    //Log.Info("ProcessReceive:11");

                    StartSend(receiveSendToken.theMediator.GiveBack());
                    //Log.Info("ProcessReceive:12");
                }
                else 
                    CloseClientSocket(receiveSendEventArgs);

            }
            else
            {
                // Since we have NOT gotten enough bytes for the whole message,
                // we need to do another receive op. Reset some variables first.

                // All of the data that we receive in the next receive op will be
                // message. None of it will be prefix. So, we need to move the 
                // receiveSendToken.receiveMessageOffset to the beginning of the 
                // receive buffer space for this SAEA.
                //Log.Info("ProcessReceive:13");  

                receiveSendToken.receiveMessageOffset = receiveSendToken.bufferOffsetReceive;

                // Do NOT reset receiveSendToken.receivedPrefixBytesDoneCount here.
                // Just reset recPrefixBytesDoneThisOp.
                receiveSendToken.recPrefixBytesDoneThisOp = 0;
                                
                StartReceive(receiveSendEventArgs);
                //Log.Info("ProcessReceive:14");  

            }            
        }