Example #1
0
 /// <summary>
 /// 发送到服务
 /// </summary>
 /// <param name="msg"></param>
 public void SendMessage(SocketEntity msmqEntity)
 {
     try
     {
         if (msmqEntity == null)
         {
             return;
         }
         if (SocketHelper.IsSocketConnected_Poll(socket))
         {
             string strmsg = JSONSerialize.JsonSerialize_Newtonsoft(msmqEntity);
             byte[] data   = Encoding.UTF8.GetBytes(SocketHelper.SocketMsgSplit + strmsg + SocketHelper.SocketMsgSplit);
             socket.BeginSend(data, 0, data.Length, 0, new AsyncCallback(SendCallback), socket);
             OnSockConnEvent(new SocketStatusEventArgs(true));
         }
         else
         {
             Connect();
         }
     }
     catch (Exception ex)
     {
         logger.ErrorException("SendMessage", ex);
     }
 }
Example #2
0
        byte[] bsplit = Encoding.UTF8.GetBytes(SocketHelper.SocketMsgSplit); //分隔符
        private void ReceiveCallback(IAsyncResult ar)
        {
            StateObject state = (StateObject)ar.AsyncState;

            try
            {
                if (!state.workSocket.Connected)
                {
                    return;
                }
                int REnd = state.workSocket.EndReceive(ar);
                if (REnd > 0)
                {
                    for (int i = 0; i < REnd; i++)
                    {
                        state.lstBuffer.Add(state.buffer[i]);
                    }

                    string[] strmsgs = SocketHelper.SplitSocketMsg(ref state.lstBuffer, ref state.msgpart);
                    if (strmsgs != null)
                    {
                        foreach (string strtmp in strmsgs)
                        {
                            if (!string.IsNullOrEmpty(strtmp))
                            {
                                try
                                {
                                    SocketEntity msmqEntity = JSONSerialize.JsonDeserialize_Newtonsoft <SocketEntity>(strtmp);
                                    if (msmqEntity != null)
                                    {
                                        OnSockMsgEvent(new SocketEventArgs(msmqEntity));
                                    }
                                }
                                catch (Exception ex)
                                {
                                }
                            }
                        }
                    }
                    OnSockConnEvent(new SocketStatusEventArgs(true));
                }
                else
                {
                    //dispose();
                }
            }
            catch (SocketException sockex)
            {
                OnSockConnEvent(new SocketStatusEventArgs(false));
            }
            catch (Exception ex)
            {
                msgpart = "";
                OnSockMsgEvent(new SocketEventArgs(new SocketEntity(Entity.ConstValue.MSMQTYPE.Msg_Err, "解析Socket消息发生异常,ex:" + ex.Message)));
            }
            finally
            {
                try
                {
                    socket.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
                }
                catch
                { }
            }
        }