static public void Disconnect(ConnObj con) { if (con.Connected) { con.mySocket.Shutdown(SocketShutdown.Both); con.mySocket.Close(); } //con.Disconnect }
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); }
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); }
static public void BeginReceive(ConnObj con) { try { con.mySocket.BeginReceive( con.Buffer, 0, con.Buffer.Length, SocketFlags.None, EndRcvCB, con); } catch (Exception) { } }
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(); } } }
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) { } } }
static private void EndSend(IAsyncResult ar) { ConnObj obj = (ConnObj)ar.AsyncState; obj.mySocket.EndSend(ar); }