private void ReadCallBack(IAsyncResult AsyncResult) { try { Socket SocketClient = (Socket)AsyncResult.AsyncState; ReadSize = SocketClient.EndReceive(AsyncResult); } #if DEBUG_SOCKET catch (Exception e) { AccessoryLibrary.ShowException(e); ReadSize = -1; } #else catch (Exception ex) { ReadSize = -1; logger.Error(ex.ToString() + "\n"); } #endif finally { ReceiveDone.Set(); } }
public int Read(Byte[] Data) { ReadSize = 0; SendSocketDataAvailableNotification = false; if (IsConnected && (_SocketClient.Available > 0)) { try { #if ASYNC_RECEIVE ReceiveDone.Reset(); int ByteToRead = Math.Min(Data.Length, _SocketClient.Available); _SocketClient.BeginReceive(Data, 0, ByteToRead, SocketFlags.None, new AsyncCallback(ReadCallBack), _SocketClient); ReceiveDone.WaitOne(); #else ReadSize = _SocketClient.Receive(Data); #endif } #if DEBUG_SOCKET catch (Exception e) { AccessoryLibrary.ShowException(e); ReceiveDone.Set(); ReadSize = -1; } #else catch (Exception ex) { ReceiveDone.Set(); ReadSize = -1; logger.Error(ex.ToString() + "\n"); } #endif } /* * in the calling procedure * * After calling Read() * ResetDataAvailableNotification() must be called again * to enable data notification * */ return(ReadSize); }
private void SendCallBack(IAsyncResult AsyncResult) { try { Socket SocketClient = (Socket)AsyncResult.AsyncState; SocketClient.EndSend(AsyncResult); } #if DEBUG_SOCKET catch (Exception e) { AccessoryLibrary.ShowException(e); } #else catch (Exception ex) { logger.Error(ex.ToString() + "\n"); } #endif }
public void Disconnect() { // Socket.DisconnectAsync() is new in .NET 2.0 SP1 // not available in original .NET 2.0 SendSocketDataAvailableNotification = false; if (IsConnected) { try { DisconnectDone.Reset(); SocketClient.BeginDisconnect(false, new AsyncCallback(DisconnectCallBack), SocketClient); DisconnectDone.WaitOne(); SocketClient = null; //if (OnEventMessage != null) { OnEventMessage(this, new EventSocketPortMessage(SocketPortMessageType.Disconnected)); } #if FIRE_EVENT_BY_BEGININVOKE if (OnEventMessage != null) { OnEventMessage.BeginInvoke(this, new SocketPortMessageEventArgs(SocketMessageType.Disconnected), null, null); } #else if (OnEventMessage != null) { OnEventMessage(this, new SocketPortMessageEventArgs(SocketMessageType.Disconnected)); } #endif } #if DEBUG_SOCKET catch (Exception e) { AccessoryLibrary.ShowException(e); DisconnectDone.Set(); } #else catch (Exception ex) { DisconnectDone.Set(); logger.Error(ex.ToString() + "\n"); } #endif } }
public void ResetDataAvailableNotification() { try { SendSocketDataAvailableNotification = (SocketClient != null) && (SocketClient.Connected); } #if DEBUG_SOCKET catch (Exception e) { AccessoryLibrary.ShowException(e); SendSocketDataAvailableNotification = false; } #else catch (Exception ex) { SendSocketDataAvailableNotification = false; logger.Error(ex.ToString() + "\n"); } #endif }
public void Disconnect() { SendSocketDataAvailableNotification = false; if (IsConnected) { try { SocketClient.Shutdown(SocketShutdown.Both); SocketClient.Disconnect(false); } #if DEBUG_SOCKET catch (Exception e) { AccessoryLibrary.ShowException(e); } #else catch { } #endif } }
private void DisconnectCallBack(IAsyncResult AsyncResult) { try { Socket ClientSocket = (Socket)AsyncResult.AsyncState; ClientSocket.EndDisconnect(AsyncResult); } #if DEBUG_SOCKET catch (Exception e) { AccessoryLibrary.ShowException(e); } #else catch (Exception ex) { logger.Error(ex.ToString() + "\n"); } #endif finally { DisconnectDone.Set(); } }
public void DiscardInBuffer() { SendSocketDataAvailableNotification = false; if (IsConnected && (_SocketClient.Available > 0)) { try { Byte[] Data = new Byte[_SocketClient.Available * 2]; #if ASYNC_RECEIVE ReceiveDone.Reset(); _SocketClient.BeginReceive(Data, 0, Data.Length, SocketFlags.None, new AsyncCallback(ReadCallBack), _SocketClient); ReceiveDone.WaitOne(); #else ReadSize = _SocketClient.Receive(Data); #endif } #if DEBUG_SOCKET catch (Exception e) { AccessoryLibrary.ShowException(e); ReceiveDone.Set(); } #else catch (Exception ex) { ReceiveDone.Set(); logger.Error(ex.ToString() + "\n"); } #endif finally { ReadSize = 0; } } ResetDataAvailableNotification(); }
public void Connect() { IPAddress IP = null; IPAddress.TryParse(RemoteIpAddress, out IP); SendSocketDataAvailableNotification = false; if (IP == null) { try { IPAddress[] IPS = Dns.GetHostAddresses(RemoteIpAddress); if ((IPS != null) && (IPS.Length > 0)) { IP = IPS[0]; } } #if DEBUG_SOCKET catch (Exception e) { AccessoryLibrary.ShowException(e); } #else catch (Exception ex) { logger.Error(ex.ToString() + "\n"); } #endif } if (IP != null) { SocketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); SocketClient.Blocking = false; try { #if FIRE_EVENT_BY_BEGININVOKE if (OnEventMessage != null) { OnEventMessage.BeginInvoke(this, new SocketPortMessageEventArgs(SocketMessageType.Connecting), null, null); } #else if (OnEventMessage != null) { OnEventMessage(this, new SocketPortMessageEventArgs(SocketMessageType.Connecting)); } #endif SocketClient.ReceiveBufferSize = ClientSocketControl.ReceiveBufferSize; ConnectDone.Reset(); SocketClient.BeginConnect(new IPEndPoint(IP, IpPortNumber), new AsyncCallback(ConnectCallBack), SocketClient); ConnectDone.WaitOne(); if (IsConnected) { BeginSocketThread(); SendSocketDataAvailableNotification = true; #if FIRE_EVENT_BY_BEGININVOKE if (OnEventMessage != null) { OnEventMessage.BeginInvoke(this, new SocketPortMessageEventArgs(SocketMessageType.Connected), null, null); } #else if (OnEventMessage != null) { OnEventMessage(this, new SocketPortMessageEventArgs(SocketMessageType.Connected)); } #endif } else { SocketClient.Close(); SocketClient = null; #if FIRE_EVENT_BY_BEGININVOKE if (OnEventMessage != null) { OnEventMessage.BeginInvoke(this, new SocketPortMessageEventArgs(SocketMessageType.ConnectFailed), null, null); } #else if (OnEventMessage != null) { OnEventMessage(this, new SocketPortMessageEventArgs(SocketMessageType.ConnectFailed)); } #endif } } catch (Exception ex) { ConnectDone.Set(); SocketClient.Close(); SocketClient = null; logger.Error(ex.ToString() + "\n"); #if FIRE_EVENT_BY_BEGININVOKE if (OnEventMessage != null) { OnEventMessage.BeginInvoke(this, new SocketPortMessageEventArgs(SocketMessageType.ConnectFailed), null, null); } #else if (OnEventMessage != null) { OnEventMessage(this, new SocketPortMessageEventArgs(SocketMessageType.ConnectFailed)); } #endif } } else { #if FIRE_EVENT_BY_BEGININVOKE if (OnEventMessage != null) { OnEventMessage.BeginInvoke(this, new SocketPortMessageEventArgs(SocketMessageType.ConnectFailed), null, null); } #else if (OnEventMessage != null) { OnEventMessage(this, new SocketPortMessageEventArgs(SocketMessageType.ConnectFailed)); } #endif } }