/// <summary> /// Terminate the connection as client and start listening for protocol messages /// </summary> /// <param name="ar"></param> private void TcpConnectionCallbackClient(IAsyncResult ar) { try { //Retrive socket information Socket handler = (Socket)ar.AsyncState; //complete the connections handler.EndConnect(ar); this._log = "Connection created"; NetStateObject netStateObject = new NetStateObject(); netStateObject.socket = handler; //Start to listen for data from the server handler.BeginReceive(netStateObject.recBuffer, 0, netStateObject.bufferSize, 0, new AsyncCallback(ReceiveCallback), netStateObject); //Wait 0.5sec before return System.Threading.Thread.Sleep(500); //Raise the connection state change event this.OnConnectionStateChanged(); } catch (Exception ex) { this._log = "Error: " + ex.Message; } }
/// <summary> /// Finalizza la connessione, quando la connessione TCP è stabilita solleva evento OnConnectionStateChanged /// </summary> /// <param name="ar"></param> private void TcpConnectionCallbackServer(IAsyncResult ar) { try { Socket listener = (Socket)ar.AsyncState; Socket handler = listener.EndAccept(ar); this._remote = handler; this._log = "Connection created"; NetStateObject netStateObject = new NetStateObject(); netStateObject.socket = handler; //Begin receive for remote messages handler.BeginReceive(netStateObject.recBuffer, 0, netStateObject.bufferSize, 0, new AsyncCallback(ReceiveCallback), netStateObject); //Wait 0.5sec before return System.Threading.Thread.Sleep(500); //Raise the connection state change event this.OnConnectionStateChanged(); } catch (Exception ex) { this._log = "Error: " + ex.Message; } }
/// <summary> /// Async received data callback method /// </summary> /// <param name="ar">async parameter</param> private void ReceiveCallback(IAsyncResult ar) { try { //Retrieve the NetObject and the client socket from the asynchronous state object NetStateObject netStateObj = (NetStateObject)ar.AsyncState; Socket handler = netStateObj.socket; //read data from the remote server int bytesRead = handler.EndReceive(ar); if (bytesRead > 0) { //There might be more data, so store the data received so far netStateObj.receivedMex_Sb.Append(Encoding.ASCII.GetString(netStateObj.recBuffer, 0, bytesRead)); //Check if there is a complete message string string recMex = netStateObj.receivedMex_Sb.ToString(); //first occurence of trailer starting from end of message int trailerPosition = recMex.IndexOf(NetMessages.packetTrailer); //last occurence of the header starting from the first trailer, in this way I'm sure to read //messages from the first to the last received int headerPosition = recMex.LastIndexOf(NetMessages.packetHeader, trailerPosition); //if there is a complete messages elaborate it and delete from the string builder of temp object if (headerPosition > -1 && trailerPosition > -1) { int length = trailerPosition - headerPosition + 1; //Extrapulate the message lock (this) { this._receivedMex = recMex.Substring(headerPosition, length); //Delete the entire message from the string builder netStateObj.receivedMex_Sb.Remove(0, trailerPosition + 1); } OnMessageReceived(); } //Waiting for new messages handler.BeginReceive(netStateObj.recBuffer, 0, netStateObj.bufferSize, 0, new AsyncCallback(ReceiveCallback), netStateObj); } } catch (Exception ex) { _log = "Error: " + ex.Message; } }