insure.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # THIS FILE IS A PART OF VCStudio
  2. # PYTHON 3
  3. ###############################################################################
  4. # With the Multiuser system. It became clear early on that simply using the UDP
  5. # protocol would not be cool. Because it's just not checking enough things.
  6. # In my point of view. I want to send to the end network all the data directly
  7. # and not care too much about what format it is in and stuff.
  8. # So this file will insure that the data will be transferred what ever it is
  9. # just as is. The hard part will be here.
  10. ###############################################################################
  11. import json
  12. def send(c, message):
  13. # This function will do the sending of the message. I will treat every
  14. # message as if it's a file. And will write everything into a json format
  15. # for transferring. Unless it's already in binarry. In which case I will
  16. # keep it that way.
  17. # The mode will be either B ( bytes ) or J ( json ). All the rest will
  18. # handalled by the json madule hopefully.
  19. T = b"B"
  20. if type(message) != bytes:
  21. T = b"J"
  22. message = bytes(json.dumps(message), 'utf-8')
  23. # So now it's bytes anyway and it means we can send it over the network.
  24. # This will be done in 3 stages. TYPE ( T ), AMOUNT ( len(message) )
  25. # and the message it self. Always.
  26. # Now we we going to wait for a send message from the other side.
  27. m = c.recv(4)
  28. m = m.decode('utf8')
  29. while m != "SEND":
  30. print("DE-SYNCED! '"+m+"' NOT 'SEND'")
  31. c.sendall(b"N")
  32. m = c.recv(4)
  33. m = m.decode('utf8')
  34. c.sendall(T)
  35. c.recv(2)
  36. c.sendall(bytes(str(len(message)), 'utf-8'))
  37. c.recv(2)
  38. c.sendall(message)
  39. c.recv(2)
  40. def recv(c):
  41. # This function will do the recieving of the message.
  42. # I guess in order to fix most of the problems with this recv function
  43. # which is a little unsynsing here and there. I will make a fail switch
  44. # so it wil alight it self properly even it network if hidby, bibdy or
  45. # any of my server or client code is terribly unrelible.
  46. c.sendall(b"SEND")
  47. # It might fail or it might work
  48. T = c.recv(1)
  49. T = T.decode('utf8')
  50. tr = 0
  51. while T not in ["B", "J"]:
  52. print("DE-SYNCED! '"+T+"' NOT 'J' OR 'B'")
  53. c.sendall(b"SEND")
  54. T = c.recv(1)
  55. T = T.decode('utf8')
  56. tr = tr + 1
  57. if tr == 8:
  58. exit()
  59. c.sendall(b"OK")
  60. # So here we just recieved the T , Type of our message
  61. SIZE = c.recv(1024)
  62. SIZE = int(SIZE.decode('utf8'))
  63. # Now we recieved our amount. Next is to recieve the message
  64. c.sendall(b"OK")
  65. message = b""
  66. cs = 0
  67. while SIZE > cs:
  68. l = c.recv(SIZE)
  69. cs = cs + len(l)
  70. message = message + l
  71. c.sendall(b"OK")
  72. message[SIZE:]
  73. # Now let's ge the data back from JSON id it's a JSON.
  74. if T == "J":
  75. message = json.loads(message.decode('utf8'))
  76. return message
  77. # P for Pain
  78. # Can't afford to feel the joy
  79. # Can't find strength to feel the pain
  80. # I'll shrink and go and feel no more
  81. # Is it fine to be lame?
  82. # I don't want death. Since death
  83. # Has an opposite effect
  84. # From suffering from being lame
  85. # Death is opposite of pain
  86. # An importance of the pain
  87. # Is that joy is just a drug
  88. # Such as suffering and pain
  89. # It's a head confusing bug
  90. # Joyful, satisfying shit
  91. # make you feel comfortable
  92. # But discomfort and the pain
  93. # It's what is affordable
  94. # Lameness feel pathetic shit
  95. # Me described using four words
  96. # No more joy, only regrets
  97. # Five more words of discomfort
  98. # Shrink from big and fall from tall
  99. # Give up everything you have
  100. # Go homeless, starve, and suffer. Feel.
  101. # The pain inducing hollow and venomous discomfort,
  102. # swallowing all and every thought and only
  103. # pain. Pain. And nothing more exists.
  104. # And nothing more is fair.
  105. # Can't afford to feel the joy
  106. # But find strength to feel the pain
  107. # I'll shrink and go and feel no more
  108. # It is fine to be lame.
  109. # https://open.lbry.com/@blenderdumbass:f/P-for-Pain:f?r=GRS5mK5GDEGa77vRotG4LwMqPCyhpF2k