Beispiel #1
0
        private void WriteCallback(IAsyncResult asyncWrite)
        {
            WriteSocketObject obj = (WriteSocketObject)asyncWrite.AsyncState;

            try
            {
                Socket socket = obj.socket;
                if (socket != null && socket.Connected)
                {
                    int len = socket.EndSend(asyncWrite);
                    obj.WriteSuccessCallback();
                }
            }
            catch (SocketException e)
            {
                DebugUtils.LogError(DebugUtils.Type.AsyncSocket, "Async tcp socket has SocketException when sending request callback: " + e + ", error code = " + e.ErrorCode + ", native error code = " + e.NativeErrorCode + ", socket error code = " + e.SocketErrorCode);
                Disconnect(obj.WriteFailedCallback);
                ProcessDisconnnectInGame();
            }
            catch (Exception e)
            {
                DebugUtils.LogError(DebugUtils.Type.AsyncSocket, "Async tcp socket has Exception when sending request callback: " + e);
                Disconnect();
            }
            finally
            {
                sendTimeoutObject.Set();
            }
        }
        /// <summary>
        /// the functions sending the request
        /// </summary>
        public void SendRequest(long seq, byte[] data, int dataLength, Action sendRequestSuccessCallback, Action sendRequestFailedCallback)
        {
            if (isConnected && socket != null)
            {
                //DebugUtils.Log( DebugUtils.Type.AsyncSocket, "Send Start " + data.Length + " : " + DebugBuffer( data ) + ", " + DateTime.Now );

                try
                {
                    Segment segment = new Segment();
                    segment.seq  = seq;
                    segment.data = data;
                    waitingSegments.Add(segment);

                    WriteSocketObject obj = new WriteSocketObject();
                    obj.socket = socket;
                    obj.WriteSuccessCallback = sendRequestSuccessCallback;
                    obj.WriteFailedCallback  = sendRequestFailedCallback;

                    sendTimeoutObject.Reset();
                    socket.BeginSendTo(data, 0, dataLength, SocketFlags.None, server, new AsyncCallback(WriteCallback), obj);

                    bool signalled = sendTimeoutObject.WaitOne(TIMEOUT, false);

                    if (!signalled)
                    {
                        const string msg = "Write async udp socket timeout!";
                        throw new Exception(msg);
                    }
                    else
                    {
                        //DebugUtils.Log( DebugUtils.Type.AsyncSocket, "Async socket's BeginSend is complete!" );
                    }
                }
                catch (SocketException e)
                {
                    DebugUtils.LogError(DebugUtils.Type.AsyncSocket, "Async udp socket has SocketException when sending request: " + e + ", error code = " + e.ErrorCode + ", native error code = " + e.NativeErrorCode + ", socket error code = " + e.SocketErrorCode);
                    Disconnect(sendRequestFailedCallback);
                    //ProcessDisconnnectInGame();
                    return;
                }
                catch (Exception e)
                {
                    DebugUtils.LogError(DebugUtils.Type.AsyncSocket, "Async udp socket has Exception when sending requeset: " + e);
                    Disconnect(sendRequestFailedCallback);
                    return;
                }
            }
            else
            {
                string msg = string.Format("Udp socket to {0} is not prepared, but you still try to send message!", serverType);
                DebugUtils.LogError(DebugUtils.Type.AsyncSocket, msg);
                Loom.QueueOnMainThread(sendRequestFailedCallback);
            }
        }