Beispiel #1
0
        /// <summary>
        /// End asynchronous send of command to the network,
        /// and send out the rest of the packet.
        /// </summary>
        private void InternalSend(IAsyncResult ar)
        {
            try
            {
                if (socket == null)
                {
                    return;
                }
                if (socket.Connected)
                {
                    socket.Send(Encoding.ASCII.GetBytes(metatype.ToString()));
                }
                //int sent = socket.EndSend(ar);
                //if (sent > 0)
                //{
                //    sentBytes += (ulong)sent;

                //  //  InternalSendCommandTarget();
                //    InternalSendMetadataType();
                //   // InternalSendMetaBuffer();

                //    // fire Sent event only if all the Send routines
                //    // completed succesfully
                //    if (Sent != null)
                //        Sent(this, EventArgs.Empty);
                //}
                //else
                //{
                //    Disconnect();
                //}
            }
            catch (Exception exc)
            {
                if (SockUtils.HandleSocketError(exc))
                {
                    if (Disconnected != null)
                    {
                        Disconnected(this, EventArgs.Empty);
                    }
                }
                else
                {
                    throw;
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Read a ChatSocket from the network stream asynchronously.
        /// </summary>
        public void Receive()
        {
            if (socket == null)
            {
                return;
            }

            try
            {
                if (socket.Connected)
                {
                    // stores the received data
                    //  byte[] result = new byte[1024];
                    //   int receiveNumber = socket.Receive(result);
                    //  Metatype = Encoding.ASCII.GetString(result, 0, receiveNumber);

                    byte[] buffer = new byte[4];
                    socket.BeginReceive(
                        buffer, 0, 4, SocketFlags.None,
                        new AsyncCallback(InternalReceive), buffer);
                }
                else
                {
                    WmsCommon.Instance().CloseSocket(this);
                }
            }
            catch (Exception exc)
            {
                WmsCommon.Instance().CloseSocket(this);
                if (SockUtils.HandleSocketError(exc))
                {
                    if (Disconnected != null)
                    {
                        Disconnected(this, EventArgs.Empty);
                    }
                }
                //else
                //{
                //    throw exc;
                //}
            }
        }
Beispiel #3
0
        /// <summary>
        /// Write out this ChatSocket object to the networkStream asynchronously.
        /// </summary>
        public void Send()
        {
            if (socket == null)
            {
                return;
            }

            try
            {
                byte[] buffer = new byte[4];
                buffer = BitConverter.GetBytes((int)command);
                if (socket.Connected)
                {
                    socket.Send(Encoding.ASCII.GetBytes(metatype.ToString()));
                    if (WmsCommon.Instance().clients.Count(r => r.scocKet == socket) > 0)
                    {
                        WmsCommon.Instance().clients.Find(r => r.scocKet == socket).statusSocket = "1";
                    }
                }
            }
            catch (Exception exc)
            {
                WmsCommon.Instance().CloseSocket(this);
                if (SockUtils.HandleSocketError(exc))
                {
                    if (Disconnected != null)
                    {
                        Disconnected(this, EventArgs.Empty);
                    }
                }
                //else
                //{
                //    throw;
                //}
            }
        }
Beispiel #4
0
        /// <summary>
        /// End asynchronous receive of command from the network,
        /// and continues to read the rest of the packet.
        /// </summary>
        private void InternalReceive(IAsyncResult ar)
        {
            try
            {
                if (socket == null)
                {
                    return;
                }
                if (socket.Connected)
                {
                    int received = socket.EndReceive(ar);
                    if (received > 0)
                    {
                        byte[] buffer = (byte[])ar.AsyncState;


                        Metatype = Encoding.ASCII.GetString(buffer, 0, received);
                        byte[] result        = new byte[1024];
                        int    receiveNumber = socket.Receive(result);

                        Metatype = Metatype + Encoding.ASCII.GetString(result, 0, receiveNumber);


                        //receivedBytes += (ulong)received;

                        //byte[] result = new byte[1024];
                        //result = (byte[])ar.AsyncState;
                        //int bufferVal = BitConverter.ToInt32(buffer, 0);
                        //Encoding.ASCII.GetString(result, 0, receiveNumber);
                        // check if it is a know command

                        command = (ChatCommand)7;
                        //command = (ChatCommand)bufferVal;
                        //Trace.Write(string.Format("ChatSocket RECEIVE from {1} -> {0} {2}",
                        //    socket.LocalEndPoint.ToString(), socket.RemoteEndPoint.ToString(), command.ToString()));

                        ////  InternalReceiveCommandTarget();
                        //  InternalReceiveMetadataType();
                        // InternalReceiveMetaBuffer();

                        // fire the Received event only if all the Receive routines
                        // completed succesfully
                        if (Received != null)
                        {
                            Received(this, EventArgs.Empty);
                        }
                    }

                    //else
                    //{
                    //    // it is an unknown command so close the socket
                    //    Disconnect();
                    //}
                }
                else // received == 0
                {
                    Disconnect();
                }
            }
            catch (Exception exc)
            {
                WmsCommon.Instance().CloseSocket(this);

                if (SockUtils.HandleSocketError(exc))
                {
                    if (Disconnected != null)
                    {
                        Disconnected(this, EventArgs.Empty);
                    }
                }
                else
                {
                    //  throw exc;
                }
            }
        }