Example #1
0
 /// <summary>
 /// Sends packet to the server and returns recieved feedback
 /// </summary>
 /// <param name="packet">packet to send</param>
 /// <returns></returns>
 public CustomPacket SendReceiveMessage(CustomPacket packet)
 {
     lock (sendLock)
     {
         SendMsg(packet, out NetworkStream nwStream);
         return(ReadLong(nwStream));
     }
 }
Example #2
0
        private void SendMsg(CustomPacket packet, out NetworkStream nwStream)
        {
            nwStream = client.GetStream();
            byte[] bytesToSend = Serialize <CustomPacket>(packet, out byte[] len);

            nwStream.Write(len, 0, bytesOfData);
            nwStream.Write(bytesToSend, 0, bytesToSend.Length);
        }
Example #3
0
        private void ServeClient(TcpClient client)
        {
            while (true)
            {
                try
                {
                    NetworkStream nwStream = client.GetStream();
                    nwStream.ReadTimeout = 120000;

                    byte[] readMsgLen = new byte[bytesOfData];
                    nwStream.Read(readMsgLen, 0, bytesOfData);
                    int dataLen = BitConverter.ToInt32(readMsgLen, 0);

                    byte[] buffer = new byte[dataLen];

                    CustomPacket receivedPacket = ReadLong(buffer, dataLen, nwStream);

                    if (receivedPacket.Flag == FlagType.ping)
                    {
                        continue;
                    }
                    else if (receivedPacket.Flag == FlagType.endConnection)
                    {
                        onConnectionClose(client.Client.RemoteEndPoint.ToString());
                        client.Close();
                        return;
                    }
                    //else if (dataReceived == "")
                    //    break;

                    CustomPacket packetToSend = Commands(receivedPacket, client);

                    byte[] bytesToSend = Serialize <CustomPacket>(packetToSend, out byte[] dataToSendLen);

                    nwStream.Write(dataToSendLen, 0, bytesOfData);
                    nwStream.Write(bytesToSend, 0, bytesToSend.Length);
                }
                catch (System.IO.IOException ex)
                {
                    onConnectionLost(client.Client.RemoteEndPoint.ToString(), ex);
                    client.Close();
                    return;
                }
                catch (System.Net.Sockets.SocketException ex)
                {
                    onConnectionLost(client.Client.RemoteEndPoint.ToString(), ex);
                    client.Close();
                    return;
                }
                catch (Exception ex)
                {
                    onConnectionLost(client.Client.RemoteEndPoint.ToString(), ex);
                    client.Close();
                    return;
                }
            }
        }
Example #4
0
 public void Disconnect()
 {
     lock (sendLock)
     {
         CustomPacket packet = new CustomPacket(FlagType.endConnection, HeaderTypes.basic, endConnectionToken, new List <string>(), null);
         SendMsg(packet, out NetworkStream nwStream);
         _shutdownEvent.Set();
         isConnected = false;
         client.Close();
     }
 }
Example #5
0
 /// <summary>
 /// Thread sending pings to the server
 /// </summary>
 private void KeepAlive()
 {
     while (isConnected)
     {
         lock (sendLock)
         {
             CustomPacket packet = new CustomPacket(FlagType.ping, HeaderTypes.basic, "ping", null, null);
             SendMsg(packet, out NetworkStream nwStream);
         }
         if (_shutdownEvent.WaitOne(tickRate))
         {
             break;
         }
     }
 }
Example #6
0
 public virtual CustomPacket Commands(CustomPacket packet, TcpClient client)
 {
     throw new Exception("Commands override not implemented");
 }