# THIS FILE IS A PART OF VCStudio
# PYTHON 3

###############################################################################

# With the Multiuser system. It became clear early on that simply using the UDP
# protocol would not be cool. Because it's just not checking enough things.
# In my point of view. I want to send to the end network all the data directly
# and not care too much about what format it is in and stuff.

# So this file will insure that the data will be transferred what ever it is
# just as is. The hard part will be here.

###############################################################################

import json

def send(c, message):
    
    # This function will do the sending of the message. I will treat every 
    # message as if it's a file. And will write everything into a json format
    # for transferring. Unless it's already in binarry. In which case I will
    # keep it that way.
    
    # The mode will be either B ( bytes ) or J ( json ). All the rest will 
    # handalled by the json madule hopefully.
    
    T = b"B"
    if type(message) != bytes:
        T = b"J"
        message = bytes(json.dumps(message), 'utf-8')
    
    # So now it's bytes anyway and it means we can send it over the network.
    # This will be done in 3 stages. TYPE ( T ), AMOUNT  ( len(message) )
    # and the message it self. Always.
    
    # Now we we going to wait for a send message from the other side.
    m = c.recv(4)
    m = m.decode('utf8')
    while m != "SEND":
        print("DE-SYNCED! '"+m+"' NOT 'SEND'")
        c.sendall(b"N")
        m = c.recv(4)
        m = m.decode('utf8')
    
    
    c.sendall(T)
    c.recv(2)
    c.sendall(bytes(str(len(message)), 'utf-8'))
    c.recv(2)
    c.sendall(message)
    c.recv(2)
    
def recv(c):
    
    # This function will do the recieving of the message.
    
    # I guess in order to fix most of the problems with this recv function
    # which is a little unsynsing here and there. I will make a fail switch
    # so it wil alight it self properly even it network if hidby, bibdy or
    # any of my server or client code is terribly unrelible. 
    
    c.sendall(b"SEND")
    
    # It might fail or it might work
    
    T = c.recv(1)
    T = T.decode('utf8')
    tr = 0
    while T not in ["B", "J"]:
        print("DE-SYNCED! '"+T+"' NOT 'J' OR 'B'")        
        c.sendall(b"SEND")
        T = c.recv(1)
        T = T.decode('utf8')
        tr = tr + 1
        if tr == 8:
            exit()
    
    c.sendall(b"OK")
    
    # So here we just recieved the T , Type of our message
    
    SIZE = c.recv(1024)
    SIZE = int(SIZE.decode('utf8'))
    
    # Now we recieved our amount.  Next is to recieve the message
    c.sendall(b"OK")
    
    message = b""
    cs = 0
            
    while SIZE > cs:
        l = c.recv(SIZE)
        cs = cs + len(l)
        message = message + l
    
    c.sendall(b"OK")
    
    message[SIZE:]
    
    # Now let's ge the data back from JSON id it's a JSON.
    if T == "J":
        message = json.loads(message.decode('utf8'))
    
    return message






    
#                  P for Pain    
    
#    Can't afford to feel the joy
#    Can't find strength to feel the pain
#    I'll shrink and go and feel no more
#    Is it fine to be lame?

#    I don't want death. Since death
#    Has an opposite effect
#    From suffering from being lame
#    Death is opposite of pain

#    An importance of the pain
#    Is that joy is just a drug
#    Such as suffering and pain
#    It's a head confusing bug

#    Joyful, satisfying shit
#    make you feel comfortable
#    But discomfort and the pain
#    It's what is affordable

#    Lameness feel pathetic shit
#    Me described using four words
#    No more joy, only regrets
#    Five more words of discomfort

#    Shrink from big and fall from tall
#    Give up everything you have
#    Go homeless, starve, and suffer. Feel.
#    The pain inducing hollow and venomous discomfort,
#    swallowing all and every thought and only
#    pain. Pain. And nothing more exists.
#    And nothing more is fair.

#    Can't afford to feel the joy
#    But find strength to feel the pain
#    I'll shrink and go and feel no more
#    It is fine to be lame.

#    https://open.lbry.com/@blenderdumbass:f/P-for-Pain:f?r=GRS5mK5GDEGa77vRotG4LwMqPCyhpF2k