/// <summary> /// sends the specified image data as a byte array /// </summary> /// <param name="imgdata"> /// the specified image data as a byte array /// </param> public void sendIMG(byte[] imgdata) { try { sendrecv.sendStartPattern(_Writer); sendrecv.writeOnStream(_Writer, new byte[] { headers.packettypebytes[headers.packettype.IMG] }); sendrecv.writeOnStream(_Writer, BitConverter.GetBytes(((Int32)imgdata.Length))); sendrecv.writeOnStream(_Writer, imgdata); } catch (Exception ex) { if (ex is ConnectionNotWorkingException) { stopAsyncRead(); ConnectionProblems.Invoke("Partner has ConnectionProblems"); } } }
/// <summary> /// sends the specified chat message /// </summary> /// <param name="msg">the chat message which is supposed to be sent</param> public void sendChatMSG(string msg) { try { sendrecv.sendStartPattern(_Writer); sendrecv.writeOnStream(_Writer, new byte[] { headers.packettypebytes[headers.packettype.Chat] }); byte[] bytes = Encoding.UTF8.GetBytes(msg); sendrecv.writeOnStream(_Writer, BitConverter.GetBytes(bytes.Length)); sendrecv.writeOnStream(_Writer, bytes); } catch (Exception ex) { if (ex is ConnectionNotWorkingException) { stopAsyncRead(); ConnectionProblems.Invoke("Partner has ConnectionProblems"); } } }
//sending data: #region send data /// <summary> /// sends the specified laserpointerinfo object as a serialized json string /// </summary> /// <param name="LPInfo"> /// the laserpointerinfo object which is supposed to be sent /// </param> public void sendLaserPointerInfo(LaserPointerInfo LPInfo) { try { sendrecv.sendStartPattern(_Writer); sendrecv.writeOnStream(_Writer, new byte[] { headers.packettypebytes[headers.packettype.LaserPointer] }); string jsonlpinfo = JsonConvert.SerializeObject(LPInfo); byte[] bytes = Encoding.UTF8.GetBytes(jsonlpinfo); //serializing lpinfo object and converting it to a byte array sendrecv.writeOnStream(_Writer, BitConverter.GetBytes((Int32)bytes.Length)); sendrecv.writeOnStream(_Writer, bytes); //sending the laserpointerinfo } catch (Exception ex) { if (ex is ConnectionNotWorkingException) { stopAsyncRead(); ConnectionProblems.Invoke("Partner has ConnectionProblems"); } } }
/// <summary> /// continuesly check every 1.5s if the connection is still alive /// </summary> void checkIfConnAlive() { try { while (asReading) { sendrecv.writeOnStream(_Writer, new byte[] { 1 }); for (int i = 0; i < 15 && asReading; i++) { Thread.Sleep(100); /*waiting 15 times 100ms instead of 1.5 s so this * Thread doesn't prevent the client from stopping async * listening for too long*/ } } } catch { stopAsyncRead(); ConnectionProblems.Invoke("Server is unreachable"); connected = false; } }
/// <summary> /// send the specified file. /// </summary> /// <param name="filepath"> /// the path to the file which is supposed to be sent. /// </param> public void sendFile(string filepath) { try { if (!File.Exists(filepath)) { throw new FileNotFoundException(); } sendrecv.sendStartPattern(_Writer); sendrecv.writeOnStream(_Writer, new byte[] { headers.packettypebytes[headers.packettype.File] }); FileInfo fi = new FileInfo(filepath); byte[] len = BitConverter.GetBytes((Int32)fi.Length); sendrecv.writeOnStream(_Writer, len); sendrecv.writeOnStream(_Writer, File.ReadAllBytes(filepath)); } catch (Exception ex) { if (ex is ConnectionNotWorkingException) { stopAsyncRead(); ConnectionProblems.Invoke("Partner has ConnectionProblems"); } } }
//receiving data: #region receiving data /// <summary> /// invokes the connectionproblems event to inform that the session partner has connection problems /// </summary> private void PartnerhasConnectionProblems() { ConnectionProblems.Invoke("Lost Connection to Partner"); }