예제 #1
0
 static public void Disconnect(ConnObj con)
 {
     if (con.Connected)
     {
         con.mySocket.Shutdown(SocketShutdown.Both);
         con.mySocket.Close();
     }
     //con.Disconnect
 }
예제 #2
0
 static public bool Send(ConnObj con, byte[] buffer)
 {
     if (con.Connected)
     {
         con.mySocket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None,
                                EndSendCB, con);
         return(true);
     }
     return(false);
 }
예제 #3
0
 static public bool Connect(ConnObj con)
 {
     if (!con.Connected)
     {
         IAsyncResult ar =
             con.mySocket.BeginConnect(con.Ip, con.Port, EndConnCB, con);
         ar.AsyncWaitHandle.WaitOne();
         return(con.Connected);
     }
     return(con.Connected);
 }
예제 #4
0
 static public void BeginReceive(ConnObj con)
 {
     try
     {
         con.mySocket.BeginReceive(
             con.Buffer, 0, con.Buffer.Length,
             SocketFlags.None, EndRcvCB, con);
     }
     catch (Exception)
     {
     }
 }
예제 #5
0
        static private void EndRcv(IAsyncResult ar)
        {
            ConnObj con = (ConnObj)ar.AsyncState;

            int size;

            try
            {
                size = con.mySocket.EndReceive(ar);
            }
            catch (ObjectDisposedException)
            {
                return;
            }
            catch (SocketException ex)
            {
                if (ex.ErrorCode == 0x00002746)  //server forced a socket closed
                {
                    con.BroadcastDisconnected();
                }
                else
                {
                }
                return;
            }
            catch (InvalidOperationException ex)
            {
                return;
            }

            byte[] buffer = new byte[size];
            Buffer.BlockCopy(con.Buffer, 0, buffer, 0, size);
            con.BroadcastRcv(buffer);

            try
            {
                BeginReceive(con);
            }
            catch (SocketException se)
            {
                if (se.SocketErrorCode == SocketError.Shutdown ||
                    se.SocketErrorCode == SocketError.ConnectionAborted)
                {
                    con.BroadcastDisconnected();
                }
            }
        }
예제 #6
0
        static private void EndConnect(IAsyncResult ar)
        {
            ConnObj con = (ConnObj)ar.AsyncState;

            con.mySocket.EndConnect(ar);

            if (con.mySocket.Connected)
            {
                try
                {
                    BeginReceive(con);
                }
                catch (SocketException se)
                {
                }
            }
        }
예제 #7
0
        static private void EndSend(IAsyncResult ar)
        {
            ConnObj obj = (ConnObj)ar.AsyncState;

            obj.mySocket.EndSend(ar);
        }