Пример #1
0
 /// <summary>
 /// send request to server
 /// se.SocketErrorCode == SocketError.WouldBlock ||
 /// se.SocketErrorCode == SocketError.IOPending ||
 ///            se.SocketErrorCode == SocketError.NoBufferSpaceAvailable
 /// </summary>
 /// <param name="nCommand">command unique id</param>
 /// <param name="msg">message content</param>
 /// <param name="length">the length of mesage</param>
 /// <param name="bNeedRestore">whether need to be restore</param>
 public void DoSendMsg(byte[] msg)
 {
     try
     {
         //byte[] tmpMsg = System.Text.Encoding.Default.GetBytes(msg);
         if (msg != null)
         {
             m_socket.BeginSend(msg, 0, msg.Length, SocketFlags.None, new System.AsyncCallback(SendCallback), null);
         }
         else
         {
             sendNoti = new FSendMsgResult(1, "parameter can not be null", false);
         }
     }
     catch (System.Net.Sockets.SocketException se)
     {
         string         tip   = string.Format("Send sockect exception errorcode: {0}, errormsg: {1}", se.NativeErrorCode, se.ToString());
         FSendMsgResult state = new FSendMsgResult((int)se.SocketErrorCode, tip);
         sendNoti = new FSendMsgResult((int)se.SocketErrorCode, tip, false);
     }
     catch (System.Exception e)
     {
         sendNoti = new FSendMsgResult(-100, e.ToString(), false);
     }
 }
Пример #2
0
 static public int get_bPassed(IntPtr l)
 {
     try {
         FSendMsgResult self = (FSendMsgResult)checkSelf(l);
         pushValue(l, true);
         pushValue(l, self.bPassed);
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
Пример #3
0
 static public int set_reason(IntPtr l)
 {
     try {
         FSendMsgResult self = (FSendMsgResult)checkSelf(l);
         System.String  v;
         checkType(l, 2, out v);
         self.reason = v;
         pushValue(l, true);
         return(1);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
Пример #4
0
 static public int set_errorCode(IntPtr l)
 {
     try {
         FSendMsgResult self = (FSendMsgResult)checkSelf(l);
         System.Int32   v;
         checkType(l, 2, out v);
         self.errorCode = v;
         pushValue(l, true);
         return(1);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
Пример #5
0
 static public int set_bPassed(IntPtr l)
 {
     try {
         FSendMsgResult self = (FSendMsgResult)checkSelf(l);
         System.Boolean v;
         checkType(l, 2, out v);
         self.bPassed = v;
         pushValue(l, true);
         return(1);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
Пример #6
0
 private void SendCallback(System.IAsyncResult ia)
 {
     try
     {
         if (m_socket.Connected)
         {
             m_socket.EndSend(ia);
         }
     }
     catch (System.Exception e)
     {
         string tip = e.ToString() + " type::" + ia.AsyncState.GetType().ToString();
         sendNoti = new FSendMsgResult(1, tip);
     }
 }
Пример #7
0
    /// <summary>
    /// receive message
    /// </summary>
    public void ReceiveMessage(out byte[] msg)
    {
        msg = null;
        int count = 0;

        try
        {
            if (m_socket.Available != 0)
            {
                m_arrTempRecv = new byte[512];
                count         = m_socket.Receive(m_arrTempRecv);
                if (count > 0)
                {
                    byte[] tmp = new byte[count];
                    System.Array.Copy(m_arrTempRecv, tmp, count);
                    //LaunchGame.Instance.PushLString(tmp);
                    msg = tmp;
                }
                if (count > RECV_BUF_LEN)
                {
                    //@todo cast a exception??
                    //throw new SocketException();
                    FSendMsgResult result = new FSendMsgResult(1, "the message length is lager than RECV_BUF_LEN=" + RECV_BUF_LEN.ToString());
                    if (null != onFailedReceiveMsg)
                    {
                        onFailedReceiveMsg.Invoke(result);
                    }
                }
            }
        }
        catch (SocketException e)
        {
            string reason = string.Format("Receive sockect exception errorcode: {0}, errormsg: {1}", e.SocketErrorCode, e.ToString());
            //if (e.SocketErrorCode == SocketError.WouldBlock ||
            //    e.SocketErrorCode == SocketError.IOPending ||
            //    e.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
            //{
            //    System.Threading.Thread.Sleep(10);
            //}

            FSendMsgResult result = new FSendMsgResult((int)e.SocketErrorCode, reason);
            if (null != onFailedReceiveMsg)
            {
                onFailedReceiveMsg.Invoke(result);
            }
        }
    }
Пример #8
0
 static public int constructor(IntPtr l)
 {
     try {
         FSendMsgResult o;
         System.Int32   a1;
         checkType(l, 2, out a1);
         System.String a2;
         checkType(l, 3, out a2);
         System.Boolean a3;
         checkType(l, 4, out a3);
         o = new FSendMsgResult(a1, a2, a3);
         pushValue(l, true);
         pushValue(l, o);
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
Пример #9
0
    void Update()
    {
        switch (m_socketState)
        {
        case ESocketState.ESS_Connectting:
            if (Time.realtimeSinceStartup - m_ConnectStartTime >= m_ConnectInternal)
            {
                m_socketState = ESocketState.ESS_ConnecttingFailed;
            }
            break;

        case ESocketState.ESS_ConnecttingFailed:
            if (m_socket != null)
            {
                Disconnect();
                connNoti = new FSocketStateChanged(ESocketState.ESS_ConnecttingFailed, "connection out of date");
                _onSocketStateChanged.Invoke(connNoti);
                connNoti = null;
            }
            break;

        case ESocketState.ESS_Connected:
            if (connNoti != null)
            {
                _onSocketStateChanged.Invoke(connNoti);
                connNoti = null;
            }

            if (sendNoti != null)
            {
                _onFailedSendMsg.Invoke(sendNoti);
                sendNoti = null;
            }

            if (m_socket != null)
            {
                if (m_socket.Connected == false)
                {
                    m_socketState = ESocketState.ESS_Disconnected;
                }
                else if (m_socket.Poll(100, SelectMode.SelectRead) && m_socket.Available == 0)
                {
                    m_socketState = ESocketState.ESS_Disconnected;
                }
            }
            break;

        case ESocketState.ESS_Disconnected:
            if (m_socket != null)
            {
                Disconnect();
                connNoti = new FSocketStateChanged(ESocketState.ESS_Disconnected, "Disconnected");
                _onSocketStateChanged.Invoke(connNoti);
                connNoti = null;
            }
            break;

        case ESocketState.ESS_Quit:
            break;
        }
    }