Esempio n. 1
0
 public static void Recv(Socket socket, OnRecvDlgt onRecv, OnExcpDlgt onExcp, OnRecvProgressDlgt onRecvProgress, int reciveBufferSize, bool recvLoop)
 {
     try
     {
         // Create the state object.
         RecvState state = new RecvState();
         state.m_Socket         = socket;
         state.m_OnRecv         = onRecv;
         state.m_OnRecvProgress = onRecvProgress;
         state.m_OnExcp         = onExcp;
         state.m_recvLoop       = recvLoop;
         state.m_buf            = (recvLoop ? new byte[reciveBufferSize] : new byte[1]);
         StartRecvPacket(state);
     }
     catch (Exception e)
     {
         if (!(e is System.ObjectDisposedException))
         {
         }
     }
 }
Esempio n. 2
0
        // Send a string on the given socket.
        // The onSocket delegate will be executed once the send action has ended
        public static void Send(Socket socket, byte[] bufferToSend, OnSendDlgt onSend, OnExcpDlgt onExcp)
        {
            try
            {
                byte[] lenBuf;

                ConvertIntToArray(bufferToSend.Length, out lenBuf);

                SendState state = new SendState();
                state.m_sendId         = Interlocked.Increment(ref _sendId);
                state.m_socket         = socket;
                state.m_onSend         = onSend;
                state.m_onExcp         = onExcp;
                state.m_buf            = new byte[lenBuf.Length + bufferToSend.Length];
                state.m_bytesLenBuf    = lenBuf.Length;
                state.m_bytesSentSoFar = 0;
                state.m_startSendTime  = DateTime.UtcNow;
                Buffer.BlockCopy(lenBuf, 0, state.m_buf, 0, lenBuf.Length);
                Buffer.BlockCopy(bufferToSend, 0, state.m_buf, lenBuf.Length, bufferToSend.Length);

                //Begin sending the data to the remote device.
                var asyncResult = state.m_socket.BeginSend(
                    state.m_buf,
                    0,
                    state.m_buf.Length,
                    0,
                    new AsyncCallback(SendCallback),
                    state);


                //var isSignaled = asyncResult.AsyncWaitHandle.WaitOne(0);
                //if (isSignaled == false)
                //{
                //    ThreadPool.RegisterWaitForSingleObject(asyncResult.AsyncWaitHandle, SendCompleteCallback, state, -1, true);
                //    Console.WriteLine($"* asyncResult: IsCompleted:{asyncResult.IsCompleted} socket Handle: {state.m_socket.Handle.ToInt32()} sendId: #{state.m_sendId}");
                //}
            }
            catch (ObjectDisposedException)
            {
                // DisposedException is received if the current host initiated
                // a disconnection. In this case - do nothing.
            }
            catch (Exception e)
            {
                onExcp(e);
            }
        }