Exemple #1
0
 private void OnSocketData(string strEndPoint, OVRSocketBuffer data)
 {
     if (EventDataReceived != null)
     {
         EventDataReceived(strEndPoint, data);
     }
 }
        public OVRSocketClient(TcpClient tcpClient)
        {
            client                = tcpClient;
            EventDataReceived     = null;
            EventConnectionDroped = null;
            EventSocketError      = null;
            eventReceiveStop      = new EventWaitHandle(true, EventResetMode.ManualReset);
            eventSendStop         = new EventWaitHandle(true, EventResetMode.ManualReset);
            eventConnectionDroped = new EventWaitHandle(false, EventResetMode.ManualReset);
            eventSendData         = new AutoResetEvent(false);
            sendBuffer            = new OVRSocketBuffer();
            sendBufferQueue       = new Queue();

            keep_alive = new byte[sizeof(uint) * 3];
            //OnOff
            //    - This parameter determines whether keep alive is on or off. 0: off, 1: on
            BitConverter.GetBytes((uint)1).CopyTo(keep_alive, 0);
            //KeepAliveInterval
            //    - This parameter determines the interval separating keep alive retransmissions until a response is received.
            BitConverter.GetBytes((uint)5000).CopyTo(keep_alive, sizeof(uint));
            //KeepAliveTime
            //    - This parameter controls how often TCP attempts to verify that an idle connection is still intact by sending a keep alive packet.
            BitConverter.GetBytes((uint)1000).CopyTo(keep_alive, sizeof(uint) * 2);

            lock (strRemoteEndPoint)
            {
                strRemoteEndPoint = client.Client.RemoteEndPoint.ToString();
            }
        }
Exemple #3
0
 private void OnInokeSocketData(string strEndPoint, OVRSocketBuffer data)
 {
 }
        private void Receive()
        {
            if (this.client == null)
            {
                return;
            }

            eventReceiveStop.Reset();

            System.Diagnostics.Trace.Write("Receive Thread Enter: " + strRemoteEndPoint + "\n");

            OVRSocketBuffer rcvdBuffer = new OVRSocketBuffer(BUFFER_SIZE);

            while (true)
            {
                try
                {
                    int bytesReaded = client.Client.Receive(rcvdBuffer.Buffer);
                    // If no data any more or the Server is closed, Close Client itself.client.ReceiveBufferSize
                    if (bytesReaded <= 0)
                    {
                        System.Diagnostics.Trace.Write("Receive Thread Break On Disconnect: " + strRemoteEndPoint + "\n");

                        eventConnectionDroped.Set();

                        System.Diagnostics.Trace.Write("OnConnectionDroped Called At Receive Disconnect: " + strRemoteEndPoint + "\n");

                        if (EventConnectionDroped != null)
                        {
                            EventConnectionDroped(this.RemoteEndPoint, SocketError.ConnectionReset);
                        }

                        break;
                    }

                    rcvdBuffer.DataBytes += bytesReaded;

                    System.Diagnostics.Trace.Write("Receive Bytes: " + bytesReaded.ToString() + "\n");

                    // Process Received Data
                    if (EventDataReceived != null)
                    {
                        EventDataReceived(this.RemoteEndPoint, rcvdBuffer);
                    }

                    // Reset Received Buffer
                    rcvdBuffer.DataBytes = 0;
                }
                catch (SocketException ex)
                {
                    if (IsConnectionDroped(ex.SocketErrorCode))
                    {
                        System.Diagnostics.Trace.Write("Receive Thread Break On Exception: " + strRemoteEndPoint + "---" + ex.Message + "Error Code: " + ex.SocketErrorCode.ToString() + "\n");

                        eventConnectionDroped.Set();

                        if (Monitor.TryEnter(syncStop, 10)) // 如果进入则表明Socket不是主动关闭的。

                        {
                            System.Diagnostics.Trace.Write("OnConnectionDroped Called At Receive Exception: " + strRemoteEndPoint + "\n");

                            try
                            {
                                if (EventConnectionDroped != null)
                                {
                                    EventConnectionDroped(this.RemoteEndPoint, ex.SocketErrorCode);
                                }
                            }
                            finally
                            {
                                Monitor.Exit(syncStop);
                            }
                        }

                        break;
                    }
                    else
                    {
                        System.Diagnostics.Trace.Write("Receive Error: " + strRemoteEndPoint + "---" + ex.Message + "Error Code: " + ex.SocketErrorCode.ToString() + "\n");

                        if (EventSocketError != null)
                        {
                            EventSocketError(this.RemoteEndPoint, ex.SocketErrorCode);
                        }
                    }
                }
            }

            System.Diagnostics.Trace.Write("Receive Thread Exit: " + strRemoteEndPoint + "\n");

            eventReceiveStop.Set();
        }
        public bool SendData(Byte[] data)
        {
            lock (this.client)
            {
                if (this.client == null || this.client.Client == null || !this.client.Client.Connected)
                {
                    return(false);
                }
            }

            bool bQueueIt = true;

            if (Monitor.TryEnter(sendBuffer, 100))
            {
                try
                {
                    if (sendBuffer.DataBytes < 1)   // sendBuffer is Empty
                    {
                        // Put message in sendBuffer
                        if (data.Length > sendBuffer.Size)
                        {
                            sendBuffer.ReAlloc(data.Length);
                            System.Diagnostics.Trace.Write("sendBuffer.ReAlloc.\n");
                        }

                        data.CopyTo(sendBuffer.Buffer, 0);
                        sendBuffer.DataBytes = data.Length;

                        System.Diagnostics.Trace.Write("Data Filled in sendBuffer.\n");

                        bQueueIt = false;
                    }
                }
                finally
                {
                    Monitor.Exit(sendBuffer);
                }
            }

            if (bQueueIt)  // queue it...
            {
                lock (sendBufferQueue)
                {
                    if (sendBufferQueue.Count >= MAX_QUEUE_SIZE)   // sendBufferQueue Overflow.
                    {
                        return(false);
                    }

                    OVRSocketBuffer buffer = new OVRSocketBuffer(data.Length);
                    data.CopyTo(buffer.Buffer, 0);
                    buffer.DataBytes = data.Length;
                    sendBufferQueue.Enqueue(buffer);

                    System.Diagnostics.Trace.Write("Data Enqueued at sendBufferQueue: " + sendBufferQueue.Count.ToString() + "\n");
                }
            }

            eventSendData.Set();

            return(true);
        }
        public bool SendMessage(string message)
        {
            if (message == null)
            {
                return(true);
            }

            lock (this.client)
            {
                if (this.client == null || this.client.Client == null || !this.client.Client.Connected)
                {
                    return(false);
                }
            }

            bool bQueueIt = true;

            if (Monitor.TryEnter(sendBuffer, 100))
            {
                try
                {
                    if (sendBuffer.DataBytes < 1)   // sendBuffer is Empty
                    {
                        // Put message in sendBuffer
                        int iByteCount = Encoding.UTF8.GetByteCount(message);
                        if (iByteCount + 5 > sendBuffer.Size)
                        {
                            sendBuffer.ReAlloc(iByteCount * 2);
                            System.Diagnostics.Trace.Write("sendBuffer.ReAlloc.\n");
                        }

                        sendBuffer.Buffer[0] = 0x01; //sendBuffer.Buffer[1] = 0x00; // SOH
                        int iBytes = Encoding.UTF8.GetBytes(message, 0, message.Length, sendBuffer.Buffer, 5);
                        BitConverter.GetBytes(iBytes).CopyTo(sendBuffer.Buffer, 1);
                        //sendBuffer.Buffer[iBytes + 2] = 0x04; sendBuffer.Buffer[iBytes + 3] = 0x00; // EOT

                        sendBuffer.DataBytes = iBytes + 5;

                        System.Diagnostics.Trace.Write("Data Filled in sendBuffer.\n");

                        bQueueIt = false;
                    }
                }
                finally
                {
                    Monitor.Exit(sendBuffer);
                }
            }

            if (bQueueIt)  // queue it...
            {
                lock (sendBufferQueue)
                {
                    if (sendBufferQueue.Count >= MAX_QUEUE_SIZE)   // sendBufferQueue Overflow.
                    {
                        return(false);
                    }

                    int             iByteCount = Encoding.Unicode.GetByteCount(message);
                    OVRSocketBuffer buffer     = new OVRSocketBuffer(iByteCount + 5);

                    sendBuffer.Buffer[0] = 0x01; //sendBuffer.Buffer[1] = 0x00; // SOH
                    int iBytes = Encoding.UTF8.GetBytes(message, 0, message.Length, sendBuffer.Buffer, 5);
                    BitConverter.GetBytes(iBytes).CopyTo(sendBuffer.Buffer, 1);
                    //sendBuffer.Buffer[iBytes + 2] = 0x04; sendBuffer.Buffer[iBytes + 3] = 0x00; // EOT

                    sendBuffer.DataBytes = iBytes + 5;

                    sendBufferQueue.Enqueue(buffer);

                    System.Diagnostics.Trace.Write("Data Enqueued at sendBufferQueue: " + sendBufferQueue.Count.ToString() + "\n");
                }
            }

            eventSendData.Set();

            return(true);
        }